forked from enarjord/passivbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
315 lines (299 loc) · 13.4 KB
/
plotting.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
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
import json
import re
import matplotlib.pyplot as plt
import pandas as pd
from colorama import init, Fore
from prettytable import PrettyTable
from njit_funcs import round_up
from procedures import dump_live_config
from pure_funcs import round_dynamic, denumpyize
def dump_plots(
result: dict,
longs: pd.DataFrame,
shorts: pd.DataFrame,
sdf: pd.DataFrame,
df: pd.DataFrame,
n_parts: int = None,
):
init(autoreset=True)
plt.rcParams["figure.figsize"] = [29, 18]
try:
pd.set_option("display.precision", 10)
except Exception as e:
print("error setting pandas precision", e)
table = PrettyTable(["Metric", "Value"])
table.align["Metric"] = "l"
table.align["Value"] = "l"
table.title = "Summary"
table.add_row(["Exchange", result["exchange"] if "exchange" in result else "unknown"])
table.add_row(["Market type", result["market_type"] if "market_type" in result else "unknown"])
table.add_row(["Symbol", result["symbol"] if "symbol" in result else "unknown"])
table.add_row(["No. days", round_dynamic(result["result"]["n_days"], 6)])
table.add_row(["Starting balance", round_dynamic(result["result"]["starting_balance"], 6)])
for side in ["long", "short"]:
if result[side]["enabled"]:
table.add_row([" ", " "])
table.add_row([side.capitalize(), result[side]["enabled"]])
adg_realized_per_exp = result["result"][f"adg_realized_per_exposure_{side}"]
table.add_row(
["ADG realized per exposure", f"{round_dynamic(adg_realized_per_exp * 100, 3)}%"]
)
profit_color = (
Fore.RED
if result["result"][f"final_balance_{side}"] < result["result"]["starting_balance"]
else Fore.RESET
)
table.add_row(
[
"Final balance",
f"{profit_color}{round_dynamic(result['result'][f'final_balance_{side}'], 6)}{Fore.RESET}",
]
)
table.add_row(
[
"Final equity",
f"{profit_color}{round_dynamic(result['result'][f'final_equity_{side}'], 6)}{Fore.RESET}",
]
)
table.add_row(
[
"Net PNL + fees",
f"{profit_color}{round_dynamic(result['result'][f'net_pnl_plus_fees_{side}'], 6)}{Fore.RESET}",
]
)
table.add_row(
[
"Total gain",
f"{profit_color}{round_dynamic(result['result'][f'gain_{side}'] * 100, 4)}%{Fore.RESET}",
]
)
table.add_row(
[
"Average daily gain",
f"{profit_color}{round_dynamic((result['result'][f'adg_{side}']) * 100, 3)}%{Fore.RESET}",
]
)
table.add_row(
[
"Loss to profit ratio",
f"{round_dynamic(result['result'][f'loss_profit_ratio_{side}'], 4)}",
]
)
table.add_row(
[
f"Price action distance mean",
round_dynamic(result["result"][f"pa_distance_mean_{side}"], 6),
]
)
table.add_row(
[
f"Price action distance std",
round_dynamic(result["result"][f"pa_distance_std_{side}"], 6),
]
)
table.add_row(
[
f"Price action distance max",
round_dynamic(result["result"][f"pa_distance_max_{side}"], 6),
]
)
table.add_row(
[
"Closest bankruptcy",
f'{round_dynamic(result["result"][f"closest_bkr_{side}"] * 100, 4)}%',
]
)
table.add_row(
[
"Lowest equity/balance ratio",
f'{round_dynamic(result["result"][f"eqbal_ratio_min_{side}"], 4)}',
]
)
table.add_row(
[
"Equity/balance ratio std",
f'{round_dynamic(result["result"][f"equity_balance_ratio_std_{side}"], 4)}',
]
)
table.add_row(["No. fills", result["result"][f"n_fills_{side}"]])
table.add_row(["No. entries", result["result"][f"n_entries_{side}"]])
table.add_row(["No. closes", result["result"][f"n_closes_{side}"]])
table.add_row(["No. initial entries", result["result"][f"n_ientries_{side}"]])
table.add_row(["No. reentries", result["result"][f"n_rentries_{side}"]])
table.add_row(["No. unstuck entries", result["result"][f"n_unstuck_entries_{side}"]])
table.add_row(["No. unstuck closes", result["result"][f"n_unstuck_closes_{side}"]])
table.add_row(["No. normal closes", result["result"][f"n_normal_closes_{side}"]])
table.add_row(
[
"Average n fills per day",
round_dynamic(result["result"][f"avg_fills_per_day_{side}"], 3),
]
)
table.add_row(
[
"Mean hours stuck",
round_dynamic(result["result"][f"hrs_stuck_avg_{side}"], 6),
]
)
table.add_row(
[
"Max hours stuck",
round_dynamic(result["result"][f"hrs_stuck_max_{side}"], 6),
]
)
profit_color = Fore.RED if result["result"][f"pnl_sum_{side}"] < 0 else Fore.RESET
table.add_row(
[
"PNL sum",
f"{profit_color}{round_dynamic(result['result'][f'pnl_sum_{side}'], 4)}{Fore.RESET}",
]
)
table.add_row(["Profit sum", round_dynamic(result["result"][f"profit_sum_{side}"], 4)])
table.add_row(["Loss sum", round_dynamic(result["result"][f"loss_sum_{side}"], 4)])
table.add_row(["Fee sum", round_dynamic(result["result"][f"fee_sum_{side}"], 4)])
table.add_row(["Biggest pos size", result["result"][f"biggest_psize_{side}"]])
table.add_row(
[
"Biggest pos cost",
round_dynamic(result["result"][f"biggest_psize_quote_{side}"], 4),
]
)
table.add_row(
["Volume quote", round_dynamic(result["result"][f"volume_quote_{side}"], 6)]
)
dump_live_config(result, result["plots_dirpath"] + "live_config.json")
json.dump(denumpyize(result), open(result["plots_dirpath"] + "result.json", "w"), indent=4)
print("writing backtest_result.txt...\n")
with open(f"{result['plots_dirpath']}backtest_result.txt", "w") as f:
output = table.get_string(border=True, padding_width=1)
print(output)
f.write(re.sub("\033\\[([0-9]+)(;[0-9]+)*m", "", output))
n_parts = n_parts if n_parts is not None else max(3, int(round_up(result["n_days"] / 14, 1.0)))
for side, fdf in [("long", longs), ("short", shorts)]:
if result[side]["enabled"]:
plt.clf()
fig = plot_fills(df, fdf, plot_whole_df=True, title=f"Overview Fills {side.capitalize()}")
if not fig:
continue
fig.savefig(f"{result['plots_dirpath']}whole_backtest_{side}.png")
print(f"\nplotting balance and equity {side}...")
plt.clf()
sdf[f"balance_{side}"].plot()
sdf[f"equity_{side}"].plot(
title=f"Balance and equity {side.capitalize()}", xlabel="Time", ylabel="Balance"
)
plt.savefig(f"{result['plots_dirpath']}balance_and_equity_sampled_{side}.png")
for z in range(n_parts):
start_ = z / n_parts
end_ = (z + 1) / n_parts
print(f"{side} {z} of {n_parts} {start_ * 100:.2f}% to {end_ * 100:.2f}%")
fig = plot_fills(
df,
fdf.iloc[int(len(fdf) * start_) : int(len(fdf) * end_)],
title=f"Fills {side} {z+1} of {n_parts}",
)
if fig is not None:
fig.savefig(f"{result['plots_dirpath']}backtest_{side}{z + 1}of{n_parts}.png")
else:
print(f"no {side} fills...")
print(f"plotting {side} initial entry band")
if "timestamp" in df.columns:
tick_interval = df.timestamp.iloc[1] - df.timestamp.iloc[0]
else:
tick_interval = df.index[1] - df.index[0]
spans_multiplier = 60 / (tick_interval / 1000)
spans = [
result[side]["ema_span_0"] * spans_multiplier,
((result[side]["ema_span_0"] * result[side]["ema_span_1"]) ** 0.5) * spans_multiplier,
result[side]["ema_span_1"] * spans_multiplier,
]
emas = pd.DataFrame(
{
str(span): df.iloc[::100]
.price.ewm(span=max(1.0, span / 100), adjust=False)
.mean()
for span in spans
}
)
ema_band_lower = emas.min(axis=1)
ema_band_upper = emas.max(axis=1)
if side == "long":
ientry_band = ema_band_lower * (1 - result[side]["initial_eprice_ema_dist"])
else:
ientry_band = ema_band_upper * (1 + result[side]["initial_eprice_ema_dist"])
plt.clf()
df.price.iloc[::100].plot(style="y-", title=f"{side.capitalize()} Initial Entry Band")
ientry_band.plot(style=f"{('b' if side == 'long' else 'r')}-.")
plt.savefig(f"{result['plots_dirpath']}initial_entry_band_{side}.png")
if result[side]["auto_unstuck_wallet_exposure_threshold"] != 0.0:
print(f"plotting {side} unstucking bands...")
unstucking_band_lower = ema_band_lower * (1 - result[side]["auto_unstuck_ema_dist"])
unstucking_band_upper = ema_band_upper * (1 + result[side]["auto_unstuck_ema_dist"])
plt.clf()
df.price.iloc[::100].plot(
style="y-", title=f"{side.capitalize()} Auto Unstucking Bands"
)
unstucking_band_lower.plot(style="b-.")
unstucking_band_upper.plot(style="r-.")
plt.savefig(f"{result['plots_dirpath']}auto_unstuck_bands_{side}.png")
print("plotting pos sizes...")
plt.clf()
sdf[["psize_long", "psize_short"]].plot(
title="Position size in terms of contracts",
xlabel="Time",
ylabel="Position size",
)
plt.savefig(f"{result['plots_dirpath']}psizes_plot.png")
def plot_fills(df, fdf_, side: int = 0, plot_whole_df: bool = False, title=""):
if fdf_.empty:
return
plt.clf()
fdf = fdf_.set_index("timestamp")
dfc = df # .iloc[::max(1, int(len(df) * 0.00001))]
if dfc.index.name != "timestamp":
dfc = dfc.set_index("timestamp")
if not plot_whole_df:
dfc = dfc[(dfc.index > fdf.index[0]) & (dfc.index < fdf.index[-1])]
dfc = dfc.loc[fdf.index[0] : fdf.index[-1]]
dfc.price.plot(style="y-", title=title, xlabel="Time", ylabel="Price + Fills")
if side >= 0:
longs = fdf[fdf.type.str.contains("long")]
lientry = longs[longs.type.str.contains("ientry")]
lrentry = longs[longs.type.str.contains("rentry")]
lnclose = longs[longs.type.str.contains("nclose")]
luentry = longs[longs.type.str.contains("unstuck_entry")]
luclose = longs[longs.type.str.contains("unstuck_close")]
ldca = longs[longs.type.str.contains("secondary")]
lientry.price.plot(style="b.")
lrentry.price.plot(style="b.")
lnclose.price.plot(style="r.")
ldca.price.plot(style="go")
luentry.price.plot(style="bx")
luclose.price.plot(style="rx")
# longs.where(longs.pprice != 0.0).pprice.fillna(method="ffill").plot(style="b--")
lppu = longs[(longs.pprice != longs.pprice.shift(1)) & (longs.pprice != 0.0)]
for i in range(len(lppu) - 1):
plt.plot(
[lppu.index[i], lppu.index[i + 1]], [lppu.pprice.iloc[i], lppu.pprice.iloc[i]], "b--"
)
if side <= 0:
shorts = fdf[fdf.type.str.contains("short")]
sientry = shorts[shorts.type.str.contains("ientry")]
srentry = shorts[shorts.type.str.contains("rentry")]
snclose = shorts[shorts.type.str.contains("nclose")]
suentry = shorts[shorts.type.str.contains("unstuck_entry")]
suclose = shorts[shorts.type.str.contains("unstuck_close")]
sdca = shorts[shorts.type.str.contains("secondary")]
sientry.price.plot(style="r.")
srentry.price.plot(style="r.")
snclose.price.plot(style="b.")
sdca.price.plot(style="go")
suentry.price.plot(style="rx")
suclose.price.plot(style="bx")
# shorts.where(shorts.pprice != 0.0).pprice.fillna(method="ffill").plot(style="r--")
sppu = shorts[(shorts.pprice != shorts.pprice.shift(1)) & (shorts.pprice != 0.0)]
for i in range(len(sppu) - 1):
plt.plot(
[sppu.index[i], sppu.index[i + 1]], [sppu.pprice.iloc[i], sppu.pprice.iloc[i]], "r--"
)
return plt