def testSTOCH(self): barDs = self.__loadBarDS() self.assertAmountsAreEqual( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 3, talib.MA_Type.SMA)[0][8], 24.0128) self.assertAmountsAreEqual( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 3, talib.MA_Type.SMA)[1][8], 36.254) self.assertAmountsAreEqual( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 4, talib.MA_Type.SMA)[0][-1], 30.194) self.assertAmountsAreEqual( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 4, talib.MA_Type.SMA)[1][-1], 46.641)
def testSTOCH(self): barDs = self.__loadBarDS() self.assertTrue( compare( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 3, talib.MA_Type.SMA)[0][8], 24.0128)) self.assertTrue( compare( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 3, talib.MA_Type.SMA)[1][8], 36.254)) self.assertTrue( compare( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 4, talib.MA_Type.SMA)[0][-1], 30.194)) self.assertTrue( compare( indicator.STOCH(barDs, 252, 5, 3, talib.MA_Type.SMA, 4, talib.MA_Type.SMA)[1][-1], 46.641))
def onBars(self, bars): if self.__atr is None: return barDs = self.getFeed().getDataSeries(self.__instrument) atr_21 = self.__atr[-21:] if len(atr_21) < 20: return #print atr_21[:-1] maxatr = max(atr_21[:-1]) nowatr = self.__atr[-1] stochk, stochd = indicator.STOCH(barDs, 100, fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) #print stochk[-1],"--",stochd[-1] #print "stochk:%s mfast:%s mslow:%s nowatr:%s maxatr:%s"%(stochk[-1], self.__mafast[-1], self.__maslow[-1], nowatr, maxatr) if self.__position is None: if stochk[-1] < 20 and self.__mafast[-1] < self.__maslow[-1] : # Enter a buy market order for 10 shares. The order is good till canceled. self.__position = self.enterLong(self.__instrument, 10, True) #print dir(self.__position) # Check if we have to exit the position. elif stochk[-1] > 75 and nowatr < maxatr and not self.__position.exitActive(): self.__position.exitMarket()