25
25
import numpy as np
26
26
from enum import Enum
27
27
28
+ import pandas as pd
29
+
30
+ from custom_indicators import BollingerBandsBandwitch
31
+
28
32
# action list
29
33
class Action (Enum ):
30
34
HOLD = 0
@@ -62,6 +66,25 @@ def __init__(self, *argv):
62
66
self .atr = bt .indicators .ATR (self .data , period = self .p .atrperiod )
63
67
64
68
self .stochastic = bt .ind .stochastic .Stochastic (self .data )
69
+ self .rsi = bt .ind .RelativeStrengthIndex (self .data )
70
+ self .bbands = bt .ind .BollingerBands (self .data )
71
+ self .bbandsPct = bt .ind .BollingerBandsPct (self .data )
72
+ self .bbandsBandwitch = BollingerBandsBandwitch .BollingerBandsBandwitch (self .data )
73
+
74
+ self .sma_200 = bt .ind .MovingAverageSimple (self .data ,period = 200 )
75
+ self .sma_150 = bt .ind .MovingAverageSimple (self .data ,period = 150 )
76
+ self .sma_100 = bt .ind .MovingAverageSimple (self .data ,period = 100 )
77
+ self .sma_50 = bt .ind .MovingAverageSimple (self .data ,period = 50 )
78
+ self .sma_21 = bt .ind .MovingAverageSimple (self .data ,period = 21 )
79
+
80
+ self .ema_200 = bt .ind .ExponentialMovingAverage (self .data ,period = 200 )
81
+ self .ema_100 = bt .ind .ExponentialMovingAverage (self .data ,period = 100 )
82
+ self .ema_50 = bt .ind .ExponentialMovingAverage (self .data ,period = 50 )
83
+ self .ema_26 = bt .ind .ExponentialMovingAverage (self .data ,period = 26 )
84
+ self .ema_12 = bt .ind .ExponentialMovingAverage (self .data ,period = 12 )
85
+ self .ema_9 = bt .ind .ExponentialMovingAverage (self .data ,period = 9 )
86
+
87
+ self .macd = bt .ind .MACDHisto (self .data )
65
88
66
89
pass
67
90
@@ -76,6 +99,11 @@ def next(self):
76
99
77
100
self .obseravation = self .next_observation ()
78
101
102
+ # Do nothing if a parameter is not valid yet (basically wait for all idicators to be loaded)
103
+ if pd .isna (self .obseravation ).any ():
104
+ print ("Waiting indicators" )
105
+ return
106
+
79
107
# Prepare data for Model
80
108
action , _states = self .model .predict (self .obseravation ) # deterministic=True
81
109
@@ -112,10 +140,43 @@ def next_observation(self):
112
140
# https://stackoverflow.com/questions/53979199/tensorflow-keras-returning-multiple-predictions-while-expecting-one
113
141
114
142
# Ichimoku
115
- inputs = [ self .ichimoku .tenkan [0 ], self .ichimoku .kijun [0 ], self .ichimoku .senkou [0 ], self .ichimoku .senkou_lead [0 ], self .ichimoku .chikou [0 ] ]
143
+ #inputs = [ self.ichimoku.tenkan[0], self.ichimoku.kijun[0], self.ichimoku.senkou[0], self.ichimoku.senkou_lead[0], self.ichimoku.chikou[0] ]
144
+
145
+ # OHLCV
146
+ inputs = [ self .data .open [0 ],self .data .high [0 ],self .data .low [0 ],self .data .close [0 ],self .data .volume [0 ] ]
116
147
117
148
# Stochastic
118
- inputs = inputs + [self .stochastic .percK [0 ] / 100.0 , self .stochastic .percD [0 ] / 100.0 ]
149
+ inputs = inputs + [self .stochastic .percK [0 ], self .stochastic .percD [0 ]]
150
+
151
+ # Rsi
152
+ inputs = inputs + [self .rsi .rsi [0 ]]
153
+
154
+ # bbands
155
+ inputs = inputs + [self .bbands .bot [0 ]] # BBL
156
+ inputs = inputs + [self .bbands .mid [0 ]] # BBM
157
+ inputs = inputs + [self .bbands .top [0 ]] # BBU
158
+ inputs = inputs + [self .bbandsBandwitch .bandwitch [0 ]] # BBB
159
+ inputs = inputs + [self .bbandsPct .pctb [0 ]] # BBP
160
+
161
+ # sma
162
+ inputs = inputs + [self .sma_200 .sma [0 ]]
163
+ inputs = inputs + [self .sma_150 .sma [0 ]]
164
+ inputs = inputs + [self .sma_100 .sma [0 ]]
165
+ inputs = inputs + [self .sma_50 .sma [0 ]]
166
+ inputs = inputs + [self .sma_21 .sma [0 ]]
167
+
168
+ # ema
169
+ inputs = inputs + [self .ema_200 .ema [0 ]]
170
+ inputs = inputs + [self .ema_100 .ema [0 ]]
171
+ inputs = inputs + [self .ema_50 .ema [0 ]]
172
+ inputs = inputs + [self .ema_26 .ema [0 ]]
173
+ inputs = inputs + [self .ema_12 .ema [0 ]]
174
+ inputs = inputs + [self .ema_9 .ema [0 ]]
175
+
176
+ # macd
177
+ inputs = inputs + [self .macd .macd [0 ]] # MACD
178
+ inputs = inputs + [self .macd .histo [0 ]] # MACD histo
179
+ inputs = inputs + [self .macd .signal [0 ]] # MACD signal
119
180
120
181
return np .array (inputs )
121
182
0 commit comments