Skip to main content

Schwab API Python Library

MIT License

A Python library for interacting with the Schwab API, providing simple access to trading and market data.

Prerequisites

  1. You need to have a Schwab brokerage account.
  2. Become an Individual Developer via the Schwab Developer Portal.
  3. Create an individual developer app and wait until its status is "Ready for use".

Installation

Install the library using pip:

pip install pyschwab

Preparation

  1. Change directory to your project directory
  2. Copy pyschwab.yaml to your project's config directory.
  3. Copy env_example to .env and replace the placeholders with your settings.

Warning

WARNING: This library can place real orders on your Schwab brokerage account. Use this API at your own risk. The author and contributors are not responsible for any financial losses or unintended trades that may occur as a result of using this library. Ensure you fully understand the functionality and risks before using it in a live trading environment.

Usage

Here are some sample usages:

import yaml

from pyschwab.auth import Authorizer
from pyschwab.trading import TradingApi
from pyschwab.types import Symbol
from pyschwab.market import MarketApi

# Load configuration
with open("config/pyschwab.yaml", 'r') as file:
    app_config = yaml.safe_load(file)

# Authorization
# On the first run, this will open a browser for authorization.
# Follow the instructions. Subsequent runs will auto-refresh the access token.
# When refresh token expires, it will open browser for authorization again.
authorizer = Authorizer(app_config['auth'])

access_token = authorizer.get_access_token()
print(access_token)

# Example usage of trading APIs
trading_api = TradingApi(access_token, app_config['trading'])
trading_data = trading_api.fetch_trading_data()
# List positions
for position in trading_data.positions:
    print("position:", position)

# List transactions 
for transaction in trading_api.get_transactions():
    print("transaction:", transaction)

# Buy/Sell equity
trading_api.buy_equity("TSLA", quantity=10, price=100)
trading_api.sell_equity("TSLA", quantity=10, price=200)

# Buy/Sell option
symbol = Symbol("RDDT", expiration="260116", call_put=True, strike=50.00)
trading_api.buy_single_option(symbol, quantity=1, price=10)
# Or: trading_api.buy_option("RDDT  260116C00050000", quantity=1, price=10)

trading_api.sell_single_option(symbol, quantity=1, price=30)

# Buy Call Spread
trading_api.trade_spread("TSLA", 0.9,  "2024-05-31", buy_sell=True, call_put=True, strikes=[177.5, 180], quantity=1)

# List orders
for order in trading_api.get_orders():
    print("order:", order)

# Example usage of market APIs
market_api = MarketApi(access_token, app_config['market'])

# Get quotes
symbols = ['TSLA', 'NVDA']
quotes = market_api.get_quotes(symbols)
for symbol in symbols:
    print("quote for ", symbol, ":", quotes[symbol])

# Get option chains
option_chain = market_api.get_option_chains('TSLA')
print(option_chain)

# Get price history 
history = market_api.get_price_history('TSLA')
print(history)

Tests

To run the tests, use the following command:

pytest -s

You can customize the tests by editing the test_api.json file. Each test case has enabled/dry_run attribute that you can toggle.


License

MIT License

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Metadata

Release files for pyschwab 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyschwab 0.0.3
File Size Uploaded
pyschwab-0.0.3.tar.gz 22.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyschwab 0.0.3
File Interpreter ABI Platform
pyschwab-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 46.7 kB

Release files / pyschwab-0.0.3.tar.gz

Download URL pyschwab-0.0.3.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5d32f0922aa45d55719d388de6d34895bbb7816f392e73c817904923533e7eed
BLAKE2b-256 checksum
How to use checksums
26f4b5b517ffdd41a025e06ff5c862c0b6d5d7b0979dbedb8b914fa7b3ac8dde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1021-azure

Release files / pyschwab-0.0.3-py3-none-any.whl

Download URL pyschwab-0.0.3-py3-none-any.whl
Size 24.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c3563751e8d6e49532692b595f41e9d714b26564c6662eb2913e141e5709f83
BLAKE2b-256 checksum
How to use checksums
3c1d72c661ad9f0ff26e40929780c28f13846425ee1d80606c2f9469581a1275
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1021-azure
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page