-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple_es.py
116 lines (93 loc) · 2.86 KB
/
simple_es.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
import numpy as np
import backtrader as bt
from es import EvolutionStrategy
from keras.models import Model, Input, Sequential
from keras.layers import Dense, Activation
class ESStrategy(bt.Strategy):
params = {
'model': None,
'ma_period': 200,
'rsi_period': 14
}
def __init__(self):
self.order = None
self.dataclose = self.datas[0].close
self.datavol = self.datas[0].volume
self.sma = bt.indicators.SimpleMovingAverage(
self.datas[0],
period=self.p.ma_period
)
self.rsi = bt.indicators.RelativeStrengthIndex(
period=self.p.rsi_period
)
def stop(self):
cash = self.broker.getvalue()
print('Result cash: {}'.format(cash))
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
return
self.order = None
def next(self):
if self.order:
return
input_data = [self.dataclose[0], self.datavol[0]]
# for i in range(7):
# input_data.append(self.dataclose[i - 6])
# for i in range(7):
# input_data.append(self.datavol[i - 6])
# for i in range(7):
# input_data.append(self.sma[i - 6])
# for i in range(7):
# input_data.append(self.rsi[i-6])
inp = np.asanyarray(input_data)
inp = np.expand_dims(inp, 0)
predict = self.p.model.predict(inp)[0]
predict = np.argmax(predict)
if predict == 2:
return
if not self.position:
if predict == 0:
self.order = self.buy()
else:
if predict == 1:
self.order = self.sell()
if not self.position:
if predict == 1:
self.order = self.sell()
else:
if predict == 0:
self.order = self.buy()
data = bt.feeds.GenericCSVData(
dataname='eur_usd_1d.csv',
separator=',',
dtformat=('%Y%m%d'),
tmformat=('%H%M00'),
datetime=0,
time=1,
open=2,
high=3,
low=4,
close=5,
volume=6,
openinterest=-1
)
def get_model():
model = Sequential()
model.add(Dense(128, input_dim=2, activation='relu'))
# model.add(Dense(256, activation='relu'))
model.add(Dense(3, activation='relu'))
model.compile(optimizer='Adam', loss='mse')
return model
def get_reward(weights):
model = get_model()
model.set_weights(weights)
cerebro = bt.Cerebro()
cerebro.addstrategy(ESStrategy, model=model)
cerebro.adddata(data)
cerebro.broker.setcash(1000)
cerebro.addsizer(bt.sizers.FixedSize, stake=50)
cerebro.run()
return cerebro.broker.getvalue() - 5000.0
model = get_model()
es = EvolutionStrategy(model.get_weights(), get_reward, population_size=50, sigma=0.2, learning_rate=0.01)
es.run(1000, print_step=1)