-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplots.py
258 lines (219 loc) · 5.57 KB
/
plots.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import os
from _config import *
from strategies import *
def plot_graph(stock_plot_df, trade_plot_df, broke_plot_df,
stock, strategy, indicator_1_name, indicator_2_name, save_results_choice=False):
'''TODO: describe this function
'''
# ----------------------------------------------------------------------------------------------
'''
TODO: plot the graphs
TODO: combine the graphs into a single graph
NOTE: adding <extra></extra> to the end of the hovertemplate will remove the NAME of the line / plotted thing!
'''
fig = go.Figure()
fig = make_subplots(
rows=3, cols=1,
shared_xaxes=True,
vertical_spacing=0.005,
specs=[
[{"type": "xy"}],
[{"type": "xy"}],
# [{"type": "xy"}],
[{"type": "xy","secondary_y": True}]
]
)
# ----------------------------------------------------------------------------------------------
# plot the stock data over time
# plot the first indicator (High/Fast)
fig.add_trace(
go.Scatter(
x=stock_plot_df.Date,
y=stock_plot_df.Indicator_1,
name=indicator_1_name,
hoverinfo='text+name',
line_shape='linear',
line=dict(color='rgba(204,204,204,0.95)'),
# hovertemplate='<extra></extra>',
),
row=3, col=1,
# secondary_y=True,
)
# plot the second indicator (Low/Slow)
fig.add_trace(
go.Scatter(
x=stock_plot_df.Date,
y=stock_plot_df.Indicator_2,
name=indicator_2_name,
hoverinfo='text+name',
line_shape='linear',
line=dict(color='rgba(204,204,204,0.95)'),
# hovertemplate='<extra></extra>',
),
row=3, col=1,
# secondary_y=True,
)
# ----------------------------------------------------------------------------------------------
# # plot the Close
fig.add_trace(
go.Scatter(
x=stock_plot_df.Date,
y=stock_plot_df.Close,
name=stock,
hoverinfo='text+name',
line_shape='linear',
line=dict(color="#4F4F4F"),
hovertemplate='<extra></extra>',
),
row=3, col=1,
# secondary_y=True,
)
# plot the Buy signals
fig.add_trace(
go.Scatter(
x=stock_plot_df.Date,
y=stock_plot_df.BuySignal,
name="Buy",
hoverinfo='text+name',
mode='markers',
marker_symbol='triangle-up',
marker_size=12,
marker_color="green",
hovertemplate='Buy<br>Price: $%{y:.2f}'+'<br>Date: %{x}<extra></extra>',
),
row=3, col=1,
# secondary_y=True,
)
# plot the Sell signals
fig.add_trace(
go.Scatter(
x=stock_plot_df.Date,
y=stock_plot_df.SellSignal,
name="Sell",
hoverinfo='text+name',
mode='markers',
marker_symbol='triangle-down',
marker_size=12,
marker_color="red",
hovertemplate='Sell<br>Price: $%{y:.2f}'+'<br>Date: %{x}<extra></extra>',
),
row=3, col=1,
# secondary_y=True,
)
# plot candlesticks using open-high-low-close
# fig.add_trace(
# go.Candlestick(
# x=stock_plot_df.Date,
# open=stock_plot_df.Open,
# high=stock_plot_df.High,
# low=stock_plot_df.Low,
# close=stock_plot_df.Close,
# name="SPY",
# # yaxis='y1',
# ),
# row=3, col=1,
# # secondary_y=True
# )
# fig.add_trace(
# go.Bar(
# x=stock_plot_df.Date,
# y=stock_plot_df.Volume,
# name='Volume',
# yaxis='y2',
# marker={'color': 'rgba(128,128,128,0.85)'},
# ),
# # row=3, col=1,
# secondary_y=False,
# )
# ----------------------------------------------------------------------------------------------
# plot the trading profit over time
# plot the positive trades
fig.add_trace(
go.Scatter(
x=trade_plot_df.Date,
y=trade_plot_df.Positive,
name="Positive",
# hoverinfo='text+name',
# line_shape='linear',
mode='markers',
marker_symbol='circle',
marker_size=12,
marker_color="blue",
hovertemplate='Realized Gain: $%{y:.2f}'+'<br>Date: %{x}<extra></extra>'
),
row=2,col=1
)
# plot the negative trades
fig.add_trace(
go.Scatter(
x=trade_plot_df.Date,
y=trade_plot_df.Negative,
name="Negative",
# hoverinfo='text+name',
# line_shape='linear',
mode='markers',
marker_symbol='circle',
marker_size=12,
marker_color="red",
hovertemplate='Realized Loss: $%{y:.2f}'+'<br>Date: %{x}<extra></extra>'
),
row=2,col=1
)
# ----------------------------------------------------------------------------------------------
# plot the brokerage values over time
# plot the positive trades
fig.add_trace(
go.Scatter(
x=broke_plot_df.Date,
y=broke_plot_df.TotalCash,
name="Total Cash",
# hoverinfo='text+name',
line_shape='linear',
# mode='markers',
# marker_symbol='circle',
# marker_size=12,
# marker_color="blue",
hovertemplate='Cash: $%{y:.2f}'+'<extra></extra>',
line=dict(color="red"),
),
row=1,col=1
)
# plot the negative trades
fig.add_trace(
go.Scatter(
x=broke_plot_df.Date,
y=broke_plot_df.TotalValue,
name="Total Value",
# hoverinfo='text+name',
line_shape='linear',
# mode='markers',
# marker_symbol='circle',
# marker_size=12,
# marker_color="red",
hovertemplate='Value: $%{y:.2f}'+'<extra></extra>',
line=dict(color="blue"),
),
row=1,col=1
)
fig.update_xaxes(rangeslider=dict(visible=False))
fig.update_yaxes(autorange=True)
fig.update_layout(
title=stock+" - "+strategy,
width=PLOT_WIDTH,
height=PLOT_HEIGHT,
yaxis = dict(range=[0,1000000000]),
# yaxis2 = dict(range=[0,500]),
# hovermode='x',
)
fig.update(
# layout_xaxis_rangeslider_visible=False,
)
fig.show()
# save the results as a PNG if wanted
if save_results_choice:
if not os.path.exists('images'):
os.mkdir('images')
fig.write_image('images/'+stock+'_'+STRATEGIES[strategy]['name']+'_strat.png')
return None