# 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.
## 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:
```python
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:
```bash
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.
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hzheng/pyschwab",
"name": "pyschwab",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": "finance, trading, schwab, api, python",
"author": "Hui Zheng",
"author_email": "xyz.dll@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/04/17/1039ddd72225969c13412a98c221bdec4d39ea0c5a545b4a6b500357efa8/pyschwab-0.0.2.tar.gz",
"platform": null,
"description": "\n# Schwab API Python Library\n\n[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nA Python library for interacting with the Schwab API, providing simple access to trading and market data.\n\n## Prerequisites\n1. You need to have a Schwab brokerage account.\n2. Become an Individual Developer via the [Schwab Developer Portal](https://beta-developer.schwab.com/).\n3. Create an individual developer app and wait until its status is \"Ready for use\".\n\n## Installation\n\nInstall the library using pip:\n\n```bash\npip install pyschwab\n```\n\n## Preparation\n\n1. Change directory to your project directory\n2. Copy [pyschwab.yaml](https://github.com/hzheng/pyschwab/blob/main/config/pyschwab.yaml) to your project's `config` directory.\n3. Copy [env_example](https://github.com/hzheng/pyschwab/blob/main/env_example) to `.env` and replace the placeholders with your settings.\n\n## Warning\n**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.\n\n## Usage\n\nHere are some sample usages:\n\n```python\nimport yaml\n\nfrom pyschwab.auth import Authorizer\nfrom pyschwab.trading import TradingApi\nfrom pyschwab.types import Symbol\nfrom pyschwab.market import MarketApi\n\n# Load configuration\nwith open(\"config/pyschwab.yaml\", 'r') as file:\n app_config = yaml.safe_load(file)\n\n# Authorization\n# On the first run, this will open a browser for authorization.\n# Follow the instructions. Subsequent runs will auto-refresh the access token.\n# When refresh token expires, it will open browser for authorization again.\nauthorizer = Authorizer(app_config['auth'])\n\naccess_token = authorizer.get_access_token()\nprint(access_token)\n\n# Example usage of trading APIs\ntrading_api = TradingApi(access_token, app_config['trading'])\ntrading_data = trading_api.fetch_trading_data()\n# List positions\nfor position in trading_data.positions:\n print(\"position:\", position)\n\n# List transactions \nfor transaction in trading_api.get_transactions():\n print(\"transaction:\", transaction)\n\n# Buy/Sell equity\ntrading_api.buy_equity(\"TSLA\", quantity=10, price=100)\ntrading_api.sell_equity(\"TSLA\", quantity=10, price=200)\n\n# Buy/Sell option\nsymbol = Symbol(\"RDDT\", expiration=\"260116\", call_put=True, strike=50.00)\ntrading_api.buy_single_option(symbol, quantity=1, price=10)\n# Or: trading_api.buy_option(\"RDDT 260116C00050000\", quantity=1, price=10)\n\ntrading_api.sell_single_option(symbol, quantity=1, price=30)\n\n# Buy Call Spread\ntrading_api.trade_spread(\"TSLA\", 0.9, \"2024-05-31\", buy_sell=True, call_put=True, strikes=[177.5, 180], quantity=1)\n\n# List orders\nfor order in trading_api.get_orders():\n print(\"order:\", order)\n\n# Example usage of market APIs\nmarket_api = MarketApi(access_token, app_config['market'])\n\n# Get quotes\nsymbols = ['TSLA', 'NVDA']\nquotes = market_api.get_quotes(symbols)\nfor symbol in symbols:\n print(\"quote for \", symbol, \":\", quotes[symbol])\n\n# Get option chains\noption_chain = market_api.get_option_chains('TSLA')\nprint(option_chain)\n\n# Get price history \nhistory = market_api.get_price_history('TSLA')\nprint(history)\n```\n\n## Tests\n\nTo run the tests, use the following command:\n\n```bash\npytest -s\n```\n\nYou can customize the tests by editing the `test_api.json` file. Each test case has enabled/dry_run attribute that you can toggle. \n\n---\n\n## License\n\nMIT License\n\n```\nMIT License\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python library for the Schwab trading API",
"version": "0.0.2",
"project_urls": {
"Documentation": "https://pyschwab.readthedocs.io",
"Homepage": "https://github.com/hzheng/pyschwab",
"Repository": "https://github.com/hzheng/pyschwab"
},
"split_keywords": [
"finance",
" trading",
" schwab",
" api",
" python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3a02f5542062dc0c98399bb2a4ce89967ae44330ffc64ad2c1fea6b8994671f0",
"md5": "9a7f48bd3bae01eaabaccf2da723489f",
"sha256": "e2b38b18258a7801392a7030dc8a77e9cedec0df1183b5fc8184d25e06665835"
},
"downloads": -1,
"filename": "pyschwab-0.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9a7f48bd3bae01eaabaccf2da723489f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 24321,
"upload_time": "2024-05-27T03:12:56",
"upload_time_iso_8601": "2024-05-27T03:12:56.885618Z",
"url": "https://files.pythonhosted.org/packages/3a/02/f5542062dc0c98399bb2a4ce89967ae44330ffc64ad2c1fea6b8994671f0/pyschwab-0.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "04171039ddd72225969c13412a98c221bdec4d39ea0c5a545b4a6b500357efa8",
"md5": "ea987a249fbf299af8b9ef401235af5e",
"sha256": "a311e1f708a0b7dc499b63e807e16c75723c7103e3ea3b9c32ee7c1e584ab79b"
},
"downloads": -1,
"filename": "pyschwab-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "ea987a249fbf299af8b9ef401235af5e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 22185,
"upload_time": "2024-05-27T03:12:58",
"upload_time_iso_8601": "2024-05-27T03:12:58.369669Z",
"url": "https://files.pythonhosted.org/packages/04/17/1039ddd72225969c13412a98c221bdec4d39ea0c5a545b4a6b500357efa8/pyschwab-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-27 03:12:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hzheng",
"github_project": "pyschwab",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pyschwab"
}