diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | app.py | 42 | ||||
-rw-r--r-- | main.py | 43 |
3 files changed, 87 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f218e1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +close_prices.json +timestamps.json +__pycache__/
\ No newline at end of file @@ -0,0 +1,42 @@ +from dash import Dash, dcc, html, Input, Output +import plotly.graph_objects as go +import json +import datetime + +app = Dash(__name__) + + +app.layout = html.Div([ + html.H4('Interactive color selection with simple Dash example'), + html.P("Select color:"), + dcc.Dropdown( + id="dropdown", + options=['Gold', 'MediumTurquoise', 'LightGreen'], + value='Gold', + clearable=False, + ), + dcc.Graph(id="graph"), +]) + +# pull stock data from json files +timestamps_file = open('timestamps.json', 'r') +timestamps_file_data = timestamps_file.read() +timestamps = json.loads(timestamps_file_data) +timestamps = [datetime.datetime.fromtimestamp(t) for t in timestamps] + +prices_file = open('close_prices.json', 'r') +prices = json.loads(prices_file.read()) + +# print('timestamps:\t', timestamps, '\nprices:\t', prices) + + +@app.callback( + Output("graph", "figure"), + Input("dropdown", "value")) +def display_color(color): + fig = go.Figure( + data=go.Line(x=timestamps, y=prices, marker_color=color)) + return fig + + +app.run(debug=True)
\ No newline at end of file @@ -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() + |