A modern C++ SDK for the Alpaca Trading and Market Data APIs.
- Trading API v2: Full support for account, orders, positions, assets, watchlists, and portfolio history
- Market Data API v2: Historical bars, latest trades, and latest quotes for stocks
- Modern C++20: Clean API with proper namespacing and modern idioms
- CMake-based: Easy integration with CMake-based projects
- Header-only friendly: Include only what you need
- Warning-free builds: Third-party library warnings silenced; our code compiles with strict warnings (
-Wall -Wextra -Wpedantic)
- C++20 compatible compiler (GCC 11+, Clang 14+, MSVC 2022+)
- CMake 3.25+
- OpenSSL development libraries
# Clone the repository
git clone https://github.com/your-org/alpaca-markets-cpp.git
cd alpaca-markets-cpp
# Build
make build
# Run tests
make testThere are three main ways to use this library in your project:
The easiest way to use alpaca-markets-cpp is with CMake's FetchContent module:
cmake_minimum_required(VERSION 3.25)
project(my_trading_app)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
alpaca_markets
GIT_REPOSITORY https://github.com/your-org/alpaca-markets-cpp.git
GIT_TAG main # or a specific tag like v1.0.0
)
FetchContent_MakeAvailable(alpaca_markets)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE alpaca::markets)When used via FetchContent:
- Tests and examples are automatically disabled for the dependency
- Dependencies (RapidJSON, cpp-httplib) are fetched automatically
- No system installation required
Install the library to your system or a custom prefix:
# Configure and build
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
# Build
cmake --build build --config Release
# Install (to system or custom prefix)
cmake --install build --prefix /usr/local # or ~/.local, /opt/alpaca, etc.Then use in your project:
cmake_minimum_required(VERSION 3.20)
project(my_trading_app)
set(CMAKE_CXX_STANDARD 20)
# Find the installed package
find_package(alpaca_markets REQUIRED)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE alpaca::markets)If installed to a non-standard prefix, add it to CMAKE_PREFIX_PATH:
cmake -B build -DCMAKE_PREFIX_PATH=/opt/alpacagit submodule add https://github.com/your-org/alpaca-markets-cpp.git external/alpaca-markets-cppadd_subdirectory(external/alpaca-markets-cpp)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE alpaca::markets)The library requires these system dependencies:
| Dependency | Purpose | Install (Ubuntu/Debian) |
|---|---|---|
| OpenSSL | HTTPS support | apt install libssl-dev |
| ZLIB | Compression | apt install zlib1g-dev |
| Brotli (optional) | Brotli compression | apt install libbrotli-dev |
ALPACA_MARKETS_USE_SYSTEM_RAPIDJSON=ONto use a system RapidJSON package.ALPACA_MARKETS_USE_SYSTEM_HTTPLIB=ONto use a system cpp-httplib package.
Set the following environment variables before running:
# Required
export APCA_API_KEY_ID="your-api-key"
export APCA_API_SECRET_KEY="your-secret-key"
# Optional (defaults to paper trading)
export APCA_API_BASE_URL="https://paper-api.alpaca.markets" # or https://api.alpaca.markets for live
export APCA_API_DATA_URL="https://data.alpaca.markets"Or use the new naming convention:
export ALPACA_MARKETS_KEY_ID="your-api-key"
export ALPACA_MARKETS_SECRET_KEY="your-secret-key"
export ALPACA_MARKETS_TRADING_URL="https://paper-api.alpaca.markets" # base host (no /v2)
export ALPACA_MARKETS_DATA_URL="https://data.alpaca.markets"If you prefer a .env file, this repo also supports a more explicit naming scheme (and maps it to the variables above):
# Paper trading (endpoint + key id)
ALPACA_MARKETS_PAPER_TRADING_API_ENDPOINT="https://paper-api.alpaca.markets"
ALPACA_MARKETS_PAPER_TRADING_API_KEY_ID="your-paper-key-id"
# Live/individual (endpoint + key id + secret)
ALPACA_MARKETS_INDIVIDUAL_API_ENDPOINT="https://api.alpaca.markets"
ALPACA_MARKETS_INDIVIDUAL_API_KEY_ID="your-live-key-id"
ALPACA_MARKETS_INDIVIDUAL_API_SECRET_KEY="your-live-secret"#include <alpaca/markets/markets.hpp>
#include <iostream>
int main() {
// Parse environment configuration
alpaca::markets::Environment env;
if (alpaca::markets::Status status = env.parse(); !status.ok()) {
std::cerr << "Error: " << status.getMessage() << std::endl;
return 1;
}
// Create client
alpaca::markets::Client client(env);
// Get account information
auto [status, account] = client.getAccount();
if (!status.ok()) {
std::cerr << "Error: " << status.getMessage() << std::endl;
return 1;
}
std::cout << "Account ID: " << account.id << std::endl;
std::cout << "Buying Power: $" << account.buying_power << std::endl;
// Get latest quote for a stock
auto [quote_status, quote] = client.getLatestQuote("AAPL");
if (quote_status.ok()) {
std::cout << "AAPL Bid: $" << quote.quote.bid_price << std::endl;
std::cout << "AAPL Ask: $" << quote.quote.ask_price << std::endl;
}
return 0;
}graph TD
subgraph SDK["Alpaca Markets C++ SDK"]
Client["REST Client"]
Stream["Streaming (placeholder)"]
Models["Models / DTOs"]
end
subgraph Alpaca["Alpaca APIs"]
TradingAPI["Trading API v2"]
DataAPI["Market Data API v2"]
StreamAPI["WebSocket Stream"]
end
Client -->|HTTPS| TradingAPI
Client -->|HTTPS| DataAPI
Stream -.->|WSS| StreamAPI
Client --> Models
Stream --> Models
sequenceDiagram
participant App as Application
participant Client as Client
participant HTTP as cpp-httplib
participant API as Alpaca API
participant Model as Model
App->>Client: getAccount()
Client->>HTTP: GET /v2/account
HTTP->>API: HTTPS request
API-->>HTTP: JSON response
HTTP-->>Client: response body
Client->>Model: parse JSON
Model-->>Client: Account object
Client-->>App: {Status, Account}
alpaca-markets-cpp/
├── CMakeLists.txt # Main CMake configuration
├── Makefile # Developer workflow targets
├── include/
│ └── alpaca/
│ └── markets/ # Public headers
│ ├── markets.hpp # Umbrella header
│ ├── client.hpp # REST API client
│ ├── config.hpp # Environment configuration
│ └── ... # Model headers
├── src/ # Implementation files
├── tests/ # Unit tests
├── examples/ # Example applications
└── cmake/ # CMake modules (for future use)
getAccount()- Get account informationgetAccountConfigurations()- Get account settingsupdateAccountConfigurations()- Update account settingsgetAccountActivity()- Get account activity history
getOrders()- List ordersgetOrder()- Get specific ordersubmitOrder()- Submit new order (supports trailing stop withtrail_price/trail_percent)submitNotionalOrder()- Submit order by dollar amount (fractional shares)replaceOrder()- Replace existing ordercancelOrder()- Cancel ordercancelOrders()- Cancel all orders
Order Types: Market, Limit, Stop, StopLimit, TrailingStop
Order Classes: Simple, Bracket, OneCancelsOther, OneTriggersOther, MultiLeg
getPositions()- List all positionsgetPosition()- Get position for symbolclosePosition()- Close positionclosePositions()- Close all positions
getAssets()- List assets (supportsUSEquityandCryptoasset classes)getAsset()- Get asset details
getBars()- Get historical bar datagetLatestTrade()- Get latest trade for symbolgetLatestQuote()- Get latest quote for symbolgetLatestTrades()- Get latest trades for multiple symbolsgetLatestQuotes()- Get latest quotes for multiple symbolsgetLatestBar()- Get latest bar for symbolgetLatestBars()- Get latest bars for multiple symbolsgetSnapshot()- Get market snapshot (trade, quote, bars) for symbolgetSnapshots()- Get market snapshots for multiple symbolsgetTrades()- Get historical trades with paginationgetQuotes()- Get historical quotes with paginationgetMultiTrades()- Get historical trades for multiple symbols with paginationgetMultiQuotes()- Get historical quotes for multiple symbols with paginationgetAuctions()- Get auction data (opening/closing) for a symbolgetMultiAuctions()- Get auction data for multiple symbols
getCorporateActions()- Get corporate actions (splits, dividends, mergers, name changes, etc.)
getNews()- Get news articles with symbol filtering and pagination
getLatestCryptoTrade()/getLatestCryptoTrades()- Latest crypto tradesgetLatestCryptoQuote()/getLatestCryptoQuotes()- Latest crypto quotesgetLatestCryptoBar()/getLatestCryptoBars()- Latest crypto barsgetCryptoSnapshot()/getCryptoSnapshots()- Crypto market snapshotsgetCryptoBars()- Historical crypto barsgetCryptoTrades()- Historical crypto tradesgetCryptoQuotes()- Historical crypto quotes
getOptionContracts()- List option contracts with filtersgetOptionContract()- Get specific option contract by symbol or ID
getWatchlists()- List watchlistsgetWatchlist()- Get watchlist by IDcreateWatchlist()- Create new watchlistupdateWatchlist()- Update watchlistdeleteWatchlist()- Delete watchlist
getAnnouncements()- Get corporate action announcements (dividends, splits, mergers, spinoffs)getAnnouncement()- Get specific announcement by ID
getClock()- Get market clockgetCalendar()- Get market calendar
The SDK provides typed API errors similar to official SDKs:
// APIError provides detailed error information from the Alpaca API
alpaca::markets::APIError err(422, 40010000, "insufficient qty available for order");
std::cerr << err.what() << std::endl;
// Output: insufficient qty available for order (HTTP 422, Code 40010000)
// Convert to Status for uniform error handling
alpaca::markets::Status status = err.toStatus();Configure request resiliency with retry and timeout settings:
alpaca::markets::Environment env;
env.parse();
// Configure retries with exponential backoff
alpaca::markets::RetryConfig retry;
retry.max_retries = 5;
retry.initial_delay = std::chrono::milliseconds{100};
retry.max_delay = std::chrono::milliseconds{5000};
retry.backoff_multiplier = 2.0;
env.setRetryConfig(retry);
// Configure timeouts
alpaca::markets::TimeoutConfig timeout;
timeout.connection_timeout = std::chrono::seconds{30};
timeout.read_timeout = std::chrono::seconds{60};
env.setTimeoutConfig(timeout);Use PageIterator for convenient iteration over paginated results:
#include <alpaca/markets/pagination.hpp>
// Manually iterate through pages
auto [status, result] = client.getTrades("AAPL", start, end, 100);
while (status.ok() && !result.second.empty()) {
for (const auto& trade : result.first) {
process(trade);
}
auto [s, r] = client.getTrades("AAPL", start, end, 100, result.second);
status = s;
result = r;
}| Target | Description |
|---|---|
make build |
Build the project |
make test |
Run unit tests |
make lint |
Run linting (requires clang-format, clang-tidy) |
make clean |
Remove build directory |
make help |
Show available targets |
This C++ SDK aims to provide feature parity with Alpaca's official SDKs:
| Feature | Status |
|---|---|
| Trading API v2 | ✅ |
| Market Data API v2 | ✅ |
| Options Contracts API | ✅ |
| Market Snapshots | ✅ |
| Historical Trades/Quotes | ✅ |
| Multi-Symbol Historical Data | ✅ |
| Auctions Data | ✅ |
| Market Data Corporate Actions | ✅ |
| Trailing Stop Orders | ✅ |
| Notional (Dollar) Orders | ✅ |
| Multi-Symbol Quotes/Trades | ✅ |
| Corporate Actions (Trading) | ✅ |
| Crypto Asset Class | ✅ |
| Typed API Errors | ✅ |
| Crypto Market Data | ✅ |
| News API | ✅ |
| Retry/Backoff Configuration | ✅ |
| Timeout Configuration | ✅ |
| Pagination Helpers | ✅ |
| WebSocket Streaming | 🔄 Placeholder |
See LICENSE file.