-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmlStrategy.py
137 lines (110 loc) · 4.02 KB
/
mlStrategy.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
# strategy.py
from qtpylib.algo import Algo
import pickle
import tpot
from xgboost import XGBClassifier
mlTrainedFile = 'trainedML.pkl'
inputLookBack = 5
import pandas as pd
import numpy as np
def getInputData(bars, inputLookBack):
output = pd.DataFrame(bars['close'])
for i in range(inputLookBack):
if i == 0:
output['close'] = np.log(bars['close'])
output['volume'] = bars['volume'].diff()
else:
output['diff_%i' % i] = bars['close'] - bars['close'].shift(i)
return output.fillna(0).as_matrix()
def getTargetData(bars):
future_return = pd.DataFrame(bars['close'])
future_return = future_return.diff().shift(-1)
return (future_return > 0).fillna(0).astype(int).values.ravel()
def trainTpot(symbol):
import os
from tpot import TPOTClassifier
bars = pd.DataFrame.from_csv(settings.csvData + os.sep + symbol + '.BAR.csv')
inputData = getInputData(bars, inputLookBack)
target = getTargetData(bars)
# mlObject = TPOTClassifier(generations=5,population_size=50,verbosity=3,n_jobs=-2)
mlObject = XGBClassifier()
mlObject.fit(inputData, target)
print(mlObject.score(inputData, target))
# save tpot
with open(mlTrainedFile, 'wb') as output:
pickle.dump(mlObject, output, pickle.HIGHEST_PROTOCOL)
class MLAlgo(Algo):
position = 0
mlTrainedObject = None
def on_bar(self, instrument):
if self.mlTrainedObject is None:
with open(mlTrainedFile, 'rb') as input:
self.mlTrainedObject = pickle.load(input)
# get instrument history
bars = instrument.get_bars(lookback=inputLookBack)
# make sure we have at least 20 bars to work with
if len(bars) < inputLookBack:
return
if self.mlTrainedObject is None:
print("Trained first! tpotObject doesnt exists")
# get Input data
input = getInputData(bars, inputLookBack)
prediction = self.mlTrainedObject.predict(input)
# get current position data
positions = instrument.get_positions()
# trading logic - entry signal
if prediction[-1] > 0 and self.position == 0:
if not instrument.pending_orders:
print("[%s] BUY prediction=%i " % (
bars.index[-1], prediction[-1]))
self.position = 1
volume = capital / bars.close[-1]
# send a buy signal
instrument.buy(int(volume))
# trading logic - exit signal
elif prediction[-1] == 0 and self.position != 0:
print("[%s] Close Buy prediction=%i " % (
bars.index[-1], prediction[-1]))
# exit / flatten position
instrument.exit()
self.position = 0
else:
print("[%s] Nothing/Hold prediction=%i " % (
bars.index[-1], prediction[-1]))
BACKTEST_ENABLE = True
TRAIN_ML = False
nameOfStrategy = 'mlStrategy'
symbol = 'AAPL'
capital = 100000
import settings
import pickle
import backtest
import os
if __name__ == "__main__":
if TRAIN_ML or not os.path.exists(mlTrainedFile):
print('Training ML')
trainTpot(symbol)
if not BACKTEST_ENABLE:
strategy = MLAlgo(
instruments=[(symbol, "STK", "SMART", "USD", "", 0.0,)],
resolution="1D",
ibport=7497,
blotter='robotrader',
log=settings.logsPath + nameOfStrategy + '.log'
)
strategy.run()
else:
strategy = MLAlgo(
instruments=[(symbol, "STK", "SMART", "USD", "", 0.0,)],
resolution="1D",
backtest=True,
start='2017-01-01', # YYY-MM-DD [HH:MM:SS[.MS]
end='2018-02-16',
output=settings.backtestData + nameOfStrategy + '.csv',
data=settings.csvData,
log=settings.logsPath + nameOfStrategy + '.log',
ibport=7497,
blotter='robotrader'
)
strategy.run()
backtest.analyze(backtestNameFile=nameOfStrategy + '.csv')