Skip to content

Commit

Permalink
add README.md and publish to PyPI
Browse files Browse the repository at this point in the history
  • Loading branch information
hzheng committed May 22, 2024
1 parent 61e9579 commit ce067fd
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 4 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Publish to PyPI

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "::add-path::$HOME/.local/bin"
- name: Install dependencies
run: poetry install

- name: Build the package
run: poetry build

- name: Publish to PyPI
env:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
run: poetry publish --username __token__ --password $PYPI_TOKEN --no-interaction

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__pycache__/
__pycache__
dist

.env
tokens.json
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Hui Zheng

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.
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

# Schwab API Python Library

[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](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](https://beta-developer.schwab.com/).
3. Create an individual developer app and wait until its status is "Ready for use".

## Installation

Install the library using pip:

```bash
pip install pyschwab
```

## Preparation

1. Change directory to your project directory
2. Copy [pyschwab.yaml](https://github.com/hzheng/pyschwab/blob/main/config/pyschwab.yaml) to your project's `config` directory.
3. Copy [env_example](https://github.com/hzheng/pyschwab/blob/main/env_example) to `.env` and replace the placeholders with your settings.

## Usage

Here are some sample usages:

```python
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.
```
2 changes: 1 addition & 1 deletion config/pyschwab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ auth:
callback_url: $CALLBACK_URL

token:
token_path: config/tokens.json
token_path: tokens.json
refresh_token_expires_in_sec: 604800 # 7 days
access_token_expires_in_sec: 1800

Expand Down
4 changes: 4 additions & 0 deletions env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
APP_KEY=replace_with_your_app_key
APP_SECRET=replace_with_your_app_secret
CALLBACK_URL=https://127.0.0.1

20 changes: 18 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
[tool.poetry]
name = "pyschwab"
version = "0.0.1"
version = "0.0.1a2"
description = "A Python library for the Schwab trading API"
authors = ["Hui Zheng <[email protected]>"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/hzheng/pyschwab"
documentation = "https://pyschwab.readthedocs.io"
keywords = ["finance", "trading", "schwab", "api", "python"]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.11",
"Topic :: Software Development :: Libraries",
"Topic :: Office/Business :: Financial",
]

[tool.poetry.dependencies]
python = "^3.11"
pytest = "^8.2.0"
pyyaml = "^6.0.1"
python-dotenv = "^1.0.1"
requests = "^2.31.0"
pydantic = "^2.7.1"
holidays = "^0.48"

[tool.poetry.dev-dependencies]
pytest = "^8.2.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.urls]
"Homepage" = "https://github.com/hzheng/pyschwab"

0 comments on commit ce067fd

Please sign in to comment.