# py-schwab-wrapper
`py-schwab-wrapper` is a Python wrapper for Schwab's API, designed to handle authentication, token management, and data retrieval from Schwab's market data services. The initial version provides functionality to get price history for a symbol, with OAuth2-based authentication flow.
## Features
Be sure to check out our CHANGELOG for full details on functionality
- OAuth2 authentication and token refresh.
- Fetch historical price data for a symbol.
- Get account information
- Get orders for a specific account
- Place orders with user friendly abstractions.
## Installation
You can install the `py-schwab-wrapper` package via `pip`:
```bash
pip install py-schwab-wrapper
```
## Usage
### Initial Setup
1. **Obtain OAuth Credentials**: You need to get your `client_id` and `client_secret` from Schwab's developer portal.
2. **Set Environment Variables**: You can store these credentials in a `.env` file in your project root.
Example `.env` file:
```bash
SCHWAB_CLIENT_ID=<your_client_id>
SCHWAB_CLIENT_SECRET=<your_client_secret>
```
3. **Authenticate and Generate `token.json`**:
The first time you use the API, you need to authenticate using Schwab’s OAuth2 flow to generate a `token.json` file. You can use the included `authenticate.py` to handle this process.
## OAuth2 Authentication Flow
Before making API requests, you need to authenticate via Schwab's OAuth2 flow and store the access/refresh tokens in a `token.json` file. Here's how you can do it using the included `authenticate.py` script:
1. Run `authenticate.py` to start the OAuth2 flow:
```bash
python authenticate.py
```
2. Follow the instructions in the browser to authorize the application and obtain the tokens.
### A note on authentication
Be mindful if you configure your application's callback URL to be secure `https://127.0.0.1:5000/callback`, you'll need to set up HTTPS for your local Flask server. Here's how you can achieve this:
```
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
```
Follow the prompts to provide the necessary information. This will create key.pem (private key) and cert.pem (certificate) files. And be sure to include them in your .gitignore file!
#### Additional .gitignore recommendations
```
# ...your boilerplate .gitignore file for python...
# Ignore .pem files
cert.pem
key.pem
.pem
# Ignore token.json file if you decide to do local storage of the token
token.json
```
## Example API Usage
Once authenticated, you can use the wrapper to get historical market data, get account information, or place an order. Be sure to check out our examples/ folder.
```python
from py_schwab_wrapper.schwab_api import SchwabAPI
from datetime import datetime, timedelta
# Initialize the API wrapper with your credentials
schwab_api = SchwabAPI(client_id="<your_client_id>", client_secret="<your_client_secret>")
# Define the time range
now = datetime.now()
start_of_day = now.replace(hour=9, minute=30, second=0, microsecond=0)
end_of_day = now.replace(hour=16, minute=0, second=0, microsecond=0)
start_date = int(start_of_day.timestamp() * 1000)
end_date = int(end_of_day.timestamp() * 1000)
# Fetch price history
price_history = schwab_api.get_price_history(
symbol='QQQ',
period_type='day',
period=1,
frequency_type='minute',
frequency=5,
need_extended_hours_data=False,
need_previous_close=True,
start_date=start_date,
end_date=end_date
)
print(price_history)
```
## Contributing
Feel free to contribute by submitting issues or pull requests on the [GitHub repository](https://github.com/CodeAndCandlesticks/py-schwab-wrapper).
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Acknowledgements
This project was developed with assistance from ChatGPT by OpenAI.
Also special thanks to [rderik](https://rderik.com/) for the motivation to pursue this project.
Raw data
{
"_id": null,
"home_page": "https://github.com/CodeAndCandlesticks/py-schwab-wrapper",
"name": "py-schwab-wrapper",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Luis Perez",
"author_email": "luispe@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/82/d4/3d8abfeb913c7e46e3756aef4c300857832525861dae649a7c9f3fa31b6d/py_schwab_wrapper-0.2.0.tar.gz",
"platform": null,
"description": "# py-schwab-wrapper\n\n`py-schwab-wrapper` is a Python wrapper for Schwab's API, designed to handle authentication, token management, and data retrieval from Schwab's market data services. The initial version provides functionality to get price history for a symbol, with OAuth2-based authentication flow.\n\n## Features\n\nBe sure to check out our CHANGELOG for full details on functionality\n\n- OAuth2 authentication and token refresh.\n- Fetch historical price data for a symbol.\n- Get account information\n- Get orders for a specific account\n- Place orders with user friendly abstractions.\n\n## Installation\n\nYou can install the `py-schwab-wrapper` package via `pip`:\n\n```bash\npip install py-schwab-wrapper\n```\n\n## Usage\n\n### Initial Setup\n\n1. **Obtain OAuth Credentials**: You need to get your `client_id` and `client_secret` from Schwab's developer portal.\n2. **Set Environment Variables**: You can store these credentials in a `.env` file in your project root.\n\nExample `.env` file:\n\n```bash\nSCHWAB_CLIENT_ID=<your_client_id>\nSCHWAB_CLIENT_SECRET=<your_client_secret>\n```\n\n3. **Authenticate and Generate `token.json`**:\n The first time you use the API, you need to authenticate using Schwab\u2019s OAuth2 flow to generate a `token.json` file. You can use the included `authenticate.py` to handle this process.\n\n\n## OAuth2 Authentication Flow\n\nBefore making API requests, you need to authenticate via Schwab's OAuth2 flow and store the access/refresh tokens in a `token.json` file. Here's how you can do it using the included `authenticate.py` script:\n\n1. Run `authenticate.py` to start the OAuth2 flow:\n ```bash\n python authenticate.py\n ```\n2. Follow the instructions in the browser to authorize the application and obtain the tokens.\n\n### A note on authentication\nBe mindful if you configure your application's callback URL to be secure `https://127.0.0.1:5000/callback`, you'll need to set up HTTPS for your local Flask server. Here's how you can achieve this:\n```\n openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes\n```\nFollow the prompts to provide the necessary information. This will create key.pem (private key) and cert.pem (certificate) files. And be sure to include them in your .gitignore file!\n\n#### Additional .gitignore recommendations\n```\n# ...your boilerplate .gitignore file for python...\n\n# Ignore .pem files\ncert.pem\nkey.pem\n.pem\n\n# Ignore token.json file if you decide to do local storage of the token\ntoken.json\n```\n\n## Example API Usage\n\nOnce authenticated, you can use the wrapper to get historical market data, get account information, or place an order. Be sure to check out our examples/ folder.\n\n```python\nfrom py_schwab_wrapper.schwab_api import SchwabAPI\nfrom datetime import datetime, timedelta\n\n# Initialize the API wrapper with your credentials\nschwab_api = SchwabAPI(client_id=\"<your_client_id>\", client_secret=\"<your_client_secret>\")\n\n# Define the time range\nnow = datetime.now()\nstart_of_day = now.replace(hour=9, minute=30, second=0, microsecond=0)\nend_of_day = now.replace(hour=16, minute=0, second=0, microsecond=0)\nstart_date = int(start_of_day.timestamp() * 1000)\nend_date = int(end_of_day.timestamp() * 1000)\n\n# Fetch price history\nprice_history = schwab_api.get_price_history(\n symbol='QQQ', \n period_type='day', \n period=1, \n frequency_type='minute', \n frequency=5, \n need_extended_hours_data=False, \n need_previous_close=True, \n start_date=start_date, \n end_date=end_date\n)\n\nprint(price_history)\n```\n\n## Contributing\n\nFeel free to contribute by submitting issues or pull requests on the [GitHub repository](https://github.com/CodeAndCandlesticks/py-schwab-wrapper).\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Acknowledgements\n\nThis project was developed with assistance from ChatGPT by OpenAI. \nAlso special thanks to [rderik](https://rderik.com/) for the motivation to pursue this project.\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A Python wrapper for Schwab API",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/CodeAndCandlesticks/py-schwab-wrapper"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ac7a28df39ce25ee3b1f7b9da0c3a81fb532302a10955c1c5e2869e2e38fee0c",
"md5": "4af22b785372b17169ce3fc4df1f4d23",
"sha256": "6bb65a13f199ad95a4b908be97a3593688bf4bfc2142178caa4271b957a21baf"
},
"downloads": -1,
"filename": "py_schwab_wrapper-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4af22b785372b17169ce3fc4df1f4d23",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 10121,
"upload_time": "2024-09-25T14:41:19",
"upload_time_iso_8601": "2024-09-25T14:41:19.007921Z",
"url": "https://files.pythonhosted.org/packages/ac/7a/28df39ce25ee3b1f7b9da0c3a81fb532302a10955c1c5e2869e2e38fee0c/py_schwab_wrapper-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "82d43d8abfeb913c7e46e3756aef4c300857832525861dae649a7c9f3fa31b6d",
"md5": "fc81c277b9eaafeb40d7f93149f080c3",
"sha256": "a3101704ad99d62d6831f7e0acb9b2f1592c97e1813b12a90b5ed3c6d8addbd1"
},
"downloads": -1,
"filename": "py_schwab_wrapper-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "fc81c277b9eaafeb40d7f93149f080c3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 11024,
"upload_time": "2024-09-25T14:41:20",
"upload_time_iso_8601": "2024-09-25T14:41:20.420980Z",
"url": "https://files.pythonhosted.org/packages/82/d4/3d8abfeb913c7e46e3756aef4c300857832525861dae649a7c9f3fa31b6d/py_schwab_wrapper-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-25 14:41:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "CodeAndCandlesticks",
"github_project": "py-schwab-wrapper",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "requests",
"specs": []
},
{
"name": "python-dotenv",
"specs": []
},
{
"name": "pytz",
"specs": []
},
{
"name": "flask",
"specs": []
},
{
"name": "requests-oauthlib",
"specs": []
}
],
"lcname": "py-schwab-wrapper"
}