-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_simulations.py
238 lines (217 loc) · 9.57 KB
/
test_simulations.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
import os
import pytest
import numpy as np
import pandas as pd
from skopt.space import Integer
from labquant import LabQuant
current_dir = os.path.dirname(__file__)
csv_path = os.path.join(current_dir, "ohlcv_data.csv")
df = pd.read_csv(csv_path)
# Main Strategy
def strategy(args):
df = args[0].copy()
amount = args[1]
df["SMA1"] = df.c.rolling(args[2]).mean()
df["SMA2"] = df.c.rolling(args[3]).mean()
df["pred"] = np.where(df.SMA1 > df.SMA2, amount, -amount)
return df
amount = 1
str_params = [df, amount, 44, 120]
data = strategy(str_params)
@pytest.fixture(scope="class")
def lab_inst():
# Instance creation
lab_instance = LabQuant(data, seed=1, show_roi=False)
lab_instance.test_ = True
lab_instance.start(
strategy=strategy,
str_params=str_params,
mc_nsim=10,
mc_nsteps=len(df),
sim_taskmode='process',
)
# Returns the instance to the test
yield lab_instance
# Del instance
del lab_instance
class TestLab3:
def test_show_monte_carlo_simulation_1(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_prices_price_base"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 8.2157
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -4.253
def test_show_monte_carlo_simulation_2(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_prices_black_scholes"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 25.0994
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -2.4047
def test_show_monte_carlo_simulation_3(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_prices_merton_jump_diffusion"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 24.3394
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -2.6189
def test_show_monte_carlo_simulation_4(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_positions"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 9.8833
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -2.4916
def test_show_monte_carlo_simulation_5(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_returns"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 12.9769
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -1.7261
def test_show_monte_carlo_simulation_6(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_returns_with_replacement"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 10.299
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -2.7573
def test_show_monte_carlo_simulation_7(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_endings_positions"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 13.2798
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -0.5622
def test_show_monte_carlo_simulation_8(self, lab_inst):
lab_inst.showplt6 = 1
lab_inst.mc_mode = "random_startings_positions"
lab_inst._show_monte_carlo_simulation()
while True:
if lab_inst.value_var_mc.value == 1:
lab_inst.value_var_mc.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_1[:][:, -1])), 4) == 13.1867
assert round(sum(np.array(lab_inst.np_mem_2[:][:, -1])), 4) == -0.7773
def test_show_params_simulation_1_process(self, lab_inst):
lab_inst.showplt12 = 1
lab_inst.sim_method = "grid"
lab_inst.sim_taskmode="process"
lab_inst.sim_params={"param1": [10, 20, 30], "param2": [60, 65, 70]}
lab_inst._show_hypparams_simulation()
while True:
if lab_inst.value_var_sim.value == 1:
lab_inst.value_var_sim.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_3[:][:, -1])), 4) == -145.0846
assert round(sum(np.array(lab_inst.np_mem_4[:][:, -1])), 4) == 10.7497
assert round(sum(lab_inst.sim_params_queue.get()[:, -1]), 4) == 585.0
def test_show_params_simulation_2_process(self, lab_inst):
lab_inst.showplt12 = 1
lab_inst.sim_method = "random"
lab_inst.sim_taskmode="process"
lab_inst.sim_nrandsims=5
lab_inst.sim_params={"param1": [5, 10, 15, 20, 25, 30],
"param2": [40, 45, 50, 55, 60, 65, 70]}
lab_inst._show_hypparams_simulation()
while True:
if lab_inst.value_var_sim.value == 1:
lab_inst.value_var_sim.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_3[:][:, -1])), 4) == -127.0669
assert round(sum(np.array(lab_inst.np_mem_4[:][:, -1])), 4) == 5.379
assert round(sum(lab_inst.sim_params_queue.get()[:, -1]), 4) == 270.0
def test_show_params_simulation_3_process(self, lab_inst):
lab_inst.showplt12 = 1
lab_inst.sim_method = "bayesian-opt"
lab_inst.sim_taskmode="process"
lab_inst.sim_bayesopt_ncalls = 10
lab_inst.sim_bayesopt_kwargs = {
"acq_func": "gp_hedge",
"acq_optimizer": "auto",
"verbose": "True",
}
lab_inst.sim_bayesopt_spaces = [
Integer(2, 50, name="sma_1_in"),
Integer(51, 150, name="sma_2_in"),
]
lab_inst._show_hypparams_simulation()
while True:
if lab_inst.value_var_sim.value == 1:
lab_inst.value_var_sim.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_3[:][:, -1])), 4) == -118.1221
assert round(sum(np.array(lab_inst.np_mem_4[:][:, -1])), 4) == 12.4702
assert round(sum(lab_inst.sim_params_queue.get()[:, -1]), 4) == 1175.0
def test_show_params_simulation_1_thread(self, lab_inst):
lab_inst.showplt12 = 1
lab_inst.sim_method = "grid"
lab_inst.sim_taskmode="thread"
lab_inst.sim_params={"param1": [10, 20, 30], "param2": [60, 65, 70]}
lab_inst._show_hypparams_simulation()
while True:
if lab_inst.value_var_sim.value == 1:
lab_inst.value_var_sim.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_3[:][:, -1])), 4) == -145.0846
assert round(sum(np.array(lab_inst.np_mem_4[:][:, -1])), 4) == 10.7497
assert round(sum(lab_inst.sim_params_queue.get()[:, -1]), 4) == 585.0
def test_show_params_simulation_2_thread(self, lab_inst):
lab_inst.showplt12 = 1
lab_inst.sim_method = "random"
lab_inst.sim_taskmode="thread"
lab_inst.sim_nrandsims=5
lab_inst.sim_params={"param1": [5, 10, 15, 20, 25, 30],
"param2": [40, 45, 50, 55, 60, 65, 70]}
lab_inst._show_hypparams_simulation()
while True:
if lab_inst.value_var_sim.value == 1:
lab_inst.value_var_sim.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_3[:][:, -1])), 4) == -127.0669
assert round(sum(np.array(lab_inst.np_mem_4[:][:, -1])), 4) == 5.379
assert round(sum(lab_inst.sim_params_queue.get()[:, -1]), 4) == 270.0
def test_show_params_simulation_3_thread(self, lab_inst):
lab_inst.showplt12 = 1
lab_inst.sim_method = "bayesian-opt"
lab_inst.sim_taskmode="thread"
lab_inst.sim_bayesopt_ncalls = 10
lab_inst.sim_bayesopt_kwargs = {
"acq_func": "gp_hedge",
"acq_optimizer": "auto",
"verbose": "True",
}
lab_inst.sim_bayesopt_spaces = [
Integer(2, 50, name="sma_1_in"),
Integer(51, 150, name="sma_2_in"),
]
lab_inst._show_hypparams_simulation()
while True:
if lab_inst.value_var_sim.value == 1:
lab_inst.value_var_sim.value = 0
break
assert round(sum(np.array(lab_inst.np_mem_3[:][:, -1])), 4) == -163.2657
assert round(sum(np.array(lab_inst.np_mem_4[:][:, -1])), 4) == 11.509
assert round(sum(lab_inst.sim_params_queue.get()[:, -1]), 4) == 914