-
Notifications
You must be signed in to change notification settings - Fork 1
/
fitness.py
408 lines (345 loc) · 15.7 KB
/
fitness.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
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
from loader import load_indivs, load_results, load_quotes_time, load_symbol_properties
import numpy as np
import pandas as pd
import math
from numba import float64, int64, boolean, types, jit, njit
from numba import cuda
from collections import namedtuple
from multiprocessing import Pool
Quote = namedtuple("Quote", ["model_number", "model_time", "level_time", "level", "atr", "direction", "highs", "lows", "time"])
Model = namedtuple("Model", ["range_start", "range_end", "close_time", "model_line"])
class FitnessResult:
def __init__(self, results):
self.fitness, self.ratio, self.models = results
def __str__(self):
return "fitness: {}, ratio: {} models: {}".format(self.fitness, self.ratio, self.models)
class Fitness:
def __init__(self):
self.digits, self.spread = load_symbol_properties()
self.db_indivs = load_indivs().to_numpy()
self.db_results = []
self.quotes = []
self.params = None
self.quotes_time = [np.resize(np.array(quotes).astype(int), 400) for quotes in load_quotes_time()]
for result in load_results():
self.quotes.append(np.array(result[6:]).astype(float))
self.db_results.append(np.array(result[:6]).astype(float))
self.db_results = np.array(self.db_results)
self.quotes = np.array(self.quotes)
self.quotes_time = np.array(self.quotes_time)
def calc(self, indivs: list, gpu, multithreaded, compile):
trading_params = []
nontrading_params = []
for indiv in indivs:
trading_params.append( np.array([gene.value() for gene in indiv.get("trading")]) )
nontrading_params.append( np.array([[gene.value() - gene.radius(), gene.value() + gene.radius()] for gene in indiv.get("trading", has_tag=False)]) )
results = []
if compile:
for indiv_idx in range(len(indivs)):
results.append(evaluate_compile(self.db_indivs, self.db_results, self.quotes, self.quotes_time, trading_params[indiv_idx], nontrading_params[indiv_idx], self.spread, self.digits))
elif multithreaded:
pool = Pool()
results = pool.map(evaluate, [self.db_indivs, self.db_results, self.quotes, self.quotes_time, trading_params[indiv_idx][indiv_idx], nontrading_params[indiv_idx], self.spread, self.digits])
pool.close()
pool.join()
elif gpu:
raise Exception("Gpu not implemented")
else:
for indiv_idx in range(len(indivs)):
results.append(evaluate([self.db_indivs, self.db_results, self.quotes, self.quotes_time, trading_params[indiv_idx], nontrading_params[indiv_idx], self.spread, self.digits]))
for indiv_idx, indiv in enumerate(indivs):
indiv.fitness = FitnessResult(results[indiv_idx])
def evaluate(params):
db_indivs, db_results, quotes, quotes_time, trading_params, non_trading_params, spread, digits = params
entry_dist, stop, stop_out, take, bu, bu_cond, min_dist_betw_orders, max_stops, exp, min_tim_betw_odrs = trading_params
trade_list = []
use_bu = True
if bu > bu_cond:
use_bu = False
# setting parameters
result = 0
result_list = []
orders = 0
trades = 0
models = 0
model_indexes = []
models_id = []
model_stops = []
model_takes = []
model_bus = []
max_drawdown = 0
max_account_value = 0
open_models = [] # list with OpenModel objects
recently_opened_models = [] # list of model numbers
for mod_idx in range(len(db_indivs)):
model = db_indivs[mod_idx]
model_number, model_time, level_time, level, atr, direction = db_results[mod_idx]
quote = quotes[mod_idx]
highs = quote[:int(len(quote) / 2)]
lows = quote[int(len(quote) / 2):]
time = quotes_time[mod_idx]
fits = True
open_ord_cond_shift = -spread if direction else 0 # (-spread) if buy, (0) otherwise
close_ord_cond_shift = 0 if direction else -spread # (0) if buy, (-spread) otherwise
placed_order = False
is_opened = False # order is in trade now
exp_time = math.inf
stops = 0
placed_bu = False
have_take = False
closed_by_bu = False
time_after_prev_order = math.inf # initiates with math.inf to pass (time_after_prev_order < min_tim_betw_odrs) condition and place first order
time_after_order = 0 # time passed after placing an order and before opening it
if direction: # long model
entry_lvl = round(level - entry_dist * atr, digits)
bu_lvl = round(entry_lvl + bu * atr, digits)
bu_condition_level = entry_lvl + bu_cond * atr
take_lvl = round(entry_lvl + take * atr, digits)
stop_lvl = round(entry_lvl - stop * atr, digits)
stop_out_lvl = entry_lvl - stop_out * atr
else: # short model
entry_lvl = round(level + entry_dist * atr, digits)
bu_lvl = round(entry_lvl - bu * atr, digits)
bu_condition_level = entry_lvl - bu_cond * atr
take_lvl = round(entry_lvl - take * atr, digits)
stop_lvl = round(entry_lvl + stop * atr, digits)
stop_out_lvl = entry_lvl + stop_out * atr
# bringing buffers up to date
for open_model in open_models:
if model_time >= open_model.close_time:
open_models.remove(open_model)
# checking if this model should be counted
for opened_model in recently_opened_models:
if model_number == opened_model:
fits = False
for open_model in open_models:
if open_model.range_start <= entry_lvl <= open_model.range_end:
fits = False
for gene_idx in range(len(model)):
attr = model[gene_idx]
if non_trading_params[gene_idx][0] > attr or non_trading_params[gene_idx][1] < attr:
fits = False
break
if not fits:
continue
# evaluating model
models += 1
price_is_up = False
if highs[0] > entry_lvl + open_ord_cond_shift and lows[0] > entry_lvl + open_ord_cond_shift:
price_is_up = True
current_time = time[0]
for idx in range(len(highs)):
current_time = time[idx]
time_after_prev_order += 1
if (direction and lows[idx] <= stop_out_lvl) or (not direction and highs[idx] >= stop_out_lvl):
if is_opened:
stops += 1
break
if is_opened:
if use_bu and not placed_bu and \
((direction and highs[idx] >= bu_condition_level) or (
not direction and lows[idx] <= bu_condition_level)):
stop_lvl = bu_lvl
placed_bu = True
elif (direction and lows[idx] <= stop_lvl + close_ord_cond_shift) or (
not direction and highs[idx] >= stop_lvl + close_ord_cond_shift):
if placed_bu:
closed_by_bu = True
break
stops += 1
if stops >= max_stops:
break
is_opened = False
time_after_order = 0
placed_order = False
if highs[idx] > entry_lvl + open_ord_cond_shift and lows[idx] > entry_lvl + open_ord_cond_shift:
price_is_up = True
else:
price_is_up = False
elif (direction and highs[idx] >= take_lvl + close_ord_cond_shift) or (
not direction and lows[idx] <= take_lvl + close_ord_cond_shift):
have_take = True
break
if not is_opened:
if not placed_order and time_after_prev_order < min_tim_betw_odrs: # if 'True' then we can't open new order right now
continue
if not placed_order:
time_after_prev_order = 0
placed_order = True
if current_time >= exp_time:
break
if (price_is_up and lows[idx] <= entry_lvl + open_ord_cond_shift) or \
(not price_is_up and highs[idx] >= entry_lvl + open_ord_cond_shift):
is_opened = True
else:
time_after_order += 1
result -= stops * stop
if have_take:
result += take
elif closed_by_bu:
result += bu
result = round(result, 2)
if result > max_account_value:
max_account_value = result
drawdown = round(max_account_value - result, 2)
if drawdown > max_drawdown:
max_drawdown = drawdown
open_models.append(Model(
entry_lvl - min_dist_betw_orders * atr,
entry_lvl + min_dist_betw_orders * atr,
current_time,
mod_idx + 1
))
if len(recently_opened_models) >= 10:
recently_opened_models.pop(0)
recently_opened_models.append(model_number)
ratio = result / max_drawdown if max_drawdown != 0 else 0
fitness = result
return fitness, ratio, models
@njit
def evaluate_compile(db_indivs, db_results, quotes, quotes_time, trading_params, non_trading_params, spread, digits):
entry_dist, stop, stop_out, take, bu, bu_cond, min_dist_betw_orders, max_stops, exp, min_tim_betw_odrs = trading_params
#trade_list = []
use_bu = True
if bu > bu_cond:
use_bu = False
# setting parameters
result = 0
# result_list = []
# orders = 0
# trades = 0
models = 0
# model_indexes = []
# models_id = []
# model_stops = []
# model_takes = []
# model_bus = []
max_drawdown = 0
max_account_value = 0
open_models = [] # list with OpenModel objects
recently_opened_models = [] # list of model numbers
for mod_idx in range(len(db_indivs)):
model = db_indivs[mod_idx]
model_number, model_time, level_time, level, atr, direction = db_results[mod_idx]
quote = quotes[mod_idx]
highs = quote[:int(len(quote) / 2)]
lows = quote[int(len(quote) / 2):]
time = quotes_time[mod_idx]
fits = True
open_ord_cond_shift = -spread if direction else 0 # (-spread) if buy, (0) otherwise
close_ord_cond_shift = 0 if direction else -spread # (0) if buy, (-spread) otherwise
placed_order = False
is_opened = False # order is in trade now
exp_time = math.inf
stops = 0
placed_bu = False
have_take = False
closed_by_bu = False
time_after_prev_order = math.inf # initiates with math.inf to pass (time_after_prev_order < min_tim_betw_odrs) condition and place first order
time_after_order = 0 # time passed after placing an order and before opening it
if direction: # long model
entry_lvl = round(level - entry_dist * atr, digits)
bu_lvl = round(entry_lvl + bu * atr, digits)
bu_condition_level = entry_lvl + bu_cond * atr
take_lvl = round(entry_lvl + take * atr, digits)
stop_lvl = round(entry_lvl - stop * atr, digits)
stop_out_lvl = entry_lvl - stop_out * atr
else: # short model
entry_lvl = round(level + entry_dist * atr, digits)
bu_lvl = round(entry_lvl - bu * atr, digits)
bu_condition_level = entry_lvl - bu_cond * atr
take_lvl = round(entry_lvl - take * atr, digits)
stop_lvl = round(entry_lvl + stop * atr, digits)
stop_out_lvl = entry_lvl + stop_out * atr
# bringing buffers up to date
for open_model in open_models:
if model_time >= open_model.close_time:
open_models.remove(open_model)
# checking if this model should be counted
for opened_model in recently_opened_models:
if model_number == opened_model:
fits = False
for open_model in open_models:
if open_model.range_start <= entry_lvl <= open_model.range_end:
fits = False
for gene_idx in range(len(model)):
attr = model[gene_idx]
if non_trading_params[gene_idx][0] > attr or non_trading_params[gene_idx][1] < attr:
fits = False
break
if not fits:
continue
# evaluating model
models += 1
price_is_up = False
if highs[0] > entry_lvl + open_ord_cond_shift and lows[0] > entry_lvl + open_ord_cond_shift:
price_is_up = True
current_time = time[0]
for idx in range(len(highs)):
current_time = time[idx]
time_after_prev_order += 1
if (direction and lows[idx] <= stop_out_lvl) or (not direction and highs[idx] >= stop_out_lvl):
if is_opened:
stops += 1
break
if is_opened:
if use_bu and not placed_bu and \
((direction and highs[idx] >= bu_condition_level) or (
not direction and lows[idx] <= bu_condition_level)):
stop_lvl = bu_lvl
placed_bu = True
elif (direction and lows[idx] <= stop_lvl + close_ord_cond_shift) or (
not direction and highs[idx] >= stop_lvl + close_ord_cond_shift):
if placed_bu:
closed_by_bu = True
break
stops += 1
if stops >= max_stops:
break
is_opened = False
time_after_order = 0
placed_order = False
if highs[idx] > entry_lvl + open_ord_cond_shift and lows[idx] > entry_lvl + open_ord_cond_shift:
price_is_up = True
else:
price_is_up = False
elif (direction and highs[idx] >= take_lvl + close_ord_cond_shift) or (
not direction and lows[idx] <= take_lvl + close_ord_cond_shift):
have_take = True
break
if not is_opened:
if not placed_order and time_after_prev_order < min_tim_betw_odrs: # if 'True' then we can't open new order right now
continue
if not placed_order:
time_after_prev_order = 0
placed_order = True
if current_time >= exp_time:
break
if (price_is_up and lows[idx] <= entry_lvl + open_ord_cond_shift) or \
(not price_is_up and highs[idx] >= entry_lvl + open_ord_cond_shift):
is_opened = True
else:
time_after_order += 1
result -= stops * stop
if have_take:
result += take
elif closed_by_bu:
result += bu
result = round(result, 2)
if result > max_account_value:
max_account_value = result
drawdown = round(max_account_value - result, 2)
if drawdown > max_drawdown:
max_drawdown = drawdown
open_models.append(Model(
entry_lvl - min_dist_betw_orders * atr,
entry_lvl + min_dist_betw_orders * atr,
current_time,
mod_idx + 1
))
if len(recently_opened_models) >= 10:
recently_opened_models.pop(0)
recently_opened_models.append(model_number)
ratio = result / max_drawdown if max_drawdown != 0 else 0
fitness = result
return fitness, ratio, models