aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api.py36
-rw-r--r--app.py33
-rw-r--r--last_chart_data.json1
3 files changed, 51 insertions, 19 deletions
diff --git a/api.py b/api.py
index 0625f1a..00c2733 100644
--- a/api.py
+++ b/api.py
@@ -1,10 +1,9 @@
import requests
import json
-
"""
Given the parameters,
fetches the data for the corresponding chart using yahoo finance.
-Expect it to raise an error on bad params!
+If bad request, returns the previous chart.
"""
def fetch_chart_data(ticker, period='1y', interval='1d'):
params = {
@@ -19,20 +18,45 @@ def fetch_chart_data(ticker, period='1y', interval='1d'):
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
+ try:
+ r.raise_for_status() # raises if error before parsing
+ except:
+ last_data = pull_last_from_file()
+ last_data['error'] = True
+ return last_data
+
data_obj = r.json()
# get the specific data we want
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))
+
# clean out null's and 0s from the data
- for i in range(len(timestamps)):
+ i = 0
+ while i < len(timestamps):
if close_prices[i] == None or close_prices[i] == 0:
del close_prices[i]
del timestamps[i]
i -= 1
+ i += 1
name = data_obj['chart']['result'][0]['meta']['longName']
- return {'timestamps': timestamps, 'prices': close_prices, 'name': name} \ No newline at end of file
+ data = {'timestamps': timestamps, 'prices': close_prices, 'name': name, 'error': False}
+
+ update_last_file(data)
+ # save data to file in case necessary
+ return data
+
+# helper function to pull most recent chart data on failure
+def pull_last_from_file():
+ fd = open('last_chart_data.json', 'r')
+ d = json.loads(fd.read())
+ fd.close()
+ return d
+
+def update_last_file(data):
+ fd = open('last_chart_data.json', 'w')
+ fd.truncate(0)
+ fd.write(json.dumps(data))
+ fd.close() \ No newline at end of file
diff --git a/app.py b/app.py
index c815f10..c251f73 100644
--- a/app.py
+++ b/app.py
@@ -5,6 +5,7 @@ from ema import calc_emas, calculate_profit
import plotly.graph_objects as go
import json
import datetime
+import pandas as pd
app = Dash(__name__)
@@ -24,8 +25,6 @@ app = Dash(__name__)
# for x,y in interpolated_intersections:
# intersected_x.append(x)
# intersected_y.append(y)
-percent_gain = 0
-memo_fig = None
app.layout = html.Div([
html.H4('Interactive color selection with simple Dash example'),
@@ -47,21 +46,24 @@ app.layout = html.Div([
),
html.Hr(),
dcc.Graph(id="graph"),
- html.P("If bought and sold on these signals, the percent gain/loss would be: " + str(percent_gain))
+ html.P("If bought and sold on these signals, the percent gain/loss would be: TODO"),
+ html.P(id="percent_gain")
+
])
@app.callback(
Output("graph", "figure"),
+ Output("percent_gain", "children"),
Input("ticker", "value"),
Input("period_dropdown", "value"),
Input("interval_dropdown", "value")
)
def display_color(ticker, period, interval):
- try:
chart_data = fetch_chart_data(ticker, period, interval)
- except:
- return memo_fig
- else:
+ if chart_data['error'] == True:
+ # implement a feeback mechanism for ERROR codes
+ print("TODO: FIX THIS")
+
timestamps_raw = chart_data['timestamps']
timestamps = [datetime.datetime.fromtimestamp(t) for t in timestamps_raw]
prices = chart_data['prices']
@@ -70,6 +72,7 @@ def display_color(ticker, period, interval):
ema_13 = calc_emas(13, prices)
profit = calculate_profit(ema_5, ema_13, prices, timestamps, 13)
buy_info = profit[-2]
+ print(buy_info)
buy_x = []
buy_y = []
for x,y,_ in buy_info:
@@ -85,21 +88,25 @@ def display_color(ticker, period, interval):
print("Result Analysis:\n", "Percent gain/loss:\t", profit[0], profit[1], profit[2])
percent_gain = profit[0] * 100
- finally:
+
# Code to execute no matter what (optional)
fig = go.Figure(
- [
+ data = [
go.Scatter(name='Price', x=timestamps, y=prices, line=dict(color='rgb(0, 0, 0)'), mode='lines'),
# go.Scatter(name='5 day EMA', x=timestamps, y=ema_5, line=dict(color='rgb(0, 255, 0)'), mode='lines'),
# go.Scatter(name='13 day EMA', x=timestamps, y=ema_13, line=dict(color='rgb(0, 0, 255)'), mode='lines'),
# go.Scatter(name='EMA Intersections', x=intersected_x, y=intersected_y, line=dict(color='rgb(255, 0, 0)'), mode='markers'),
go.Scatter(name='Buys', x=buy_x, y=buy_y, line=dict(color='rgb(0, 0, 255)'), mode='markers', marker_size=10),
go.Scatter(name='Sells', x=sell_x, y=sell_y, line=dict(color='rgb(255, 255, 0)'), mode='markers', marker_size=10),
- ]
+ ],
+ layout = go.Layout(
+ title=go.layout.Title(text='Chart for ' + chart_data['name']),
+ xaxis=go.layout.XAxis(title='Date (dt=' + interval + ', range=' + period + ')'),
+ yaxis=go.layout.YAxis(title='Price ($)')
+ )
)
- print(ticker, period, interval)
- memo_fig = fig
- return fig
+ return (fig, percent_gain)
+
app.run(debug=True) \ No newline at end of file
diff --git a/last_chart_data.json b/last_chart_data.json
new file mode 100644
index 0000000..cfbffbf
--- /dev/null
+++ b/last_chart_data.json
@@ -0,0 +1 @@
+{"timestamps": [1722259800, 1722346200, 1722432600, 1722519000, 1722605400, 1722864600, 1722951000, 1723037400, 1723123800, 1723210200, 1723469400, 1723555800, 1723642200, 1723728600, 1723815000, 1724074200, 1724160600, 1724247000, 1724333400, 1724419800, 1724679000, 1724765400, 1724851800, 1724938200, 1725024600, 1725370200, 1725456600, 1725543000, 1725629400, 1725888600, 1725975000, 1726061400, 1726147800, 1726234200, 1726493400, 1726579800, 1726666200, 1726752600, 1726839000, 1727098200, 1727184600, 1727271000, 1727357400, 1727443800, 1727703000, 1727789400, 1727875800, 1727962200, 1728048600, 1728307800, 1728394200, 1728480600, 1728567000, 1728653400, 1728912600, 1728999000, 1729085400, 1729171800, 1729258200, 1729517400, 1729603800, 1729690200, 1729776600, 1729863000, 1730122200, 1730208600, 1730295000, 1730381400, 1730467800, 1730730600, 1730817000, 1730903400, 1730989800, 1731076200, 1731335400, 1731421800, 1731508200, 1731594600, 1731681000, 1731940200, 1732026600, 1732113000, 1732199400, 1732285800, 1732545000, 1732631400, 1732717800, 1732890600, 1733149800, 1733236200, 1733322600, 1733409000, 1733495400, 1733754600, 1733841000, 1733927400, 1734013800, 1734100200, 1734359400, 1734445800, 1734532200, 1734618600, 1734705000, 1734964200, 1735050600, 1735223400, 1735309800, 1735569000, 1735655400, 1735828200, 1735914600, 1736173800, 1736260200, 1736346600, 1736519400, 1736778600, 1736865000, 1736951400, 1737037800, 1737124200, 1737469800, 1737556200, 1737642600, 1737729000, 1737988200, 1738074600, 1738161000, 1738247400, 1738333800, 1738593000, 1738679400, 1738765800, 1738852200, 1738938600, 1739197800, 1739284200, 1739370600, 1739457000, 1739543400, 1739889000, 1739975400, 1740061800, 1740148200, 1740407400, 1740493800, 1740580200, 1740666600, 1740753000, 1741012200, 1741098600, 1741185000, 1741271400, 1741357800, 1741613400, 1741699800, 1741786200, 1741872600, 1741959000, 1742218200, 1742304600, 1742391000, 1742477400, 1742563800, 1742823000, 1742909400, 1742995800, 1743082200, 1743168600, 1743427800, 1743514200, 1743600600, 1743687000, 1743773400, 1744032600, 1744119000, 1744205400, 1744291800, 1744378200, 1744637400, 1744723800, 1744810200, 1744896600, 1745242200, 1745328600, 1745415000, 1745501400, 1745587800, 1745847000, 1745933400, 1746019800, 1746106200, 1746192600, 1746451800, 1746538200, 1746624600, 1746711000, 1746797400, 1747056600, 1747143000, 1747229400, 1747315800, 1747402200, 1747661400, 1747747800, 1747834200, 1747920600, 1748007000, 1748352600, 1748439000, 1748525400, 1748611800, 1748871000, 1748957400, 1749043800, 1749130200, 1749216600, 1749475800, 1749562200, 1749648600, 1749735000, 1749821400, 1750080600, 1750167000, 1750253400, 1750426200, 1750685400, 1750771800, 1750858200, 1750944600, 1751031000, 1751290200, 1751376600, 1751463000, 1751549400, 1751895000, 1751981400, 1752067800, 1752154200, 1752240600, 1752499800, 1752586200, 1752672600, 1752759000, 1752845400, 1753104600, 1753191000, 1753277400, 1753363800, 1753450200, 1753709400], "prices": [544.760009765625, 542.0, 550.8099975585938, 543.010009765625, 532.9000244140625, 517.3800048828125, 522.1500244140625, 518.6599731445312, 530.6500244140625, 532.989990234375, 533.27001953125, 542.0399780273438, 543.75, 553.0700073242188, 554.3099975585938, 559.6099853515625, 558.7000122070312, 560.6199951171875, 556.219970703125, 562.1300048828125, 560.7899780273438, 561.5599975585938, 558.2999877929688, 558.3499755859375, 563.6799926757812, 552.0800170898438, 550.9500122070312, 549.6099853515625, 540.3599853515625, 546.4099731445312, 548.7899780273438, 554.4199829101562, 559.0900268554688, 562.010009765625, 562.8400268554688, 563.0700073242188, 561.4000244140625, 570.97998046875, 568.25, 569.6699829101562, 571.2999877929688, 570.0399780273438, 572.2999877929688, 571.469970703125, 573.760009765625, 568.6199951171875, 568.8599853515625, 567.8200073242188, 572.97998046875, 567.7999877929688, 573.1699829101562, 577.1400146484375, 576.1300048828125, 579.5800170898438, 584.3200073242188, 579.780029296875, 582.2999877929688, 582.3499755859375, 584.5900268554688, 583.6300048828125, 583.3200073242188, 577.989990234375, 579.239990234375, 579.0399780273438, 580.8300170898438, 581.77001953125, 580.010009765625, 568.6400146484375, 571.0399780273438, 569.8099975585938, 576.7000122070312, 591.0399780273438, 595.6099853515625, 598.1900024414062, 598.760009765625, 596.9000244140625, 597.1900024414062, 593.3499755859375, 585.75, 588.1500244140625, 590.2999877929688, 590.5, 593.6699829101562, 595.510009765625, 597.530029296875, 600.6500244140625, 598.8300170898438, 602.5499877929688, 603.6300048828125, 603.9099731445312, 607.6599731445312, 606.6599731445312, 607.8099975585938, 604.6799926757812, 602.7999877929688, 607.4600219726562, 604.3300170898438, 604.2100219726562, 606.7899780273438, 604.2899780273438, 586.280029296875, 586.0999755859375, 591.1500244140625, 594.6900024414062, 601.2999877929688, 601.3400268554688, 595.010009765625, 588.219970703125, 586.0800170898438, 584.6400146484375, 591.9500122070312, 595.3599853515625, 588.6300048828125, 589.489990234375, 580.489990234375, 581.3900146484375, 582.1900024414062, 592.780029296875, 591.6400146484375, 597.5800170898438, 603.0499877929688, 606.4400024414062, 609.75, 607.969970703125, 599.3699951171875, 604.52001953125, 601.8099975585938, 605.0399780273438, 601.8200073242188, 597.77001953125, 601.780029296875, 604.219970703125, 606.3200073242188, 600.77001953125, 604.8499755859375, 605.3099975585938, 603.3599853515625, 609.72998046875, 609.7000122070312, 611.489990234375, 612.9299926757812, 610.3800048828125, 599.9400024414062, 597.2100219726562, 594.239990234375, 594.5399780273438, 585.0499877929688, 594.1799926757812, 583.77001953125, 576.8599853515625, 583.0599975585938, 572.7100219726562, 575.9199829101562, 560.5800170898438, 555.9199829101562, 558.8699951171875, 551.4199829101562, 562.8099975585938, 567.1500244140625, 561.02001953125, 567.1300048828125, 565.489990234375, 563.97998046875, 574.0800170898438, 575.4600219726562, 568.5900268554688, 567.0800170898438, 555.6599731445312, 559.3900146484375, 560.969970703125, 564.52001953125, 536.7000122070312, 505.2799987792969, 504.3800048828125, 496.4800109863281, 548.6199951171875, 524.5800170898438, 533.9400024414062, 539.1199951171875, 537.6099853515625, 525.6599731445312, 526.4099731445312, 513.8800048828125, 527.25, 535.4199829101562, 546.6900024414062, 550.6400146484375, 550.8499755859375, 554.3200073242188, 554.5399780273438, 558.469970703125, 566.760009765625, 563.510009765625, 558.7999877929688, 561.1500244140625, 565.0599975585938, 564.3400268554688, 582.989990234375, 586.8400268554688, 587.5900268554688, 590.4600219726562, 594.2000122070312, 594.8499755859375, 592.8499755859375, 582.8599853515625, 583.0900268554688, 579.1099853515625, 591.1500244140625, 587.72998046875, 590.0499877929688, 589.3900146484375, 592.7100219726562, 596.0900268554688, 595.9299926757812, 593.0499877929688, 599.1400146484375, 599.6799926757812, 603.0800170898438, 601.3599853515625, 603.75, 597.0, 602.6799926757812, 597.530029296875, 597.4400024414062, 594.280029296875, 600.1500244140625, 606.780029296875, 607.1199951171875, 611.8699951171875, 614.9099731445312, 617.8499755859375, 617.6500244140625, 620.4500122070312, 625.3400268554688, 620.6799926757812, 620.3400268554688, 624.0599975585938, 625.8200073242188, 623.6199951171875, 624.8099975585938, 622.1400146484375, 624.219970703125, 628.0399780273438, 627.5800170898438, 628.77001953125, 628.8599853515625, 634.2100219726562, 634.4199829101562, 637.0999755859375, 636.9400024414062], "name": "SPDR S&P 500 ETF", "error": false} \ No newline at end of file