# Schwab API
**This is not an official API wrapper. Functionality may change with any updates made by Schwab. As of June 2024, 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
* Trading options
* Get quotes for multiple tickers
* Get order information
* Get transaction history
* Get tax lot info
* Account and Position Information
* Limit / Stop orders are possible using trade_v2 parameters
* Multiple individual account support
* MFA and TOTP authentication
* Session caching
* Async compatibility
## Live Demo
I am currently using this package to place trades on Schwab using my website [here](https://itsjafer.com/#/reversesplit).
![Screenshot](screenshot.png)
## 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](https://itsjafer.com/#/schwab) 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)
```
3. Open Google Authenticator and click the `+` button to add a new account
4. Either enter the TOTP key manually and scan the QR code from step 2.
5. Log in to the Schwab [security center](https://client.schwab.com/app/access/securitysettings/#/security/verification)
6. Under Two-Step Verification, select Always at Login, and then select "Security Token" as your method.
7. Enter the Symantec ID from step 2 into the Credential ID field.
8. Enter the 6-digit code from Google Authenticator into the Security Code field.
9. 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](https://github.com/itsjafer/schwab-api/blob/main/Schwab_API_Example.ipynb).
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
```
2. Run the example script:
```
pip install . && python example/example.py
```
3. 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.
### Deployment
Bumping the version number in `setup.py` will automatically trigger a deployment that must be approved by itsjafer@.
Raw data
{
"_id": null,
"home_page": "https://github.com/itsjafer/schwab-api",
"name": "schwab-api",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "schwab, python3, api, unofficial, schwab-api, schwab charles api",
"author": "Jafer Haider",
"author_email": "itsjafer@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e6/79/903ebf88645e9fa895e59f4631dd0d21ff90cd3a043ed7fa266e73a500c8/schwab_api-0.4.3.tar.gz",
"platform": null,
"description": "# Schwab API\n\n**This is not an official API wrapper. Functionality may change with any updates made by Schwab. As of June 2024, this API continues to work as expected.**\n\nThis 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.\n\n## Features\n\n* Buying and Selling tickers\n* Trading options\n* Get quotes for multiple tickers\n* Get order information\n* Get transaction history\n* Get tax lot info\n* Account and Position Information\n* Limit / Stop orders are possible using trade_v2 parameters\n* Multiple individual account support\n* MFA and TOTP authentication\n* Session caching\n* Async compatibility\n\n## Live Demo\n\nI am currently using this package to place trades on Schwab using my website [here](https://itsjafer.com/#/reversesplit).\n\n![Screenshot](screenshot.png)\n\n## Getting Started\n\n### Installing\n\nInstall using pypi and then download and install the playwright binaries:\n\n```\npip install schwab-api\npython -m playwright install\n```\n\n### Set up your account to use TOTP\n\nIn 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.\n\n\n1. Download a TOTP app like Google Authenticator.\n2. [Go to my website and generate a TOTP key there](https://itsjafer.com/#/schwab) by clicking 'Generate TOTP' and following the instructions. You should get a Symantec ID and a TOTP key/QR code.\n\nAlternatively, you can do this programmatically:\n\n```\nfrom schwab_api import generate_totp\n\nsymantec_id, totp_secret = generate_totp()\n\nprint(\"Your symantec ID is: \" + symantec_id)\nprint(\"Your TOTP secret is: \" + totp_secret)\n```\n\n3. Open Google Authenticator and click the `+` button to add a new account\n4. Either enter the TOTP key manually and scan the QR code from step 2.\n5. Log in to the Schwab [security center](https://client.schwab.com/app/access/securitysettings/#/security/verification)\n6. Under Two-Step Verification, select Always at Login, and then select \"Security Token\" as your method.\n7. Enter the Symantec ID from step 2 into the Credential ID field.\n8. Enter the 6-digit code from Google Authenticator into the Security Code field.\n9. Done! Now keep your TOTP secret from step 2 handy as your `SCHWAB_TOTP_SECRET` in your `.env` file under the example directory.\n\n### Quickstart\n\nYou can run this code in a [Colab Notebook here](https://github.com/itsjafer/schwab-api/blob/main/Schwab_API_Example.ipynb).\n\nHere's some code that logs in, gets all account holdings, and makes a stock purchase:\n```\nfrom schwab_api import Schwab\nimport pprint\n\n# Initialize our schwab instance\napi = Schwab()\n\n# Login using playwright\nprint(\"Logging into Schwab\")\nlogged_in = api.login(\n username=username,\n password=password,\n totp_secret=totp_secret # Get this by generating TOTP at https://itsjafer.com/#/schwab\n)\n\n# Get information about a few tickers\nquotes = api.quote_v2([\"PFE\", \"AAPL\"])\npprint.pprint(quotes)\n\n# Get information about all accounts holdings\nprint(\"Getting account holdings information\")\naccount_info = api.get_account_info()\npprint.pprint(account_info)\n\nprint(\"The following account numbers were found: \" + str(account_info.keys()))\n\nprint(\"Placing a dry run trade for AAPL stock\")\n# Place a dry run trade for account 99999999\nmessages, success = api.trade_v2(\n ticker=\"AAPL\", \n side=\"Buy\", #or Sell\n qty=1, \n account_id=99999999, # Replace with your account number\n dry_run=True # If dry_run=True, we won't place the order, we'll just verify it.\n)\n\nprint(\"The order verification was \" + \"successful\" if success else \"unsuccessful\")\nprint(\"The order verification produced the following messages: \")\npprint.pprint(messages)\n```\n\n## TODO\n\n* Currently, we use a headless browser to login to Schwab; in the future, we want to do this purely with requests.\n* Documentation of functionality\n\n## Development Guide\n\nWant to extend functionality? Here's how to get started:\n\n1. After forking, install dependencies:\n```\npip install .\nplaywright install && playwright install-deps\n```\n\n2. Run the example script:\n```\npip install . && python example/example.py\n```\n\n3. Iterate on existing code: \n* Authentication largely exists in `schwab_api/authentication.py` and is done using Playwright.\n* 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.\n\n### Deployment\n\nBumping the version number in `setup.py` will automatically trigger a deployment that must be approved by itsjafer@.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Unofficial Schwab API wrapper in Python 3.",
"version": "0.4.3",
"project_urls": {
"Download": "https://github.com/itsjafer/schwab-api/tarball/master",
"Homepage": "https://github.com/itsjafer/schwab-api"
},
"split_keywords": [
"schwab",
" python3",
" api",
" unofficial",
" schwab-api",
" schwab charles api"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0033d0c5442d84a7bc4e6d81b9f7d7fc65c1f121dbb881d6e520471b0100a6ff",
"md5": "3dc89010395f28a6c66eb67bbcac80a8",
"sha256": "18e32a1eb02dd387146e9921056d95b74cfe1e6e6a876a45a9b3efae53a18ad9"
},
"downloads": -1,
"filename": "schwab_api-0.4.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3dc89010395f28a6c66eb67bbcac80a8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 16780,
"upload_time": "2024-07-20T12:52:38",
"upload_time_iso_8601": "2024-07-20T12:52:38.810476Z",
"url": "https://files.pythonhosted.org/packages/00/33/d0c5442d84a7bc4e6d81b9f7d7fc65c1f121dbb881d6e520471b0100a6ff/schwab_api-0.4.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e679903ebf88645e9fa895e59f4631dd0d21ff90cd3a043ed7fa266e73a500c8",
"md5": "dd1c52ade24d445899742301e37d78eb",
"sha256": "c6c653bfed9f03ae2ece943ddb49a5974e15131aa01c6fa2032965957851bcf3"
},
"downloads": -1,
"filename": "schwab_api-0.4.3.tar.gz",
"has_sig": false,
"md5_digest": "dd1c52ade24d445899742301e37d78eb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 17688,
"upload_time": "2024-07-20T12:52:39",
"upload_time_iso_8601": "2024-07-20T12:52:39.886531Z",
"url": "https://files.pythonhosted.org/packages/e6/79/903ebf88645e9fa895e59f4631dd0d21ff90cd3a043ed7fa266e73a500c8/schwab_api-0.4.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-20 12:52:39",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "itsjafer",
"github_project": "schwab-api",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "schwab-api"
}