Skip to main content

Unofficial Schwab API wrapper in Python 3.

Reason this release was yanked:

upstream is current now

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 April 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. Account info and stock positions are pulled through requests. Orders are placed with playwright. This is a regression from the original api from itsjafer, but is necessary for now. During orders the api gets a new authorization key multiple times and I am not sure how to do this with requests.

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

Create a TOTP Authentication Token

In order to login to Schwab without having to go through SMS verification everytime, we can create an authentication token (TOTP) and attach that to our account.

Alternatively, you can do this programmatically:

from schwab_api2 import generate_totp

symantec_id, totp_secret = generate_totp()

print("Your symantec ID is: " + symantec_id)
print("Your TOTP secret is: " + totp_secret)

For the TOTP Secret:

  1. Download Duo Mobile, Google Authenticator, or any other authenticator of your choice and create an entry using the TOTP secret. You will be prompted to generate a code everytime you log in to Schwab and will need an authenticator app to do so.
  2. Keep this TOTP secret handy as you'll need to pass it to this API in order to login.

For the Symantec ID:

  1. Log in to Schwab and go to your security center.
  2. Under two-step verification, select "Always at login", and then select "Security Token".
  3. Enter the symantec ID here that you generated using the code above.

Quickstart

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

from schwab_api2 import Schwab
import pprint

# Change these variables
username = "username"
password = "password"
totp_secret = "ABCDEF1234567890"

# Create the schwab instance
api = Schwab(
    headless=True,
    browserType="firefox"
)

# Login using playwright
print("Logging into Schwab")
logged_in = api.login(
    username=username,
    password=password,
    totp_secret=totp_secret
)

# Get information about all accounts holdings
print("Getting account holdings information")
account_info = api.get_account_info()
pprint.pprint(account_info)
accounts = list(account_info.keys())
print(f"The following account numbers were found: {accounts}")


# Place a dry run trade for each account
print("Placing a dry run trade for INTC stock")

for account in accounts:
    messages, success = api.trade(
        ticker="INTC",
        action="Buy",
        qty=1,
        order_type="Market",
        account_id=str(account),
        price=0.00,
        dry_run=True
    )

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

api.close_session()

Features

  • Buying and Selling tickers
  • Multiple individual account support
  • MFA and TOTP authentication
  • Account and Position Information
  • Headless playwright implementation

TODO

  • Add Cancel Orders
  • Add Options
  • Give me Ideas!

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_api2-0.2.9.tar.gz (11.2 kB view details)

Uploaded Source

File details

Details for the file schwab_api2-0.2.9.tar.gz.

File metadata

  • Download URL: schwab_api2-0.2.9.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for schwab_api2-0.2.9.tar.gz
Algorithm Hash digest
SHA256 3c3d57d14c25668b069ebe244d9c76790a1f5d5d1b7b7d02e6d93fb6af62ebd4
MD5 085aeb2687b1b3095e4fa3ff2fbf175e
BLAKE2b-256 5381e4d7be77c1dc49e5c4e0f059e129abe8f1abe89bce73f782369493d5fe94

See more details on using hashes here.

Supported by

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