def __init__(self, close, rsi: indicators.RSI, macd: indicators.MACD, fee=0.0): Strategy.__init__(self, close.index, fee=0.0) # indique le signal en cours sachant que MACD > RSI buysignalstrat = None macdcrossedfromdown = False macdcrossedfromtop = False lowthreshpassed = False lowlowthreshpassed = False hithreshpassed = False hihithreshpassed = False hasbought = False self.signals['rsi'] = rsi.data() self.signals['macd'], self.signals['macd_signal'] = macd.data() for index, row in self.signals.iterrows(): # achat en fonction du RSI if row['rsi'] < 33: lowthreshpassed = True if row['rsi'] < 20: lowlowthreshpassed = True if row['rsi'] > 66: hithreshpassed = True if row['rsi'] > 80: hihithreshpassed = True if row['rsi'] > 20 and lowlowthreshpassed == True: lowlowthreshpassed = False if hasbought == False: hasbought = True buysignalstrat = "RSI" if row['rsi'] > 33 and lowthreshpassed == True: lowthreshpassed = False if hasbought == False: hasbought = True buysignalstrat = "RSI" # on revend par RSI seulement si c'est le seul signal qui a généré l'achat if row['rsi'] < 80 and hihithreshpassed == True: hihithreshpassed = False if hasbought == True and buysignalstrat == "RSI": hasbought = False buysignalstrat = None if row['rsi'] < 66 and hithreshpassed == True: hithreshpassed = False if hasbought == True and buysignalstrat == "RSI": hasbought = False buysignalstrat = None # achat en fonction de la MACD # TODO : lorsque le marché stagne, la MACD génère beaucoup de faux signaux, comment les éviter ? if row['macd'] > row[ 'macd_signal'] and macdcrossedfromdown == False: macdcrossedfromdown = True macdcrossedfromtop = False if hasbought == False: hasbought = True # même si nous avions acheté avec le RSI, si le signal est confirmé par la MACD # il sera considéré comme étant prévalant buysignalstrat = "MACD" if row['macd'] < row['macd_signal'] and macdcrossedfromtop == False: macdcrossedfromtop = True macdcrossedfromdown = False if hasbought == True: hasbought = False buysignalstrat = None if hasbought == True: self.signals['signal'].loc[index] = 1.0 self.signals['positions'] = self.signals['signal'].diff()
def __init__(self, close, macd: indicators.MACD, fee=0.0): Strategy.__init__(self, close.index, fee=0.0) line, signal = macd.data() self.signals['signal'] = np.where(line > signal, 1.0, 0.0) self.signals['positions'] = self.signals['signal'].diff()