A lightweight C++20 wrapper for the Charles Schwab Developer REST API, providing easy-to-use methods for fetching market data, option chains, quotes, and more.
- Authenticate via OAuth2 with automatic token refresh
- Convert human-readable dates to Unix epoch (ms)
- Fetch historical price data (OHLCV)
- Retrieve option chains and expiration series
- Query market hours and top movers
- Search instrument details and quotes
- Thread-safe helper utilities
- C++20-compatible compiler (e.g., GCC 11+, Clang 13+)
- libcurl development headers
- CMake (optional) or GNU Make
Clone and build with Make:
git clone https://github.com/yourusername/schwab-cpp-client.git
cd schwab-cpp-client
make # build static library + examples
make install PREFIX=/usr/local # optional system-wide installOr use CMake:
cmake -B build -S .
cmake --build build- Register an application at the Charles Schwab Developer Portal.
- Note your App Key, App Secret, and Redirect URI.
- Create a local
tokens.json(or any filename you prefer) to store and refresh OAuth tokens.
#include "schwab_api.hpp"
#include "utils.hpp"
using namespace std;
int main() {
Client client(
"your-app-key",
"your-app-secret",
"http://localhost/callback",
"tokens.json",
chrono::milliseconds(5000) // 5 s timeout
);
// Fetch Apple (AAPL) daily price history for the past year
map<string,string> params = {
{"symbol", "AAPL"},
{"periodType", "year"},
{"frequencyType", "daily"}
};
string json = client.priceHistory(params);
cout << json << endl;
return 0;
}Client(
const string& appKey,
const string& appSecret,
const string& callbackUrl,
const string& tokensFile,
const chrono::milliseconds timeoutMs
);| Method | Description |
|---|---|
long long datetimeToEpoch("dd-mm-yyyy HH:MM:SS") |
Convert to epoch ms |
long long dateToEpoch("dd-mm-yyyy") |
Convert date (00:00:00) to epoch ms |
bool containsReqArgs(params, reqArgs) |
Ensure required keys present |
bool validKeys(params, valKeys) |
Validate parameter names |
| Method | Purpose | Key Parameters |
|---|---|---|
priceHistory(params) |
OHLCV history | Required symbol; optional periodType, frequencyType, period, frequency, startDate, endDate, … |
optionChains(params) |
Option chains | Required symbol; optional contractType, strikeCount, strategy, … |
optionExpirationChains(symbol) |
Expiration dates | — |
marketHours(markets, date) |
Market hours | markets = equity, bond, option, future, forex; date = YYYY-MM-DD or TODAY |
movers(indexSymbol, sort, frequency) |
Top movers | e.g. $DJI, sort by VOLUME, … |
instruments(symbol, projection) |
Instrument search | projection = fundamental, symbol-search, … |
instruments(cusip) |
Instrument by CUSIP | — |
quotes(symbols, fields, indicative) |
Quotes list | symbols comma-separated, optional fields, indicative |
quotes(symbol, fields) |
Single-symbol quotes | — |
Distributed under the MIT License. See the LICENSE file for full text. end of readme