-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathdisplay_lib.py
More file actions
465 lines (432 loc) · 14 KB
/
Copy pathdisplay_lib.py
File metadata and controls
465 lines (432 loc) · 14 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import plotly.graph_objects as go
from dash import Dash, html, dcc
from plotly.subplots import make_subplots
# Function to display a plotly graph in dash
def display_graph(plotly_fig, graph_title, dash=False):
"""
Function to display a plotly graph using Dash
:param plotly_fig: plotly figure
:param graph_title: string
:param dash: boolean to determine whether to run the dash server
:return: None
"""
# Add in layout features for each plotly figure
plotly_fig.update_layout(
xaxis_rangeslider_visible=False,
autosize=True,
height=800
)
plotly_fig.update_yaxes(automargin=True)
if dash:
# Create the Dash object
app = Dash(__name__)
# Construct view
app.layout = html.Div(children=[
html.H1(children=graph_title),
html.Div("Created by James Hinton from AlgoQuant.Trade"),
dcc.Graph(
id=graph_title,
figure=plotly_fig
)
])
# Run the image
app.run_server(debug=True)
else:
plotly_fig.show()
# Function to display a backtest
def display_backtest(proposed_trades, completed_trades, win_objects, loss_objects, proposed_trades_table, indicators=[],
graph_title="Backtest"):
# Todo: add callback so that trailing stops can be explored
# Create a Dash Object
app = Dash(__name__)
# Construct view
app.layout = html.Div(children=[
html.H1(graph_title),
html.Div([
html.H1(children="Strategy With Trades"),
html.Div(children='''Original Strategy'''),
dcc.Graph(
id="strat_with_trades",
figure=completed_trades,
style={'height': '100vh'}
)
]),
html.Div([
html.H1(children="Strategy With Proposed Trades"),
html.Div(children='''Original Strategy'''),
dcc.Graph(
id="display_trades",
figure=proposed_trades
)
]),
html.Div([
html.H1(children="Proposed Trades Table"),
html.Div(children='''Original Strategy'''),
dcc.Graph(
id="table_trades",
figure=proposed_trades_table
)
]),
html.Div([
html.H1(children="Win Objects"),
html.Div(children='''Win Objects'''),
dcc.Graph(
id="win_objects",
figure=win_objects
)
]),
html.Div([
html.H1(children="Loss Objects"),
html.Div(children='''Loss Objects'''),
dcc.Graph(
id="loss_objects",
figure=loss_objects
)
])
])
app.run_server(debug=True)
# Function to construct base candlestick graph
def construct_base_candlestick_graph(dataframe, candlestick_title):
"""
Function to construct base candlestick graph
:param candlestick_title: String
:param dataframe: Pandas dataframe object
:return: plotly figure
"""
# Construct the figure
fig = go.Figure(data=[go.Candlestick(
x=dataframe['human_time'],
open=dataframe['open'],
high=dataframe['high'],
close=dataframe['close'],
low=dataframe['low'],
name=candlestick_title
)])
# Return the graph object
return fig
# Function to add a histogram to a plot
def add_histogram_to_graph(base_fig, dataframe, dataframe_column, histogram_name):
"""
Function to add a histogram to an existing plotly figure
:param base_fig: plotly figure object
:param dataframe: pandas dataframe
:param dataframe_column: string of column to plot
:param histogram_name: string title of histogram
:return: updated plotly figure
"""
# Construct trace
base_fig.add_trace(go.Histogram(
x=dataframe['human_time'],
y=dataframe[dataframe_column],
name=histogram_name
))
# Return the object
return base_fig
# Function to add a bar chart to plot
def add_bar_to_graph(base_fig, dataframe, dataframe_column, bar_name, layer=False, candlestick_title=""):
"""
Function to add a bar chart to an existing plotly figure
:param base_fig: plotly figure object
:param dataframe: pandas dataframe
:param dataframe_column: string of column to plot
:param bar_name: string title of bar chart
:return: updated plotly figure
"""
if layer:
# Create a new figure
fig2 = make_subplots(specs=[[{"secondary_y": True}]])
# Add the raw candlesticks
fig2 = fig2.add_trace(
go.Candlestick(
x=dataframe['human_time'],
open=dataframe['open'],
high=dataframe['high'],
close=dataframe['close'],
low=dataframe['low'],
name=candlestick_title
),
secondary_y=False
)
# Construct trace
fig2.add_trace(
go.Bar(
x=dataframe['human_time'],
y=dataframe[dataframe_column],
name=bar_name,
),
secondary_y=True
)
return fig2
else:
# Construct trace
base_fig.add_trace(go.Bar(
x=dataframe['human_time'],
y=dataframe[dataframe_column],
name=bar_name
))
# Return the object
return base_fig
# Function to display a MACD indicator
def display_macd_indicator(dataframe, title):
"""
Function to display a MACD indicator
:param dataframe: dataframe with all values
:param title: Title of the data
:return: figure with all data
"""
# Set up the figure
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add in the candlesticks for the original data
fig = fig.add_trace(
go.Candlestick(
x=dataframe['human_time'],
open=dataframe['open'],
high=dataframe['high'],
close=dataframe['close'],
low=dataframe['low'],
name=title
),
secondary_y=False
)
# Add in the MACD line
fig = fig.add_trace(
go.Scatter(
x=dataframe['human_time'],
y=dataframe['macd'],
name="MACD"
),
secondary_y=True
)
# Add in the MACD signal line
fig = fig.add_trace(
go.Scatter(
x=dataframe['human_time'],
y=dataframe['macd_signal'],
name="MACD Signal"
),
secondary_y=True
)
# Add in the MACD histogram
fig = fig.add_trace(
go.Bar(
x=dataframe['human_time'],
y=dataframe['macd_histogram'],
name="MACD Histogram"
),
secondary_y=True
)
return fig
# Function to display a RSI indicator
def display_rsi_indicator(dataframe, title):
"""
Function to display a RSI indicator
:param dataframe: dataframe with price data and rsi
:param title: Symbol for title
:return: figure with all data
"""
# Set up the figure
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add in the candlesticks for the original data
fig = fig.add_trace(
go.Candlestick(
x=dataframe['human_time'],
open=dataframe['open'],
high=dataframe['high'],
close=dataframe['close'],
low=dataframe['low'],
name=title
),
secondary_y=False
)
# Add in the RSI line
fig = fig.add_trace(
go.Scatter(
x=dataframe['human_time'],
y=dataframe['rsi'],
name="RSI"
),
secondary_y=True
)
return fig
# Function to add a line trace to a plot
def add_line_to_graph(base_fig, dataframe, dataframe_column, line_name):
"""
Function to add a line to trace to an existing figure
:param base_fig: plotly figure object
:param dataframe: pandas dataframe
:param dataframe_column: string of column to plot
:param line_name: string title of line trace
:return: updated plotly figure
"""
# Construct trace
base_fig.add_trace(go.Scatter(
x=dataframe['human_time'],
y=dataframe[dataframe_column],
name=line_name
))
# Return the object
return base_fig
# Function to display points on graph as diamond
def add_markers_to_graph(base_fig, dataframe, value_column, point_names):
"""
Function to add points to a graph
:param base_fig: plotly figure
:param dataframe: pandas dataframe
:param value_column: value for Y display
:param point_names: what's being plotted
:return: updated plotly figure
"""
# Construct trace
base_fig.add_trace(go.Scatter(
mode="markers",
marker=dict(size=8, symbol="diamond"),
x=dataframe['human_time'],
y=dataframe[value_column],
name=point_names
))
return base_fig
# Function to turn a dataframe into a table
def add_proposed_trades_to_graph(dataframe):
fig = go.Figure(data=[go.Table(
header=dict(values=["Time", "Order Type", "Stop Price", "Stop Loss", "Take Profit"], align='left'),
cells=dict(values=[
dataframe['human_time'],
dataframe['order_type'],
dataframe['stop_price'],
dataframe['stop_loss'],
dataframe['take_profit']
])
)]
)
return fig
# Function to add trades to graph
def add_trades_to_graph(proposed_trades_dataframe, base_fig):
# Create a point plot list
point_plot = []
# Create the colors
buy_color = "blue"
sell_color = "purple"
# Add each set of trades
for index, row in proposed_trades_dataframe.iterrows():
# Set the colour
if row['order_type'] == "BUY_STOP":
color = buy_color
else:
color = sell_color
# Add in the stop loss and take profit
base_fig.add_shape(
type="rect",
x0=row['human_time'],
y0=row['stop_loss'],
x1=row['cancel_time'],
y1=row['take_profit'],
line=dict(
color=color,
width=2
)
)
"""
# Add in the stop price
base_fig.add_shape(
type="line",
x0=row['human_time'],
x1=row['human_time'],
y0=row['stop_price'],
line=dict(
color=color,
width=2
)
)
"""
base_fig.update_layout(
xaxis_rangeslider_visible=False,
autosize=True,
)
return base_fig
# Function to add proposed trades to graph
def proposed_trades_graph(raw_candlesticks, proposed_trades):
# Plot the underlying candlesticks
fig = construct_base_candlestick_graph(raw_candlesticks, "Proposed Trades")
# Overlay the trades
fig = add_trades_to_graph(proposed_trades, fig)
# Return the figure
return fig
# Function to add display backtest outcomes
def completed_trades(raw_candles, backtest_results):
# Construct the base trade results
fig = construct_base_candlestick_graph(raw_candles, "Completed Trades")
# Add in layout features for each plotly figure
fig.update_layout(
xaxis_rangeslider_visible=False,
autosize=True,
)
fig.update_yaxes(automargin=True)
# Extract the winning Trades
winning_trades = backtest_results['win_objects']
# Iterate through each winning trade and add it to the plot
for index, trade in enumerate(winning_trades):
if index == 0:
fig = fig.add_trace(
go.Scatter(
x=[trade['trade_open_time'], trade['closing_time']],
y=[trade['closing_stop_price'], trade['closing_price']],
name="Winning Trades",
legendgroup="winning_trades",
line=dict(color="blue")
)
)
else:
fig = fig.add_trace(
go.Scatter(
x=[trade['trade_open_time'], trade['closing_time']],
y=[trade['closing_stop_price'], trade['closing_price']],
name="Winning Trades",
legendgroup="winning_trades",
line=dict(color="blue"),
showlegend=False
)
)
# Extract the losing Trades
losing_trades = backtest_results['loss_objects']
# Iterate through each losing trade and add it to the plot
for index, trade in enumerate(losing_trades):
if index == 0:
fig = fig.add_trace(
go.Scatter(
x=[trade['trade_open_time'], trade['closing_time']],
y=[trade['closing_stop_price'], trade['closing_price']],
name="Losing Trades",
legendgroup="losing_trades",
line=dict(color="red")
)
)
else:
fig = fig.add_trace(
go.Scatter(
x=[trade['trade_open_time'], trade['closing_time']],
y=[trade['closing_stop_price'], trade['closing_price']],
name="Losing Trades",
legendgroup="losing_trades",
line=dict(color="red"),
showlegend=False
)
)
# Return the figure
return fig
# Convert a dataframe into a table
def dataframe_to_table(dataframe, title):
"""
Function to convert a dataframe into a table
:param dataframe: dataframe of the data to be plotted
:param title: string of the title of the dataframe
:return: figure with table
"""
# Create the figure
fig = go.Figure(data=[go.Table(
header=dict(values=list(dataframe.columns), align='left'),
cells=dict(values=[dataframe[col] for col in dataframe.columns], align='left')
)])
# Add in the title
fig.update_layout(title_text=title)
# Return the figure
return fig