-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdisplay_lib.py
More file actions
230 lines (201 loc) · 6.55 KB
/
display_lib.py
File metadata and controls
230 lines (201 loc) · 6.55 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
from sql_lib import sql_interaction
import plotly.graph_objects as go
from dash import Dash, html, dcc
from plotly.subplots import make_subplots
# Function to retrieve back_test data and then display chart of close values
def show_data(table_name, dataframe, graph_name, project_settings):
# Table Name
# Get the data
dataframe = sql_interaction.retrieve_dataframe(table_name, project_settings)
# 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']
)])
fig.add_trace(go.Scatter(
x=dataframe['human_time'],
y=dataframe['ta_sma_200'],
name='ta_sma_200'
)
)
fig.add_trace(go.Scatter(
x=dataframe['human_time'],
y=dataframe['ta_ema_200'],
name='ta_ema_200'
))
fig.add_trace(go.Scatter(
x=dataframe['human_time'],
y=dataframe['ta_ema_15'],
name='ta_ema_15'
))
# Create Dash
app = Dash(__name__)
app.layout = html.Div(children=[
html.H1(children=graph_name),
html.Div("Example data"),
dcc.Graph(
id='Example Graph',
figure=fig
)
])
app.run_server(debug=True)
# Function to display a plotly graph in dash
def display_graph(plotly_fig, graph_title, dash=False, upload=False):
"""
Function to display a plotly graph using Dash
:param plotly_fig: plotly figure
:param graph_title: string
:return: None
"""
# Add in autoscaling for each plotly figure
plotly_fig.update_layout(
autosize=True
)
plotly_fig.update_yaxes(automargin=True)
plotly_fig.update_layout(xaxis_rangeslider_visible=False)
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 Creative Appnologies"),
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(original_strategy, strategy_with_trades, table, graph_title):
original_strategy.update_layout(
autosize=True
)
original_strategy.update_yaxes(automargin=True)
original_strategy.update_layout(xaxis_rangeslider_visible=False)
# 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=strategy_with_trades,
style={'height': '100vh'}
)
]),
html.Div([
html.H1(children="Table of Trades"),
html.Div(children='''Original Strategy'''),
dcc.Graph(
id="table_trades",
figure=table
)
])
])
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 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_dataframe(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(trades_dict, base_fig):
# Create a point plot list
point_plot = []
# Create the colors
buy_color = dict(color="green")
sell_color = dict(color="red")
# Add each set of trades
trades = trades_dict["full_trades"]
for trade in trades:
if trade['trade_outcome']['not_completed'] is False:
if trade['trade_type'] == "BUY_STOP":
color = buy_color
else:
color = sell_color
base_fig.add_trace(
go.Scatter(
x=[trade['order_time'], trade['open_time'], trade['close_time']],
y=[trade['order_price'], trade['open_price'], trade['close_price']],
name=trade['name'],
legendgroup=trade['trade_type'],
line=color
)
)
return base_fig
# Function to add a table of the strategy outcomes to Plotly