-
Notifications
You must be signed in to change notification settings - Fork 119
/
trading_strategy_fitting.py
170 lines (115 loc) · 6.34 KB
/
trading_strategy_fitting.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
import numpy as np
from time import time
from data_input_processing import Data, train_test_indices, generate_training_variables
from strategy_evaluation import post_process_training_results, output_strategy_results
from machine_learning import random_forest_fitting, svm_fitting, adaboost_fitting, gradient_boosting_fitting,\
extra_trees_fitting, tensorflow_fitting, tensorflow_sequence_fitting
SEC_IN_DAY = 86400
def meta_fitting(data_to_predict_local, data_input_2, strategy_dictionary):
fitting_inputs, fitting_targets = input_processing(data_to_predict_local, data_input_2, strategy_dictionary)
error = []
train_indices, test_indices = train_test_indices(fitting_inputs, strategy_dictionary['train_test_ratio'])
if strategy_dictionary['ml_mode'] == 'svm':
fitting_dictionary, error = svm_fitting(
fitting_inputs, fitting_targets, train_indices, test_indices, strategy_dictionary)
elif strategy_dictionary['ml_mode'] == 'randomforest':
fitting_dictionary, error = random_forest_fitting(
fitting_inputs, fitting_targets, train_indices, test_indices, strategy_dictionary)
elif strategy_dictionary['ml_mode'] == 'adaboost':
fitting_dictionary, error = adaboost_fitting(
fitting_inputs, fitting_targets, train_indices, test_indices, strategy_dictionary)
elif strategy_dictionary['ml_mode'] == 'gradientboosting':
fitting_dictionary, error = gradient_boosting_fitting(
fitting_inputs, fitting_targets, train_indices, test_indices, strategy_dictionary)
elif strategy_dictionary['ml_mode'] == 'extratreesfitting':
fitting_dictionary, error = extra_trees_fitting(
fitting_inputs, fitting_targets, train_indices, test_indices, strategy_dictionary)
fitting_dictionary['train_indices'] = train_indices
fitting_dictionary['test_indices'] = test_indices
fitting_dictionary['error'] = error
return fitting_dictionary
def input_processing(data_to_predict_local, data_input_2, strategy_dictionary):
fitting_inputs, fitting_targets = generate_training_variables(data_to_predict_local, strategy_dictionary)
fitting_inputs_2, fitting_targets_2 = generate_training_variables(data_input_2, strategy_dictionary)
fitting_inputs, fitting_inputs_2 = trim_inputs(fitting_inputs, fitting_inputs_2)
fitting_inputs = np.hstack((fitting_inputs, fitting_inputs_2))
return fitting_inputs, fitting_targets
def trim_inputs(fitting_inputs, fitting_inputs_2):
length_1 = len(fitting_inputs)
length_2 = len(fitting_inputs_2)
min_length = np.minimum(length_1, length_2)
fitting_inputs = fitting_inputs[-min_length:]
fitting_inputs_2 = fitting_inputs_2[-min_length:]
return fitting_inputs, fitting_inputs_2
def tic():
t = time()
return lambda: (time() - t)
def retrieve_data(ticker, strategy_dictionary, filename=None):
end = time() - strategy_dictionary['offset'] * SEC_IN_DAY
start = end - SEC_IN_DAY * strategy_dictionary['n_days']
data_local = None
while data_local is None:
try:
data_local = Data(
ticker, start, end, strategy_dictionary['candle_size'], strategy_dictionary['web_flag'], filename)
except:
pass
data_local.normalise_data()
return data_local
def offset_scan_validation(strategy_dictionary, offsets):
strategy_dictionary['plot_flag'] = False
strategy_dictionary['ouput_flag'] = True
total_error = 0
total_profit = 0
for offset in offsets:
strategy_dictionary['offset'] = offset
fitting_dictionary, data_to_predict, error, profit_fraction = fit_strategy(strategy_dictionary)
total_error += error / len(offsets)
total_profit += profit_fraction
underlined_output('Averages: ')
print 'Total profit: ', total_profit
print 'Average error: ', total_error
def tensorflow_offset_scan_validation(strategy_dictionary, offsets):
strategy_dictionary['plot_flag'] = False
strategy_dictionary['ouput_flag'] = True
total_error = 0
total_profit = 0
for offset in offsets:
strategy_dictionary['offset'] = offset
fitting_dictionary, data_to_predict, error, profit_fraction = fit_tensorflow(strategy_dictionary)
total_error += error
total_profit += profit_fraction
underlined_output('Averages: ')
print 'Total profit: ', total_profit
print 'Average error: ', total_error
def import_data(strategy_dictionary):
data_to_predict = retrieve_data(
strategy_dictionary['ticker_1'], strategy_dictionary, strategy_dictionary['filename1'])
data_2 = retrieve_data(strategy_dictionary['ticker_2'], strategy_dictionary, strategy_dictionary['filename2'])
return data_to_predict, data_2
def fit_strategy(strategy_dictionary):
toc = tic()
data_to_predict, data_2 = import_data(strategy_dictionary)
fitting_dictionary = meta_fitting(data_to_predict, data_2, strategy_dictionary)
fitting_dictionary = post_process_training_results(strategy_dictionary, fitting_dictionary, data_to_predict)
profit_factor = output_strategy_results(strategy_dictionary, fitting_dictionary, data_to_predict, toc)
return fitting_dictionary, data_to_predict, profit_factor
def fit_tensorflow(strategy_dictionary):
toc = tic()
data_to_predict, data_2 = import_data(strategy_dictionary)
fitting_inputs, fitting_targets = input_processing(data_to_predict, data_2, strategy_dictionary)
train_indices, test_indices = train_test_indices(fitting_inputs, strategy_dictionary['train_test_ratio'])
if strategy_dictionary['sequence_flag']:
fitting_dictionary, error = tensorflow_sequence_fitting(
'/home/thomas/test',train_indices, test_indices, fitting_inputs, fitting_targets, strategy_dictionary)
else:
fitting_dictionary, error = tensorflow_fitting(train_indices, test_indices, fitting_inputs, fitting_targets)
fitting_dictionary['train_indices'] = train_indices
fitting_dictionary['test_indices'] = test_indices
fitting_dictionary = post_process_training_results(strategy_dictionary, fitting_dictionary, data_to_predict)
profit_factor = output_strategy_results(strategy_dictionary, fitting_dictionary, data_to_predict, toc)
return fitting_dictionary, data_to_predict, error, profit_factor
def underlined_output(string):
print string
print '----------------------'
print '\n'