-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwfa.py
More file actions
121 lines (102 loc) · 3.33 KB
/
wfa.py
File metadata and controls
121 lines (102 loc) · 3.33 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
import time
import warnings
from multiprocessing import Pool
import init
from analysis.WalkForward import WalkForward
from model.Fitness import Fit, Fitness
from strategy.LiveParams import LiveParams
from utils.metrics import *
from utils.utils import *
''' walk-forward analysis '''
# INPUT ###########################################################
# data, indicators
asset = init.asset
num_months = init.num_months
isNetwork = init.isNetwork
# walk forward
percent = 25
runs = 9 # +1 added for final in-sample
fitness = Fitness(
fits = [
# (Fit.PROFIT_FACTOR, 50),
# (Fit.DRAWDOWN_PER_PROFIT, 30),
# (Fit.NUM_WINS, 30),
# (Fit.PROFIT, 30),
(Fit.CORRELATION, 20),
# (Fit.EXPECTANCY, 40),
(Fit.WIN_RATE, 80)
])
# multiprocessing uses all cores, 16 available, leave 1 for basic tasks
cores = runs + 1 # multiprocessing.cpu_count() - 1
# optimization
opt = LiveParams(
fastMinutes = [25],
disableEntryMinutes = [130], # np.linspace(55, 255, 201, dtype = int),
fastMomentumMinutes = np.linspace(75, 135, 13, dtype = int),
fastCrossoverPercent = [0], # [0, 75, 85, 95], # np.linspace(75, 95, 5),
takeProfitPercent = np.around(np.linspace(0.2, 0.75, 12), 2),
stopLossPercent = [0], # np.around(np.linspace(.25, .65, 9), 2),
fastAngleEntryFactor = [15], # np.linspace(15, 35, 3, dtype = int),
fastAngleExitFactor = [3020], # np.linspace(1000, 3000, 401, dtype = int),
slowMinutes = [2805], # np.linspace(1755, 3055, 7, dtype = int),
slowAngleFactor = [5], # np.linspace(15, 50, 8, dtype = int),
coolOffMinutes = [45], # np.linspace(0, 25, 26, dtype = int),
trendStartHour = [25], # np.linspace(0, 12, 13, dtype = int),
trendEndHour = [0], # np.linspace(12, 212, 201, dtype = int),
)
###################################################################
# clean console
os.system('clear')
warnings.filterwarnings('ignore')
start_time = time.time()
# organize outputs
data_name = asset + '_' + str(num_months) + 'm'
data_path = 'data/' + data_name
parent_path = 'wfa/' + data_name
analyzer_path = parent_path + '/' + str(percent) + '_' + str(runs)
# init data and indicators
data = getOhlc(asset, num_months, isNetwork)
emas, fractals = getIndicators(data, opt, data_path)
# remove residual analyses
shutil.rmtree(analyzer_path, ignore_errors = True)
# init walk forward
wfa = WalkForward(
num_months = num_months,
percent = percent,
fitness = fitness,
runs = runs,
data = data,
emas = emas,
fractals = fractals,
opt = opt,
parent_path = parent_path,
)
# init header metrics
print_metrics(wfa.metrics)
# run in-sample sweep
pool = Pool(cores)
pool.map(wfa.in_sample, range(runs + 1)) # add 1 for last IS (prediction)
pool.close()
pool.join()
# run out-of-sample for each fitness
pool = Pool(cores)
pool.map(wfa.out_of_sample, range(runs))
pool.close()
pool.join()
# build composite engines
fits = [fit for fit in Fit]
pool = Pool(cores)
pool.map(wfa.build_composite, fits)
pool.close()
pool.join()
# select composite of interest
wfa.analyze()
print_metrics(get_walk_forward_results_metrics(wfa))
print_composite_summary(wfa.winner_display_table)
wfa.print_last_analyzer()
# print analysis time
elapsed = time.time() - start_time
pretty = time.strftime('%-Hh %-Mm %-Ss', time.gmtime(elapsed))
print(f'\nElapsed time: {pretty}')
# plot results
wfa.plot()