schwab-python-api


Nameschwab-python-api JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/dhonn/schwab-python-api
SummaryA python client lirbary for the Schwab API.
upload_time2024-04-09 15:15:20
maintainerNone
docs_urlNone
authorDhonn Lushine
requires_python>=3.7
licenseNone
keywords finance schwab api
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # UPDATE

Currently in development, this project is a fork of the TD Ameritrade API, adapted for compatibility with the Schwab API.
Supports trading, historical and real-time data streaming.
Documentation is slowly being updated.

## Unofficial Charles Schwab API Library

## Table of Contents

- [Overview](#overview)
- [What's in the API](#whats-in-the-api)
- [Requirements](#requirements)
- [API Key & Credentials](#api-key-and-credentials)
- [Installation](#installation)
- [Usage](#usage)
- [Features](#features)
- [Documentation & Resources](#documentation-and-resources)
- [Support These Projects](#support-these-projects)
- [Authentication Workflow](#authentication-workflow)

## Overview

Current Version: **0.0.2**

The unofficial Python API client library for Charles Schwab allows individuals with
Charles Schwab accounts to manage trades, pull historical and real-time data, manage
their accounts, create and modify orders all using the Python programming language.

To learn more about the Charles Schwab API, please refer to
the [official documentation](https://developer.tdameritrade.com/apis).

## What's in the API

- Authentication - access tokens, refresh tokens, request authentication.
- Accounts & Trading
- Market Hours
- Instruments
- Movers
- Option Chains
- Price History
- Quotes
- Transaction History
- User Info & Preferences
- Watchlist

## Requirements

The following requirements must be met to use this API:

- A Charles Schwab account, you'll need your account password and account number to use the API.
- A Charles Schwab Developer Account
- A Charles Schwab Developer API Key
- A Consumer ID
- A Redirect URI, sometimes called Redirect URL
- Python 3.7 or later.

## API Key and Credentials

Each Charles Schwab API request requires a Charles Schwab Developer API Key, a consumer ID,
an account password, an account number, and a redirect URI. API Keys, consumer IDs, and
redirect URIs are generated from the Charles Schwab developer portal. To set up and create
your Charles Schwab developer account, please refer to
the [official documentation](https://developer.schwab.com/user-guides).

Additionally, to authenticate yourself using this library, you will need to provide your
account number and password for your main Charles Schwab account.

**Important:** Your account number, an account password, consumer ID, and API key should
be kept secret.

## Installation

The project can be found at PyPI, if you'd like to view the project please use
this [link](https://pypi.org/project/schwab-python-api/.

```bash
pip install schwab-python-api
```

To upgrade the library run the following command:

```bash
pip install --upgrade schwab-python-api
```

## Usage

This example demonstrates how to login to the API and demonstrates sending a request
using the `get_quotes` endpoint, using your API key.

**Credentials:**
Please note, that the `credentials_path` is a file path that will house the credentials
like your refresh token and access token. You must specify the `credentials_path` argument
yourself so that you are aware of where the tokens will be stored. For example, if you
specify the `credentials_path` as `C:\Users\Public\Credentials\td_state.json` it would
store your tokens in a JSON file located in a folder called Credentials located under
the Users profile.

```python
# Import the client
from td.client import TDClient

# Create a new session, credentials path is required.
TDSession = TDClient(
    app_key='<APP_KEY>',
    app_secret='<APP_SECRET>',
    redirect_uri='<REDIRECT_URI>',
    credentials_path='<PATH_TO_CREDENTIALS_FILE>'
)

# Login to the session
TDSession.login()

# Grab real-time quotes for 'MSFT' (Microsoft)
msft_quotes = TDSession.get_quotes(instruments=['MSFT'])

# Grab real-time quotes for 'AMZN' (Amazon) and 'SQ' (Square)
multiple_quotes = TDSession.get_quotes(instruments=['AMZN','SQ'])

# Create a streaming sesion
TDStreamingClient = TDSession.create_streaming_session()

# Live stream quotes
TDStreamingClient.level_one_quotes(
    symbols=['SPY'],
    fields=[0,1,2,3,4,5,6,8,9,10,11,12,16,17,18,19,20,33,35,39,40,41,42]
)
```

## Features

### Authentication Workflow Support

Automatically will handle the authentication workflow for new users, returning users, and users
with expired tokens (refresh token or access token).

### Request Validation

For certain requests, in a limited fashion, it will help validate your request when possible.
For example, when using the `get_movers` endpoint, it will automatically validate that the
market you're requesting data from is one of the valid options.

### Customized Objects for Watchlists, Orders, and Option Chains

Requests for saved orders, regular orders, watchlists, and option chains can be a challenging
process that has multiple opportunities to make mistakes. This library has built-in objects
that will allow you to quickly build your request and then validate certain portions of your
request when possible.

### Library Requirements

The following requirements must be met before being able to use the Charles Schwab Python API library.

- You must have a Charles Schwab Account.
- You must have a Charles Schwab Developer Account. Please go to following [folder](https://github.com/dhonn/schwab-python-api/tree/master/samples/resources)
  for instructions on how to create a Developer account.

## Documentation and Resources

### Official API Documentation

- [Getting Started](https://developer.schwab.com/products/trader-api--individual/details/documentation/Retail%20Trader%20API%20Production)
- [Endpoints](https://developer.schwab.com/products/trader-api--individual/details/specifications/Retail%20Trader%20API%20Production)
- [Guides](https://developer.schwab.com/user-guides)
- [Samples - Price History](https://developer.tdameritrade.com/content/price-history-samples)
- [Samples - Place Order](https://developer.tdameritrade.com/content/place-order-samples)

### Unofficial Documentation

- [Charles Schwab API - YouTube](https://www.youtube.com/playlist?list=PLcFcktZ0wnNnKvxFkJ5B7pvGaGa81Ny-6)

## Support these Projects

**Patreon:**
Help support this project and future projects by donating to my [Patreon Page](https://www.patreon.com/sigmacoding).
I'm always looking to add more content for individuals like yourself, unfortuantely some of the APIs I would require
me to pay monthly fees.

**YouTube:**
If you'd like to watch more of my content, feel free to visit my YouTube channel [Sigma Coding](https://www.youtube.com/c/SigmaCoding).

**Hire Me:**
If you have a project, you think I can help you with feel free to reach out at [coding.sigma@gmail.com](mailto:coding.sigma@gmail.com?subject=[GitHub]%20Project%20Proposal) or fill out the [contract request form](https://forms.office.com/Pages/ResponsePage.aspx?id=ZwOBErInsUGliXx0Yo2VfcCSWZSwW25Es3vPV2veU0pUMUs5MUc2STkzSzVQMFNDVlI5NjJVNjREUi4u)

## Authentication Workflow

**Step 1 - Start the Script:**

While in Visual Studio Code, right click anywhere in the code editor while in the file that contains your code.
The following dropdown will appear:

![Terminal Dropdown](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/terminal_dropdown.jpg "Terminal Dropdown")

From the dropdown, click `Run Python file in Terminal`, this will start the python script.

**Step 2 - Go to Redirect URL:**

The TD Library will automatically generate the redirect URL that will navigate you to the TD website for for
you authentication. You can either copy the link and paste it into a browser manually or if you're using Visual
Studio Code you can press `CTRL + Click` to have Visual Studio Code navigate you to the URL immeditately.

![Redirect URI](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/redirect_uri.jpg "Redirect URI")

**Step 3 - Login to the TD API:**

Once you've arrived at the login screen, you'll need to provide your credentials to authenticate the session.
Please provide your Account Username and Account Password in the userform and then press enter. As a reminder
these, are the same username/password combination you use to login to your regular TD Account.

!["TD Login](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/td_login.jpg "TD Login")

**Step 4 - Accept the Terms:**

Accept the Terms of the API by clicking `Allow`, this will redirect you.

![TD Terms](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/td_terms.jpg "TD Terms")

**Step 5 - Copy the Authorization Code:**

After accepting the terms, you'll be taken to the URL that you provided as your `redirect URI`. However, at
the end of that URL will be `authorization code`. To complete the authentication workflow, copy the URL as
it appears below. Don't worry if the numbers don't match, as you will have a different code.

![Auth Code](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/auth_code.jpg "Auth Code")

**Step 6 - Paste the Authorization Code in the Terminal:**

Take the URL and copy it into the Terminal, after you have pasted it, press `Enter`. The authentication workflow
will complete and the script will start running. At this stage, we are exchanging your authorization code for
an access token. That access token is valid only for 30 minutes. However, a refresh token is also stored that
will refresh your access token when it expires.

![Paste URL](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/paste_url.jpg "Paste URL")

After, that the script should run. Additionally, if you go to the location you specified in the `credentials_path`
arugment you will now see `td_state.json` file. This file contains all the info used during a session. Please
DO NOT DELETE THIS FILE OR ELSE YOU WILL NEED TO GO THROUGH THE STEPS ABOVE.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dhonn/schwab-python-api",
    "name": "schwab-python-api",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "finance, schwab, api",
    "author": "Dhonn Lushine",
    "author_email": "dhonn@yahoo.com",
    "download_url": "https://files.pythonhosted.org/packages/f7/99/5e8faa79e9e4776b5f06b6073d77ef6fe82db9128fc0810d56fd2af47e8a/schwab-python-api-0.0.2.tar.gz",
    "platform": null,
    "description": "# UPDATE\r\n\r\nCurrently in development, this project is a fork of the TD Ameritrade API, adapted for compatibility with the Schwab API.\r\nSupports trading, historical and real-time data streaming.\r\nDocumentation is slowly being updated.\r\n\r\n## Unofficial Charles Schwab API Library\r\n\r\n## Table of Contents\r\n\r\n- [Overview](#overview)\r\n- [What's in the API](#whats-in-the-api)\r\n- [Requirements](#requirements)\r\n- [API Key & Credentials](#api-key-and-credentials)\r\n- [Installation](#installation)\r\n- [Usage](#usage)\r\n- [Features](#features)\r\n- [Documentation & Resources](#documentation-and-resources)\r\n- [Support These Projects](#support-these-projects)\r\n- [Authentication Workflow](#authentication-workflow)\r\n\r\n## Overview\r\n\r\nCurrent Version: **0.0.2**\r\n\r\nThe unofficial Python API client library for Charles Schwab allows individuals with\r\nCharles Schwab accounts to manage trades, pull historical and real-time data, manage\r\ntheir accounts, create and modify orders all using the Python programming language.\r\n\r\nTo learn more about the Charles Schwab API, please refer to\r\nthe [official documentation](https://developer.tdameritrade.com/apis).\r\n\r\n## What's in the API\r\n\r\n- Authentication - access tokens, refresh tokens, request authentication.\r\n- Accounts & Trading\r\n- Market Hours\r\n- Instruments\r\n- Movers\r\n- Option Chains\r\n- Price History\r\n- Quotes\r\n- Transaction History\r\n- User Info & Preferences\r\n- Watchlist\r\n\r\n## Requirements\r\n\r\nThe following requirements must be met to use this API:\r\n\r\n- A Charles Schwab account, you'll need your account password and account number to use the API.\r\n- A Charles Schwab Developer Account\r\n- A Charles Schwab Developer API Key\r\n- A Consumer ID\r\n- A Redirect URI, sometimes called Redirect URL\r\n- Python 3.7 or later.\r\n\r\n## API Key and Credentials\r\n\r\nEach Charles Schwab API request requires a Charles Schwab Developer API Key, a consumer ID,\r\nan account password, an account number, and a redirect URI. API Keys, consumer IDs, and\r\nredirect URIs are generated from the Charles Schwab developer portal. To set up and create\r\nyour Charles Schwab developer account, please refer to\r\nthe [official documentation](https://developer.schwab.com/user-guides).\r\n\r\nAdditionally, to authenticate yourself using this library, you will need to provide your\r\naccount number and password for your main Charles Schwab account.\r\n\r\n**Important:** Your account number, an account password, consumer ID, and API key should\r\nbe kept secret.\r\n\r\n## Installation\r\n\r\nThe project can be found at PyPI, if you'd like to view the project please use\r\nthis [link](https://pypi.org/project/schwab-python-api/.\r\n\r\n```bash\r\npip install schwab-python-api\r\n```\r\n\r\nTo upgrade the library run the following command:\r\n\r\n```bash\r\npip install --upgrade schwab-python-api\r\n```\r\n\r\n## Usage\r\n\r\nThis example demonstrates how to login to the API and demonstrates sending a request\r\nusing the `get_quotes` endpoint, using your API key.\r\n\r\n**Credentials:**\r\nPlease note, that the `credentials_path` is a file path that will house the credentials\r\nlike your refresh token and access token. You must specify the `credentials_path` argument\r\nyourself so that you are aware of where the tokens will be stored. For example, if you\r\nspecify the `credentials_path` as `C:\\Users\\Public\\Credentials\\td_state.json` it would\r\nstore your tokens in a JSON file located in a folder called Credentials located under\r\nthe Users profile.\r\n\r\n```python\r\n# Import the client\r\nfrom td.client import TDClient\r\n\r\n# Create a new session, credentials path is required.\r\nTDSession = TDClient(\r\n    app_key='<APP_KEY>',\r\n    app_secret='<APP_SECRET>',\r\n    redirect_uri='<REDIRECT_URI>',\r\n    credentials_path='<PATH_TO_CREDENTIALS_FILE>'\r\n)\r\n\r\n# Login to the session\r\nTDSession.login()\r\n\r\n# Grab real-time quotes for 'MSFT' (Microsoft)\r\nmsft_quotes = TDSession.get_quotes(instruments=['MSFT'])\r\n\r\n# Grab real-time quotes for 'AMZN' (Amazon) and 'SQ' (Square)\r\nmultiple_quotes = TDSession.get_quotes(instruments=['AMZN','SQ'])\r\n\r\n# Create a streaming sesion\r\nTDStreamingClient = TDSession.create_streaming_session()\r\n\r\n# Live stream quotes\r\nTDStreamingClient.level_one_quotes(\r\n    symbols=['SPY'],\r\n    fields=[0,1,2,3,4,5,6,8,9,10,11,12,16,17,18,19,20,33,35,39,40,41,42]\r\n)\r\n```\r\n\r\n## Features\r\n\r\n### Authentication Workflow Support\r\n\r\nAutomatically will handle the authentication workflow for new users, returning users, and users\r\nwith expired tokens (refresh token or access token).\r\n\r\n### Request Validation\r\n\r\nFor certain requests, in a limited fashion, it will help validate your request when possible.\r\nFor example, when using the `get_movers` endpoint, it will automatically validate that the\r\nmarket you're requesting data from is one of the valid options.\r\n\r\n### Customized Objects for Watchlists, Orders, and Option Chains\r\n\r\nRequests for saved orders, regular orders, watchlists, and option chains can be a challenging\r\nprocess that has multiple opportunities to make mistakes. This library has built-in objects\r\nthat will allow you to quickly build your request and then validate certain portions of your\r\nrequest when possible.\r\n\r\n### Library Requirements\r\n\r\nThe following requirements must be met before being able to use the Charles Schwab Python API library.\r\n\r\n- You must have a Charles Schwab Account.\r\n- You must have a Charles Schwab Developer Account. Please go to following [folder](https://github.com/dhonn/schwab-python-api/tree/master/samples/resources)\r\n  for instructions on how to create a Developer account.\r\n\r\n## Documentation and Resources\r\n\r\n### Official API Documentation\r\n\r\n- [Getting Started](https://developer.schwab.com/products/trader-api--individual/details/documentation/Retail%20Trader%20API%20Production)\r\n- [Endpoints](https://developer.schwab.com/products/trader-api--individual/details/specifications/Retail%20Trader%20API%20Production)\r\n- [Guides](https://developer.schwab.com/user-guides)\r\n- [Samples - Price History](https://developer.tdameritrade.com/content/price-history-samples)\r\n- [Samples - Place Order](https://developer.tdameritrade.com/content/place-order-samples)\r\n\r\n### Unofficial Documentation\r\n\r\n- [Charles Schwab API - YouTube](https://www.youtube.com/playlist?list=PLcFcktZ0wnNnKvxFkJ5B7pvGaGa81Ny-6)\r\n\r\n## Support these Projects\r\n\r\n**Patreon:**\r\nHelp support this project and future projects by donating to my [Patreon Page](https://www.patreon.com/sigmacoding).\r\nI'm always looking to add more content for individuals like yourself, unfortuantely some of the APIs I would require\r\nme to pay monthly fees.\r\n\r\n**YouTube:**\r\nIf you'd like to watch more of my content, feel free to visit my YouTube channel [Sigma Coding](https://www.youtube.com/c/SigmaCoding).\r\n\r\n**Hire Me:**\r\nIf you have a project, you think I can help you with feel free to reach out at [coding.sigma@gmail.com](mailto:coding.sigma@gmail.com?subject=[GitHub]%20Project%20Proposal) or fill out the [contract request form](https://forms.office.com/Pages/ResponsePage.aspx?id=ZwOBErInsUGliXx0Yo2VfcCSWZSwW25Es3vPV2veU0pUMUs5MUc2STkzSzVQMFNDVlI5NjJVNjREUi4u)\r\n\r\n## Authentication Workflow\r\n\r\n**Step 1 - Start the Script:**\r\n\r\nWhile in Visual Studio Code, right click anywhere in the code editor while in the file that contains your code.\r\nThe following dropdown will appear:\r\n\r\n![Terminal Dropdown](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/terminal_dropdown.jpg \"Terminal Dropdown\")\r\n\r\nFrom the dropdown, click `Run Python file in Terminal`, this will start the python script.\r\n\r\n**Step 2 - Go to Redirect URL:**\r\n\r\nThe TD Library will automatically generate the redirect URL that will navigate you to the TD website for for\r\nyou authentication. You can either copy the link and paste it into a browser manually or if you're using Visual\r\nStudio Code you can press `CTRL + Click` to have Visual Studio Code navigate you to the URL immeditately.\r\n\r\n![Redirect URI](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/redirect_uri.jpg \"Redirect URI\")\r\n\r\n**Step 3 - Login to the TD API:**\r\n\r\nOnce you've arrived at the login screen, you'll need to provide your credentials to authenticate the session.\r\nPlease provide your Account Username and Account Password in the userform and then press enter. As a reminder\r\nthese, are the same username/password combination you use to login to your regular TD Account.\r\n\r\n![\"TD Login](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/td_login.jpg \"TD Login\")\r\n\r\n**Step 4 - Accept the Terms:**\r\n\r\nAccept the Terms of the API by clicking `Allow`, this will redirect you.\r\n\r\n![TD Terms](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/td_terms.jpg \"TD Terms\")\r\n\r\n**Step 5 - Copy the Authorization Code:**\r\n\r\nAfter accepting the terms, you'll be taken to the URL that you provided as your `redirect URI`. However, at\r\nthe end of that URL will be `authorization code`. To complete the authentication workflow, copy the URL as\r\nit appears below. Don't worry if the numbers don't match, as you will have a different code.\r\n\r\n![Auth Code](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/auth_code.jpg \"Auth Code\")\r\n\r\n**Step 6 - Paste the Authorization Code in the Terminal:**\r\n\r\nTake the URL and copy it into the Terminal, after you have pasted it, press `Enter`. The authentication workflow\r\nwill complete and the script will start running. At this stage, we are exchanging your authorization code for\r\nan access token. That access token is valid only for 30 minutes. However, a refresh token is also stored that\r\nwill refresh your access token when it expires.\r\n\r\n![Paste URL](https://raw.githubusercontent.com/dhonn/schwab-python-api/master/samples/instructions/photos/paste_url.jpg \"Paste URL\")\r\n\r\nAfter, that the script should run. Additionally, if you go to the location you specified in the `credentials_path`\r\narugment you will now see `td_state.json` file. This file contains all the info used during a session. Please\r\nDO NOT DELETE THIS FILE OR ELSE YOU WILL NEED TO GO THROUGH THE STEPS ABOVE.\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A python client lirbary for the Schwab API.",
    "version": "0.0.2",
    "project_urls": {
        "Homepage": "https://github.com/dhonn/schwab-python-api"
    },
    "split_keywords": [
        "finance",
        " schwab",
        " api"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "798e059eaa53f6b074028aeb0b4060da9773f3c4cf1951c98b019779241d9fc3",
                "md5": "f5d1e686c69fcc658d860a0ff6388036",
                "sha256": "06b2c758908d8f4905ee1d71249def7f461db5fe379edb575bf0a81a439d96b1"
            },
            "downloads": -1,
            "filename": "schwab_python_api-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f5d1e686c69fcc658d860a0ff6388036",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 53698,
            "upload_time": "2024-04-09T15:15:17",
            "upload_time_iso_8601": "2024-04-09T15:15:17.849466Z",
            "url": "https://files.pythonhosted.org/packages/79/8e/059eaa53f6b074028aeb0b4060da9773f3c4cf1951c98b019779241d9fc3/schwab_python_api-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f7995e8faa79e9e4776b5f06b6073d77ef6fe82db9128fc0810d56fd2af47e8a",
                "md5": "fcb4e5e670d2a786d7169ec03f7b3589",
                "sha256": "be22f8b744b3aebe771f44075937d852edb97aa95f1a70c8f4262a2164abe53a"
            },
            "downloads": -1,
            "filename": "schwab-python-api-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "fcb4e5e670d2a786d7169ec03f7b3589",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 53347,
            "upload_time": "2024-04-09T15:15:20",
            "upload_time_iso_8601": "2024-04-09T15:15:20.086144Z",
            "url": "https://files.pythonhosted.org/packages/f7/99/5e8faa79e9e4776b5f06b6073d77ef6fe82db9128fc0810d56fd2af47e8a/schwab-python-api-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-09 15:15:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dhonn",
    "github_project": "schwab-python-api",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "schwab-python-api"
}
        
Elapsed time: 0.23263s