Skip to main content

A Python library for the Schwab trading API

Project description

Schwab API Python Library

MIT License

A Python library for interacting with the Schwab API. This library provides a simple and convenient way to access Schwab's trading data and market data.

Installation

You can install the library using pip:

pip install pyschwab

Preparation

In the root directory, copy https://github.com/hzheng/pyschwab/blob/main/config/pyschwab.yaml to config directory. copy https://github.com/hzheng/pyschwab/blob/main/env_example to .env and replace variables with your settings.

Usage

Here's a basic example of how to use the library:

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

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

authorizer = Authorizer(app_config['auth'])
access_token = authorizer.get_access_token()

# Create trading api client 
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)

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

# 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)


# Create market api client 
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] )

option_chain = market_api.get_option_chains('TSLA')
print(option_chain)
 
history = market_api.get_price_history('TSLA')
print(history)

Features

  • Retrieve account information
  • Place trades
  • Check order status
  • Access market data
  • And more...

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


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.1a1.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

pyschwab-0.0.1a1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file pyschwab-0.0.1a1.tar.gz.

File metadata

  • Download URL: pyschwab-0.0.1a1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.7 Darwin/21.6.0

File hashes

Hashes for pyschwab-0.0.1a1.tar.gz
Algorithm Hash digest
SHA256 138fd6dc5083bfb483f598476ea011c56b1d1f563f962f8fcf67fb08081ed9d2
MD5 b7bb2052ad6742a99a65797bcf608892
BLAKE2b-256 c36c82e1b28b90b3538a9eabb3f632b4715e03c1d12166162a84f52f9ee46594

See more details on using hashes here.

File details

Details for the file pyschwab-0.0.1a1-py3-none-any.whl.

File metadata

  • Download URL: pyschwab-0.0.1a1-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.7 Darwin/21.6.0

File hashes

Hashes for pyschwab-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c2b0afad5112b9872f046a775d3aa9108b61cad6481c27821208086930d8116
MD5 af15b9725829a184b4579c852fba083b
BLAKE2b-256 b52e773627afb9d5fdcb9055c49448e1c547f7ff1c1a0ec46e777b2e65bc09f1

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