diff options
author | loit <michael.foiani@gmail.com> | 2025-07-28 15:00:02 -0400 |
---|---|---|
committer | loit <michael.foiani@gmail.com> | 2025-07-28 15:00:02 -0400 |
commit | 8062d3a9cc10ccfec3dab7f859fa0d1d4c118d38 (patch) | |
tree | dc6ca70dd923e900a752c0201d86719ea2345a96 /main.py | |
parent | bf04bf3d8b256b7d8c0ea581ec5c846699a4f959 (diff) |
basic functionality to pull data uising requests and make a chart using Dash and plotly
Diffstat (limited to 'main.py')
-rw-r--r-- | main.py | 43 |
1 files changed, 42 insertions, 1 deletions
@@ -1 +1,42 @@ -print("hello")
\ No newline at end of file +import requests +import json + +print("hello") + +""" +First pull data from yahoo api +""" +params = {'period1' : '1753487940', + 'period2' : '1753725600', + 'interval' : '1m', + 'events' : 'div|split|earn', + 'includePrePost' : 'false' } +headers = {'User-agent' : 'fin-backtesting-proj'} +r = requests.get("https://query2.finance.yahoo.com/v8/finance/chart/AAPL", headers=headers, params=params) + +print(r.url) +print("status_code:\t", r.status_code) + + +# decode the JSON response data into a Python object +r.raise_for_status() # raises if error before parsing +data_obj = r.json() + +timestamps = data_obj['chart']['result'][0]['timestamp'] +close_prices = data_obj['chart']['result'][0]['indicators']['quote'][0]['close'] +print('close_price len: ', len(close_prices), 'timestamps len: ', len(timestamps)) + +# save timestamps and close prices into separate files +timestamps_encoded = json.dumps(timestamps) +close_prices_encoded = json.dumps(close_prices) + +timestamps_file = open('timestamps.json', 'w') +timestamps_file.truncate(0) # erase current content in file +timestamps_file.write(timestamps_encoded) +timestamps_file.close() + +close_prices_file = open('close_prices.json', 'w') +close_prices_file.truncate(0) +close_prices_file.write(close_prices_encoded) +timestamps_file.close() + |