Skip to main content

Unofficial Schwab API wrapper in Python 3.

Project description

Schwab API

This is not an official API or even a stable recreation of a Charles Schwab API. Functionality may change with any updates made by Schwab. As of December 2023, this API continues to work as expected.

This package enables buying and selling securities programmatically on Charles Schwab. Currently, we use a headless browser to automate logging in in order to get authorization cookies. All other functionality is done through web requests made to Schwab's own API.

Features

  • Buying and Selling tickers
  • Get quotes for multiple tickers
  • Get order information
  • Account and Position Information
  • Limit / Stop orders are possible using trade_v2 parameters
  • Multiple individual account support
  • MFA and TOTP authentication
  • Web Request implementation (with the exception of authentication)

Live Demo

I am currently using this package to place trades on Schwab using my website here.

Screenshot

Contribution

I would absolutely love contributions; as someone new to open source, I'd appreciate help in setting up a reliable system for PRs as well :)

Getting Started

Installing

Install using pypi and then download and install the playwright binaries:

pip install schwab-api
python -m playwright install

Set up your account to use TOTP

In order to login to Schwab without having to go through SMS verification everytime, you'll need to create an authentication token (TOTP) and attach that to your Schwab account.

  1. Download a TOTP app like Google Authenticator.
  2. Go to my website and generate a TOTP key there by clicking 'Generate TOTP' and following the instructions. You should get a Symantec ID and a TOTP key/QR code.

Alternatively, you can do this programmatically:

from schwab_api import generate_totp

symantec_id, totp_secret = generate_totp()

print("Your symantec ID is: " + symantec_id)
print("Your TOTP secret is: " + totp_secret)
  1. Open Google Authenticator and click the + button to add a new account
  2. Either enter the TOTP key manually and scan the QR code from step 2.
  3. Log in to the Schwab security center
  4. Under Two-Step Verification, select Always at Login, and then select "Security Token" as your method.
  5. Enter the Symantec ID from step 2 into the Credential ID field.
  6. Enter the 6-digit code from Google Authenticator into the Security Code field.
  7. Done! Now keep your TOTP secret from step 2 handy as your SCHWAB_TOTP_SECRET in your .env file under the example directory.

Quickstart

You can run this code in a Colab Notebook here.

Here's some code that logs in, gets all account holdings, and makes a stock purchase:

from schwab_api import Schwab
import pprint

# Initialize our schwab instance
api = Schwab()

# Login using playwright
print("Logging into Schwab")
logged_in = api.login(
    username=username,
    password=password,
    totp_secret=totp_secret # Get this by generating TOTP at https://itsjafer.com/#/schwab
)

# Get information about a few tickers
quotes = api.quote_v2(["PFE", "AAPL"])
pprint.pprint(quotes)

# Get information about all accounts holdings
print("Getting account holdings information")
account_info = api.get_account_info()
pprint.pprint(account_info)

print("The following account numbers were found: " + str(account_info.keys()))

print("Placing a dry run trade for AAPL stock")
# Place a dry run trade for account 99999999
messages, success = api.trade_v2(
    ticker="AAPL", 
    side="Buy", #or Sell
    qty=1, 
    account_id=99999999, # Replace with your account number
    dry_run=True # If dry_run=True, we won't place the order, we'll just verify it.
)

print("The order verification was " + "successful" if success else "unsuccessful")
print("The order verification produced the following messages: ")
pprint.pprint(messages)

TODO

  • Currently, we use a headless browser to login to Schwab; in the future, we want to do this purely with requests.
  • Documentation of functionality

Development Guide

Want to extend functionality? Here's how to get started:

  1. After forking, install dependencies:
pip install .
playwright install && playwright install-deps
  1. Run the example script:
pip install . && python example/example.py
  1. Iterate on existing code:
  • Authentication largely exists in schwab_api/authentication.py and is done using Playwright.
  • Trading happens in schwab_api/schwab.py in two functions: trade and trade_v2 which use the legacy and new API respectively. Neither of these APIs are documented and were largely just reverse engineering through sniffing network requests in the UI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

schwab_api-0.3.3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

schwab_api-0.3.3-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file schwab_api-0.3.3.tar.gz.

File metadata

  • Download URL: schwab_api-0.3.3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for schwab_api-0.3.3.tar.gz
Algorithm Hash digest
SHA256 73aaf8b943c68e223c2359e3c2a96e1105fe7958a7716fc89ff0d4d8b90f4684
MD5 e3e11ea87bc4f2e994db9b4fc786517d
BLAKE2b-256 ab4012fa1c293138e163129f505bc3fa1e6b464ef8d426f7b7284acd80f3458f

See more details on using hashes here.

File details

Details for the file schwab_api-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: schwab_api-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for schwab_api-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8e7b4cbaaaf264b4f4f42b674693b8b24db00989cfb9f07b6b6aaea18f9b1b3f
MD5 0825d4a23e96a9b6cbbcadb216ba563a
BLAKE2b-256 243886ddff69557b7515e4ed0f8a6da7de3c2fea57f580d9f013cbe8be611626

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page