Created
May 11, 2024 18:40
-
-
Save hn4002/7c1a45ade87790e18c9f519120977ef6 to your computer and use it in GitHub Desktop.
schwab-py: Get price history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import json | |
| import os | |
| import pytz | |
| import sys | |
| import schwab | |
| from environment.instance_settings import schwabSettings | |
| client_id = schwabSettings.SCHWAB_APP_ID | |
| client_secret = schwabSettings.SCHWAB_APP_SECRET | |
| redirect_uri = schwabSettings.SCHWAB_REDIRECT_URI | |
| token_path = schwabSettings.SCHWAB_TOKEN_PATH | |
| #=============================================================================== | |
| def main(): | |
| c = schwab.auth.client_from_token_file(api_key=client_id, app_secret=client_secret, token_path=token_path) | |
| tz = pytz.timezone('US/Eastern') | |
| dt_end = datetime.datetime.now(tz) | |
| need_extended_hours_data = True | |
| dt_start = dt_end - datetime.timedelta(days=2) | |
| r = c.get_price_history_every_minute('TSLA', start_datetime=dt_start, end_datetime=dt_end, need_extended_hours_data=need_extended_hours_data) | |
| print(f"status code = {r.status_code}") | |
| if r.status_code == 200: | |
| print(json.dumps(r.json(), indent=4)) | |
| else: | |
| print(r.text) | |
| #=============================================================================== | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you for the example. this was the slightly more thorough example i needed, than what the documentation had. especially how to get the usable info from the response object. i forgot about that....