aboutsummaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
Diffstat (limited to 'app.py')
-rw-r--r--app.py134
1 files changed, 81 insertions, 53 deletions
diff --git a/app.py b/app.py
index 51e0564..c815f10 100644
--- a/app.py
+++ b/app.py
@@ -1,5 +1,7 @@
from dash import Dash, dcc, html, Input, Output
-from analysis import calc_emas, find_intersections, interpolate_intersection, calculate_profit
+from analysis import find_intersections, interpolate_intersection
+from api import fetch_chart_data
+from ema import calc_emas, calculate_profit
import plotly.graph_objects as go
import json
import datetime
@@ -7,71 +9,97 @@ import datetime
app = Dash(__name__)
# pull stock data from json files
-timestamps_file = open('timestamps.json', 'r')
-timestamps_file_data = timestamps_file.read()
-timestamps_raw = json.loads(timestamps_file_data)
-timestamps = [datetime.datetime.fromtimestamp(t) for t in timestamps_raw]
+# timestamps_file = open('timestamps.json', 'r')
+# timestamps_file_data = timestamps_file.read()
+# timestamps_raw = json.loads(timestamps_file_data)
+# timestamps = [datetime.datetime.fromtimestamp(t) for t in timestamps_raw]
-prices_file = open('close_prices.json', 'r')
-prices = json.loads(prices_file.read())
+# prices_file = open('close_prices.json', 'r')
+# prices = json.loads(prices_file.read())
-ema_5 = calc_emas(5, prices)
-ema_13 = calc_emas(13, prices)
-
-intersection_indices = find_intersections(ema_5, ema_13, offset=13) # offset so don't calculate the SMA days
-interpolated_intersections = [interpolate_intersection(indices, timestamps, ema_5, ema_13) for indices in intersection_indices]
-intersected_x = []
-intersected_y = []
-for x,y in interpolated_intersections:
- intersected_x.append(x)
- intersected_y.append(y)
-
-profit = calculate_profit(ema_5, ema_13, prices, timestamps, 13)
-buy_info = profit[-2]
-buy_x = []
-buy_y = []
-for x,y,_ in buy_info:
- buy_x.append(x)
- buy_y.append(y)
-
-sell_info = profit[-1]
-sell_x = []
-sell_y = []
-for x,y,_ in sell_info:
- sell_x.append(x)
- sell_y.append(y)
-
-print("Result Analysis:\n", "Percent gain/loss:\t", profit[0])
-percent_gain = profit[0] * 100
+# intersection_indices = find_intersections(ema_5, ema_13, offset=13) # offset so don't calculate the SMA days
+# interpolated_intersections = [interpolate_intersection(indices, timestamps, ema_5, ema_13) for indices in intersection_indices]
+# intersected_x = []
+# intersected_y = []
+# 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'),
- html.P("Select color:"),
+ html.Label("Ticker ", htmlFor="ticker"),
+ dcc.Input(id="ticker", value="SPY", type="text"),
+ html.Br(),
+ html.Label("Period ", htmlFor="period"),
dcc.Dropdown(
- id="dropdown",
- options=['Gold', 'MediumTurquoise', 'LightGreen'],
- value='Gold',
- clearable=False,
+ id="period_dropdown",
+ options=["1d","5d","1mo","3mo","6mo","1y","2y","5y","10y","ytd","max"],
+ value = "1y",
),
+ html.Br(),
+ html.Label("Interval ", htmlFor="Interval"),
+ dcc.Dropdown(
+ id="interval_dropdown",
+ options=["1m", "2m", "5m", "15m", "30m", "60m", "90m", "1h", "4h", "1d", "5d", "1wk", "1mo", "3mo"],
+ value = "1d",
+ ),
+ html.Hr(),
dcc.Graph(id="graph"),
- html.P("If bought and sold on these signals, the percent gain/loss would be: " + str(round(percent_gain, 4)))
+ html.P("If bought and sold on these signals, the percent gain/loss would be: " + str(percent_gain))
])
@app.callback(
Output("graph", "figure"),
- Input("dropdown", "value"))
-def display_color(color):
- fig = go.Figure(
- [
- go.Scatter(name='Price', x=timestamps, y=prices, line=dict(color='rgb(0, 255, 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),
- ]
+ Input("ticker", "value"),
+ Input("period_dropdown", "value"),
+ Input("interval_dropdown", "value")
)
- return fig
+def display_color(ticker, period, interval):
+ try:
+ chart_data = fetch_chart_data(ticker, period, interval)
+ except:
+ return memo_fig
+ else:
+ timestamps_raw = chart_data['timestamps']
+ timestamps = [datetime.datetime.fromtimestamp(t) for t in timestamps_raw]
+ prices = chart_data['prices']
+
+ ema_5 = calc_emas(5, prices)
+ ema_13 = calc_emas(13, prices)
+ profit = calculate_profit(ema_5, ema_13, prices, timestamps, 13)
+ buy_info = profit[-2]
+ buy_x = []
+ buy_y = []
+ for x,y,_ in buy_info:
+ buy_x.append(x)
+ buy_y.append(y)
+
+ sell_info = profit[-1]
+ sell_x = []
+ sell_y = []
+ for x,y,_ in sell_info:
+ sell_x.append(x)
+ sell_y.append(y)
+
+ 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(
+ [
+ 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),
+ ]
+ )
+ print(ticker, period, interval)
+ memo_fig = fig
+ return fig
app.run(debug=True) \ No newline at end of file