Skip to content

Instantly share code, notes, and snippets.

@hn4002
Created May 11, 2024 18:40
Show Gist options
  • Select an option

  • Save hn4002/7c1a45ade87790e18c9f519120977ef6 to your computer and use it in GitHub Desktop.

Select an option

Save hn4002/7c1a45ade87790e18c9f519120977ef6 to your computer and use it in GitHub Desktop.
schwab-py: Get price history
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()
@androslee
Copy link

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....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment