-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockSimulation.py
More file actions
494 lines (408 loc) · 17 KB
/
Copy pathBlockSimulation.py
File metadata and controls
494 lines (408 loc) · 17 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
from MarketMaker import GaussianMarketMaker
from Traders import EvenTrader, IntelligentTrader, RandomTrader
from Simulation import Simulation
import matplotlib.pyplot as plt
from matplotlib import ticker
import pandas as pd
import numpy as np
from tqdm import tqdm
class RunMultipleSimulations(Simulation):
def __init__(
self, traders, market_maker, number_of_iteration=300, number_of_runs=100
):
super().__init__(traders, market_maker, number_of_iteration=number_of_iteration)
self.number_of_runs = number_of_runs
self.names = []
for trader in self.traders:
self.names.append(trader.name)
self.concatenated_results = pd.DataFrame(columns=self.names)
self.concatenated_std = pd.DataFrame(columns=self.names)
self.results_min_assets = {}
self.results_max_assets = {}
self.results_avg_assets = {}
self.results_std_assets = {}
self.volume_traded = {}
self.results_assets_history = {}
self.results = {}
for name in self.names:
try:
self.results[name] = pd.DataFrame(
columns=[
"Minimum Asset",
"Maximum Asset",
"Average Asset",
"Standard Deviation Asset",
]
)
except ValueError:
breakpoint()
self.results_min_assets[name] = []
self.results_max_assets[name] = []
self.results_avg_assets[name] = []
self.results_std_assets[name] = []
self.volume_traded[name] = []
self.results_assets_history[name] = []
self.results["Market Maker"] = pd.DataFrame(
columns=[
"Minimum Asset",
"Maximum Asset",
"Average Asset",
"Standard Deviation Asset",
]
)
self.results_min_assets["Market Maker"] = []
self.results_max_assets["Market Maker"] = []
self.results_avg_assets["Market Maker"] = []
self.results_std_assets["Market Maker"] = []
def process_simulation_results(self):
"""
Process the results of the simulation.
"""
self.display_concatenated_history_of_assets(display=False)
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter("Outputs.xlsx", engine="xlsxwriter")
# Write each dataframe to a different worksheet.
for name in self.names:
self.results[name].to_excel(writer, sheet_name=f"Stats {name}")
self.results["Market Maker"].to_excel(
writer, sheet_name="Statistiques du Market Maker"
)
self.concatenated_results.to_excel(
writer, sheet_name="Historique actifs agreges (moy)"
)
self.concatenated_std.to_excel(
writer, sheet_name="Historique actifs agreges (e-t)"
)
# Close the Pandas Excel writer and output the Excel file.
writer.close()
return
def run_multiple_simulations(self):
"""
Runs the simulations and gathers all results.
"""
for run_number in range(self.number_of_runs):
results = self.run()
for index, name in enumerate(self.names):
self.results_min_assets[name].append(results[0][index][0])
self.results_max_assets[name].append(results[0][index][1])
self.results_avg_assets[name].append(results[0][index][2])
self.results_std_assets[name].append(results[0][index][3])
self.volume_traded[name].append(results[0][index][4])
self.results_assets_history[name].append(results[2][index])
self.results_min_assets["Market Maker"].append(results[1][0][0])
self.results_max_assets["Market Maker"].append(results[1][0][1])
self.results_avg_assets["Market Maker"].append(results[1][0][2])
self.results_std_assets["Market Maker"].append(results[1][0][3])
self.results["Market Maker"]["Minimum Asset"] = self.results_min_assets[
"Market Maker"
]
self.results["Market Maker"]["Maximum Asset"] = self.results_max_assets[
"Market Maker"
]
self.results["Market Maker"]["Average Asset"] = self.results_avg_assets[
"Market Maker"
]
self.results["Market Maker"][
"Standard Deviation Asset"
] = self.results_std_assets["Market Maker"]
for name in self.names:
try:
self.results[name]["Minimum Asset"] = self.results_min_assets[name]
self.results[name]["Maximum Asset"] = self.results_max_assets[name]
self.results[name]["Average Asset"] = self.results_avg_assets[name]
self.results[name][
"Standard Deviation Asset"
] = self.results_std_assets[name]
except ValueError:
breakpoint()
return
def display_concatenated_history_of_assets(self, display=True):
complete_matrix = {}
for index, name in enumerate(self.names):
matrix_history = np.array([])
for index2, array_to_append in enumerate(self.results_assets_history[name]):
if matrix_history.shape[0] == 0:
matrix_history = array_to_append
else:
try:
matrix_history = np.vstack((matrix_history, array_to_append))
except ValueError:
matrix_history = np.vstack(
(matrix_history, array_to_append[:-1])
)
complete_matrix[name] = matrix_history
if display:
plt.figure(figsize=(15, 8))
ymin = float("inf")
ymax = -float("inf")
colours = ["b", "g", "r", "c", "m"]
for index, name in enumerate(self.names):
concat_asset_history_mean = np.mean(complete_matrix[name], axis=1)
concat_asset_history_std = np.std(complete_matrix[name], axis=1)
argmax = np.argmax(concat_asset_history_mean)
argmin = np.argmin(concat_asset_history_mean)
ymin = min(
concat_asset_history_mean[argmin]
- concat_asset_history_std[argmin]
- 20,
0,
ymin,
)
ymax = max(
(
concat_asset_history_mean[argmax]
+ concat_asset_history_std[argmax]
)
* 1.1,
ymax,
)
plt.ylim(ymin, ymax)
plt.plot(
concat_asset_history_mean,
label=f"Asset History of the {name}",
color=colours[index],
linewidth=1.8,
)
plt.fill_between(
range(len(concat_asset_history_mean)),
concat_asset_history_mean,
concat_asset_history_mean + concat_asset_history_std,
alpha=0.25,
color=colours[index],
)
plt.fill_between(
range(len(concat_asset_history_mean)),
concat_asset_history_mean,
concat_asset_history_mean - concat_asset_history_std,
alpha=0.25,
color=colours[index],
)
plt.legend()
plt.plot(
[0, len(concat_asset_history_mean)],
[1000, 1000],
"r--",
linewidth=2.5,
label="Break even for Traders",
)
title_name = f"Mean Asset history over {self.number_of_runs} simulations"
plt.title(title_name, fontsize=20, fontweight="bold")
plt.xlabel("Iteration number", fontsize=16, fontweight="bold")
plt.ylabel("Assets", fontsize=17, fontweight="bold")
plt.grid()
plt.legend()
plt.show()
return
else:
for index, name in enumerate(self.names):
self.concatenated_results[name] = np.mean(complete_matrix[name], axis=1)
self.concatenated_std[name] = np.std(complete_matrix[name], axis=1)
print(self.concatenated_results)
print(self.concatenated_std)
return
def display_multiple_complete_results(
self,
views=(
((13, 100), (13, 125), (13, 150), (13, 170)),
((31, 53), (1, 90), (-91, 180), (0, 179)),
),
):
"""
Displays multiple 3D views of the results
"""
for view in views:
self.display_multiple_complete_results_inner_function(views=view)
return
def display_multiple_complete_results_inner_function(
self, views=((13, 100), (13, 125), (13, 150), (13, 170))
):
"""
Displays multiple views of the 3D plot
"""
fig = plt.figure(figsize=(16, 15))
ax1 = fig.add_subplot(2, 2, 1, projection="3d")
ax2 = fig.add_subplot(2, 2, 2, projection="3d")
ax3 = fig.add_subplot(2, 2, 3, projection="3d")
ax4 = fig.add_subplot(2, 2, 4, projection="3d")
axs = [ax1, ax2, ax3, ax4]
for index, ax in enumerate(axs):
ax.set_xlabel("Volume Traded", fontweight="bold", fontsize=10)
ax.set_ylabel("Standard Deviation", fontweight="bold", fontsize=10)
ax.set_zlabel("Average Assets", fontweight="bold", fontsize=10)
markers = ["o", "s", "x", ">"]
for index2, name in enumerate(self.names):
ax.scatter3D(
self.volume_traded[name],
self.results_std_assets[name],
self.results_avg_assets[name],
marker=markers[index2],
s=15,
)
ax.legend(
self.names,
title=f"Different Trading Strategies",
loc="best",
prop={"size": 8},
fontsize=9,
)
elevation = views[index][0]
azim = views[index][1]
ax.grid(True)
ax.view_init(elev=elevation, azim=azim)
# plt.title(f'Results over {self.number_of_runs} Runs', fontweight='bold', fontsize=11)
plt.show()
def display_complete_results(self, elevation=13, azim=136, figsize=(5, 4)):
"""
Displays the results of multiple simulations.
"""
plt.figure(figsize=figsize)
ax = plt.axes(projection="3d")
ax.set_xlabel("Volume Traded", fontweight="bold", fontsize=15)
ax.set_ylabel("Standard Deviation", fontweight="bold", fontsize=15)
ax.set_zlabel("Average Assets", fontweight="bold", fontsize=15)
markers = ["o", "s", "x", ">"]
for index, name in enumerate(self.names):
ax.scatter3D(
self.volume_traded[name],
self.results_std_assets[name],
self.results_avg_assets[name],
marker=markers[index],
s=40,
)
ax.legend(
self.names,
title=f"Different Trading Strategies",
loc="best",
prop={"size": 12},
fontsize=23,
)
ax.grid(True)
ax.view_init(elev=elevation, azim=azim)
plt.title(
f"Results over {self.number_of_runs} Runs", fontweight="bold", fontsize=23
)
plt.show()
return
def display_price_slice(
self,
max_result=False,
min_result=False,
average_result=True,
std_result=False,
average_result_log=False,
nb_of_replicates=20,
avg_samples=100,
std_samples=100,
cap=5000,
):
"""
Understand what characteristics are best for the Market Maker
"""
plt.rcParams.update(plt.rcParamsDefault)
avg_min = 10
avg_max = 60
std_min = 0
std_max = 5
avgs = np.linspace(avg_min, avg_max, avg_samples)
stds = np.linspace(std_min, std_max, std_samples)
results_array = np.zeros((avg_samples, std_samples))
for i, avg in tqdm(enumerate(avgs)):
for j, std in enumerate(stds):
self.market_maker = GaussianMarketMaker(std=std, mid_price=avg)
res = []
for rep in range(nb_of_replicates):
res.append(self.run())
results_mm_min = 0
results_mm_max = 0
results_mm_avg = 0
results_mm_std = 0
# res = [ [[ (min, max, avg, std), ..., ], [(min, max, avg, std)]],
# [[ (min, max, avg, std), ..., ], [(min, max, avg, std)]] ]
for item in res:
results_mm_min += item[1][0][0] / len(res)
results_mm_max += item[1][0][1] / len(res)
results_mm_avg += item[1][0][2] / len(res)
results_mm_std += item[1][0][3] / len(res)
if average_result:
if results_mm_avg < -cap:
results_mm_avg = (
results_array[i - 1, j] / 2 + results_array[i, j - 1] / 2
)
if results_mm_avg > cap:
results_mm_avg = (
results_array[i - 1, j] / 2 + results_array[i, j - 1] / 2
)
results_array[i, j] = results_mm_avg
title_name = f"Average Asset for the Gaussian Market Maker\n"
elif max_result:
if results_mm_max > cap:
results_mm_max = (
results_array[i - 1, j] / 2 + results_array[i, j - 1] / 2
)
results_array[i, j] = results_mm_max
title_name = f"Maximum Asset for the Gaussian Market Maker\n"
elif min_result:
if results_mm_min < -cap:
results_mm_min = -cap
results_array[i, j] = results_mm_min
title_name = f"Minimum Asset for the Gaussian Market Maker\n"
elif std_result:
if results_mm_std < -cap:
results_mm_std = (
results_array[i - 1, j] / 2 + results_array[i, j - 1] / 2
)
if results_mm_std > cap:
results_mm_std = (
results_array[i - 1, j] / 2 + results_array[i, j - 1] / 2
)
results_array[i, j] = results_mm_std
title_name = f"Standard Deviation for the Gaussian Market Maker\n"
plt.contourf(avgs, stds, results_array.T, cmap="cividis", levels=1000)
plt.colorbar()
hfont = {"fontname": "Helvetica"}
plt.title(title_name, fontsize=13, fontweight="bold", **hfont)
plt.xlabel("Average Price", fontsize=10, fontweight="bold", **hfont)
plt.ylabel("Standard Deviation", fontsize=10, fontweight="bold", **hfont)
plt.show()
if average_result_log:
title_name = (
f"Best hyper-parameters for the Gaussian Market Maker (log scale)\n"
)
plt.contourf(
avgs,
stds,
results_array.T,
locator=ticker.LogLocator(),
cmap="cividis",
levels=200,
)
plt.colorbar()
plt.title(title_name, fontsize=13, fontweight="bold", **hfont)
plt.xlabel("Average Price", fontsize=10, fontweight="bold", **hfont)
plt.ylabel("Standard Deviation", fontsize=10, fontweight="bold", **hfont)
plt.show()
return
if __name__ == "__main__":
# Create Traders
my_even_trader = EvenTrader(1000)
my_intelligent_trader = IntelligentTrader(1000)
my_random_trader = RandomTrader(1000)
traders = [my_intelligent_trader, my_random_trader, my_even_trader]
# Create Market Maker
my_market_maker = GaussianMarketMaker()
# Create Simulation
my_multiple_simulations = RunMultipleSimulations(
traders, my_market_maker, number_of_iteration=100
)
if False:
my_multiple_simulations.display_price_slice()
# Run and Display results
if False:
my_multiple_simulations.run_multiple_simulations()
my_multiple_simulations.display_multiple_complete_results()
if False:
my_multiple_simulations.run_multiple_simulations()
my_multiple_simulations.display_concatenated_history_of_assets()
if True:
my_multiple_simulations.run_multiple_simulations()
my_multiple_simulations.process_simulation_results()