A Python library for the Schwab trading API
Project description
Schwab API Python Library
A Python library for interacting with the Schwab API, providing simple access to trading and market data.
Prerequisites
- You need to have a Schwab brokerage account.
- Become an Individual Developer via the Schwab Developer Portal.
- Create an individual developer app and wait until its status is "Ready for use".
Installation
Install the library using pip:
pip install pyschwab
Preparation
- Change directory to your project directory
- Copy pyschwab.yaml to your project's
config
directory. - Copy env_example to
.env
and replace the placeholders with your settings.
Usage
Here are some sample usages:
import yaml
from pyschwab.auth import Authorizer
from pyschwab.trading import TradingApi
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'])
account_num = 0 # CHANGE this to your actual account number
trading_data = trading_api.fetch_trading_data(account_num)
# List positions
for position in trading_data.positions:
print("position:", position)
# List transactions
for transaction in trading_api.get_transactions(account_num):
print("transaction:", transaction)
# Place order
order_dict = {
"orderType": "LIMIT", "session": "NORMAL", "duration": "DAY", "orderStrategyType": "SINGLE", "price": '100.00',
"orderLegCollection": [
{"instruction": "BUY", "quantity": 1, "instrument": {"symbol": "TSLA", "assetType": "EQUITY"}}
]
}
trading_api.place_order(order_dict, account_num)
# List orders
for order in trading_api.get_orders(account_num):
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)
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyschwab-0.0.1a3.tar.gz
(18.3 kB
view details)
Built Distribution
File details
Details for the file pyschwab-0.0.1a3.tar.gz
.
File metadata
- Download URL: pyschwab-0.0.1a3.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1021-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd4a20974fc89341b2a2c5a8f3965d5cd8415806acf7d84ed2931dbb1b390545 |
|
MD5 | 9e767baf9dc303939b1c1ff9ae5e5448 |
|
BLAKE2b-256 | 65bb9a9d2eac530c63f77de9abdad9cb37a9e5d11a3225f05bd7429a8d1ee533 |
File details
Details for the file pyschwab-0.0.1a3-py3-none-any.whl
.
File metadata
- Download URL: pyschwab-0.0.1a3-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1021-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8747e9442b91208496392f9e2112039854d42c41d6fa42388f730b170a9c7415 |
|
MD5 | 415e871cdbdac493806964ae160397c3 |
|
BLAKE2b-256 | ffd7b4713ab34bac72fd13b399fb5bfe59e072a2a925cecaae0f6c37ea2cf344 |