Package: schwab-api2
Schwab APIThis 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.ContributionI 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 StartedInstallingInstall using pypi and then download and install the playwright binaries:pip install schwab-api
python -m playwright installCreate a TOTP Authentication TokenIn 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: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.Keep this TOTP secret handyas you'll need to pass it to this API in order to login.For the Symantec ID:Log in to Schwab and go to yoursecurity center.Under two-step verification, select "Always at login", and then select "Security Token".Enter the symantec ID here that you generated using the code above.QuickstartHere'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()FeaturesBuying and Selling tickersMultiple individual account supportMFA and TOTP authenticationAccount and Position InformationHeadless playwright implementationTODOAdd Cancel OrdersAdd OptionsGive me Ideas!