Charles Schwab API
Documentation for the Schwab API
The Schwab client provides access to Schwab's API for accounts, trading, and market data operations. This client allows you to initialize a connection with Schwab, perform account-related actions, manage orders, and retrieve market data such as quotes, price history, and option chains.
Make sure you have a Schwab and Schwab developer account:
Set up your Schwab developer account here.
Create a new Schwab individual developer app with callback URL "https://127.0.0.1" (case sensitive)
Wait until the status is "Ready for use", note that "Approved - Pending" will not work.
Enable TOS (Thinkorswim) for your Schwab account, it is needed for orders and other API calls.
Schwab Client Import Statement
pfrom marketquant.data_provider import schwab
Usage Overview
To use the Schwab API client, you must initialize the client by loading environment variables that contain your Schwab API credentials (app_key
, app_secret
, callback_url
). Once initialized, you can perform various actions related to accounts, orders, and market data.
Class: SchwabInitializer
SchwabInitializer
The SchwabInitializer
class sets up the Schwab API client, loading the required credentials from environment variables.
Method: get_client
get_client
The get_client
method returns the initialized Schwab API client to be used for further API calls.
SchwabInitializer.get_client()
Returns:
An initialized Schwab API client with the required credentials.
Method: schwab
schwab
The schwab
function is a utility method to instantiate and return the Schwab API client using the SchwabInitializer
.
schwab()
Example Usage:
from marketquant.data_provider import schwab
# Initialize the Schwab API client
schwab_api = schwab()
Example Usage
Here’s an example of how to use the Schwab client to fetch account details and quotes:
from marketquant.data_provider import schwab
# Initialize the Schwab API client
schwab_api = schwab()
# Get linked accounts
accounts = schwab_api.account_linked()
# Fetch account details for a specific account
account_hash = accounts.json()['linkedAccounts'][0]['accountHash']
account_details = schwab_api.account_details(accountHash=account_hash)
# Fetch price history for Apple stock
price_history = schwab_api.price_history(symbol="AAPL", periodType="day", period=5, frequencyType="minute", frequency=1)
Last updated