schwab-api


Nameschwab-api JSON
Version 0.4.0 PyPI version JSON
download
home_pagehttps://github.com/itsjafer/schwab-api
SummaryUnofficial Schwab API wrapper in Python 3.
upload_time2024-03-14 17:54:16
maintainer
docs_urlNone
authorJafer Haider
requires_python
licenseMIT
keywords schwab python3 api unofficial schwab-api schwab charles api
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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
* 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
* 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](https://itsjafer.com/#/reversesplit).

![Screenshot](screenshot.png)

## 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](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": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "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/42/b8/df4bb7e10522992fb41b431582fbd8b6747be595e16813ba62d87bfb0da1/schwab_api-0.4.0.tar.gz",
    "platform": null,
    "description": "# Schwab API\n\n**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.**\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* 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* Web Request implementation (with the exception of authentication)\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## Contribution\n\nI would absolutely love contributions; as someone new to open source, I'd appreciate help in setting up a reliable system for PRs as well :)\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.0",
    "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": "14597597fd0e248d24746db428ff540b16656af07a665a620ff28e1e510b987a",
                "md5": "75b69a043db09894bd43463667118ace",
                "sha256": "fe0d456be00b66e15127a29f3aca42520c60381f476060d9f10661f07947bc2d"
            },
            "downloads": -1,
            "filename": "schwab_api-0.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "75b69a043db09894bd43463667118ace",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 16069,
            "upload_time": "2024-03-14T17:54:13",
            "upload_time_iso_8601": "2024-03-14T17:54:13.767205Z",
            "url": "https://files.pythonhosted.org/packages/14/59/7597fd0e248d24746db428ff540b16656af07a665a620ff28e1e510b987a/schwab_api-0.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "42b8df4bb7e10522992fb41b431582fbd8b6747be595e16813ba62d87bfb0da1",
                "md5": "ce65aa1f0afe2c0626d995a3a7c121e7",
                "sha256": "7e3a4ce94feb8f0839098dd9aa9190866f8e53139a3ffd52dae9b82fbe81999b"
            },
            "downloads": -1,
            "filename": "schwab_api-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ce65aa1f0afe2c0626d995a3a7c121e7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 17082,
            "upload_time": "2024-03-14T17:54:16",
            "upload_time_iso_8601": "2024-03-14T17:54:16.708441Z",
            "url": "https://files.pythonhosted.org/packages/42/b8/df4bb7e10522992fb41b431582fbd8b6747be595e16813ba62d87bfb0da1/schwab_api-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-14 17:54:16",
    "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"
}
        
Elapsed time: 0.22496s