Skip to main content

A Python wrapper for Schwab API

Project description

py-schwab-wrapper

py-schwab-wrapper is a Python wrapper for Schwab's API, designed to handle authentication, token management, and data retrieval from Schwab's market data services. The initial version provides functionality to get price history for a symbol, with OAuth2-based authentication flow.

Features

  • OAuth2 authentication and token refresh.
  • Fetch historical price data for a symbol.
  • Extensible for other Schwab API functionalities.

Installation

You can install the py-schwab-wrapper package via pip:

pip install py-schwab-wrapper

Usage

Initial Setup

  1. Obtain OAuth Credentials: You need to get your client_id and client_secret from Schwab's developer portal.
  2. Set Environment Variables: You can store these credentials in a .env file in your project root.

Example .env file:

SCHWAB_CLIENT_ID=<your_client_id>
SCHWAB_CLIENT_SECRET=<your_client_secret>
  1. Authenticate and Generate token.json: The first time you use the API, you need to authenticate using Schwab’s OAuth2 flow to generate a token.json file. You can use the included authenticate.py to handle this process.

OAuth2 Authentication Flow

Before making API requests, you need to authenticate via Schwab's OAuth2 flow and store the access/refresh tokens in a token.json file. Here's how you can do it using the included authenticate.py script:

  1. Run authenticate.py to start the OAuth2 flow:
    python authenticate.py
    
  2. Follow the instructions in the browser to authorize the application and obtain the tokens.

A note on authentication

Be mindful if you configure your application's callback URL to be secure https://127.0.0.1:5000/callback, you'll need to set up HTTPS for your local Flask server. Here's how you can achieve this:

    openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes

Follow the prompts to provide the necessary information. This will create key.pem (private key) and cert.pem (certificate) files. And be sure to include them in your .gitignore file!

Additional .gitignore recommendations

# ...your boilerplate .gitignore file for python...

# Ignore .pem files
cert.pem
key.pem
.pem

# Ignore token.json file if you decide to do local storage of the token
token.json

Example API Usage

Once authenticated, you can use the wrapper to fetch price history data a version of this implementation is also available in examples/test_api.py:

from py_schwab_wrapper import SchwabAPI
from datetime import datetime, timedelta

# Initialize the API wrapper with your credentials
schwab_api = SchwabAPI(client_id="<your_client_id>", client_secret="<your_client_secret>")

# Define the time range
now = datetime.now()
start_of_day = now.replace(hour=9, minute=30, second=0, microsecond=0)
end_of_day = now.replace(hour=16, minute=0, second=0, microsecond=0)
startDate = int(start_of_day.timestamp() * 1000)
endDate = int(end_of_day.timestamp() * 1000)

# Fetch price history
price_history = schwab_api.get_price_history(
    symbol='QQQ', 
    periodType='day', 
    period=1, 
    frequencyType='minute', 
    frequency=5, 
    needExtendedHoursData=False, 
    needPreviousClose=True, 
    startDate=startDate, 
    endDate=endDate
)

print(price_history)

Contributing

Feel free to contribute by submitting issues or pull requests on the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

This project was developed with assistance from ChatGPT by OpenAI. Also special thanks to rderik for the motivation to pursue this project.

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

py_schwab_wrapper-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

py_schwab_wrapper-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file py_schwab_wrapper-0.1.0.tar.gz.

File metadata

  • Download URL: py_schwab_wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for py_schwab_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 23d1d0e62ecd1b59d1ba1742af7d33072c16e86ad999431055ec9ef6487b4d4e
MD5 445f2c33e3839f50e6e5fd44609c2a16
BLAKE2b-256 dd5bf0860d5a15cbcb2da2293874627d1b52ad3943e6d85c860ad313c2691e12

See more details on using hashes here.

File details

Details for the file py_schwab_wrapper-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_schwab_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1de92d001f3db8d7d9dcfbd493ce7097a9152f2a6ddcc6d589f47bcb9ee0f234
MD5 ab7a36438dd95c7fcecada5811973909
BLAKE2b-256 f7417f850fc7d3a8ee0c7ec1c6c401caa014db84b2cf238f441c4c0bb68c494a

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