@@ -25,9 +25,9 @@ def __init__(self):
25
25
period = self .p .rsi_period
26
26
)
27
27
28
- # def stop(self):
29
- # cash = self.broker.getvalue()
30
- # print('Result cash: {}'.format(cash))
28
+ def stop (self ):
29
+ cash = self .broker .getvalue ()
30
+ print ('Result cash: {}' .format (cash ))
31
31
32
32
def notify_order (self , order ):
33
33
if order .status in [order .Submitted , order .Accepted ]:
@@ -39,11 +39,11 @@ def next(self):
39
39
if self .order :
40
40
return
41
41
42
- input_data = []
43
- for i in range (7 ):
44
- input_data .append (self .dataclose [i - 6 ])
45
- for i in range (7 ):
46
- input_data .append (self .datavol [i - 6 ])
42
+ input_data = [self . dataclose [ 0 ], self . datavol [ 0 ] ]
43
+ # for i in range(7):
44
+ # input_data.append(self.dataclose[i - 6])
45
+ # for i in range(7):
46
+ # input_data.append(self.datavol[i - 6])
47
47
# for i in range(7):
48
48
# input_data.append(self.sma[i - 6])
49
49
# for i in range(7):
@@ -54,6 +54,9 @@ def next(self):
54
54
predict = self .p .model .predict (inp )[0 ]
55
55
predict = np .argmax (predict )
56
56
57
+ if predict == 2 :
58
+ return
59
+
57
60
if not self .position :
58
61
if predict == 0 :
59
62
self .order = self .buy ()
@@ -69,13 +72,6 @@ def next(self):
69
72
self .order = self .buy ()
70
73
71
74
72
- model = Sequential ()
73
- model .add (Dense (128 , input_dim = 14 , activation = 'relu' ))
74
- model .add (Dense (256 , activation = 'relu' ))
75
- model .add (Dense (2 , activation = 'relu' ))
76
-
77
- model .compile (optimizer = 'Adam' , loss = 'mse' )
78
-
79
75
data = bt .feeds .GenericCSVData (
80
76
dataname = 'eur_usd_1d.csv' ,
81
77
separator = ',' ,
@@ -91,9 +87,19 @@ def next(self):
91
87
openinterest = - 1
92
88
)
93
89
90
+ def get_model ():
91
+ model = Sequential ()
92
+ model .add (Dense (128 , input_dim = 2 , activation = 'relu' ))
93
+ # model.add(Dense(256, activation='relu'))
94
+ model .add (Dense (3 , activation = 'relu' ))
95
+
96
+ model .compile (optimizer = 'Adam' , loss = 'mse' )
97
+ return model
94
98
95
99
def get_reward (weights ):
100
+ model = get_model ()
96
101
model .set_weights (weights )
102
+
97
103
cerebro = bt .Cerebro ()
98
104
cerebro .addstrategy (ESStrategy , model = model )
99
105
cerebro .adddata (data )
@@ -104,5 +110,7 @@ def get_reward(weights):
104
110
return cerebro .broker .getvalue () - 5000.0
105
111
106
112
107
- es = EvolutionStrategy (model .get_weights (), get_reward , population_size = 50 , sigma = 0.1 , learning_rate = 0.1 )
113
+ model = get_model ()
114
+
115
+ es = EvolutionStrategy (model .get_weights (), get_reward , population_size = 50 , sigma = 0.2 , learning_rate = 0.01 )
108
116
es .run (1000 , print_step = 1 )
0 commit comments