1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
from dash import Dash, dcc, html, Input, Output
from analysis import calc_emas, find_intersections, interpolate_intersection
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_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())
ema_5 = calc_emas(5, prices)
ema_13 = calc_emas(13, prices)
intersection_indices = find_intersections(ema_5, ema_13)
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)
@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, 255)'), mode='lines'), # prices
go.Scatter(name='5 day EMA', x=timestamps, y=ema_5, line=dict(color='rgb(0, 255, 0)'), mode='lines'), # 5 ema line
go.Scatter(name='13 day EMA', x=timestamps, y=ema_13, line=dict(color='rgb(0, 0, 255)'), mode='lines'), # 13 ema line
go.Scatter(name='EMA Intersections', x=intersected_x, y=intersected_y, line=dict(color='rgb(255, 0, 0)'), mode='markers') # EMA intersection points
]
)
return fig
app.run(debug=True)
|