def onBars(self, bars):

        bar = bars[self.__instrument]   
        high = bar.getHigh()
        low = bar.getLow()
        close = bar.getClose()
        hlc3 = (high + low + close) / 3
        self.__hlc3.appendWithDateTime(bar.getDateTime(), hlc3)
        
        qty = 1                
       
        ema1 = talib.EMA(self.__hlc3, count=self.__ema1_period+1, timeperiod=self.__ema1_period)
        ema2 = talib.EMA(self.__hlc3, count=self.__ema2_period+1, timeperiod=self.__ema2_period)
        ema3 = talib.EMA(self.__hlc3, count=self.__ema3_period+1, timeperiod=self.__ema3_period)
        
        # Wait for enough bars to be available to calculate indicators.
        if (len(ema3) < 2) or math.isnan(ema3[-2]):
            return        
        
        buy_condition = self.is_increasing(ema1, 1) and self.is_increasing(ema2, 1) and self.is_increasing(ema3, 1)
        sell_condition = self.is_decreasing(ema1, 1) and self.is_decreasing(ema2, 1) and self.is_decreasing(ema3, 1) 
        
        # If a position was not opened, check if we should enter a long position.
        if self.__position is None:
            if buy_condition:
                # Enter a buy market order for 10 shares. The order is good till canceled.
                self.__position = self.enterLong(self.__instrument, qty)

        # Else, Check for Stop-loss or Exit.        
        elif not self.__position.exitActive():            
            if sell_condition:                
                self.__position.exitMarket()
Esempio n. 2
0
 def testEMA(self):
     barDs = self.__loadBarDS()
     self.assertAmountsAreEqual(indicator.EMA(barDs.getCloseDataSeries(), 252, 2)[1], 93.16)  # Original value 93.15
     self.assertAmountsAreEqual(indicator.EMA(barDs.getCloseDataSeries(), 252, 2)[2], 93.97)  # Original value 93.96
     self.assertAmountsAreEqual(
         indicator.EMA(barDs.getCloseDataSeries(), 252, 2)[-1],
         108.22
     )  # Original value 108.21
     self.assertAmountsAreEqual(
         indicator.EMA(barDs.getCloseDataSeries(), 252, 10)[9],
         93.23
     )  # Original value 93.22
Esempio n. 3
0
 def testEMA(self):
     barDs = self.__loadBarDS()
     self.assertTrue(
         compare(
             indicator.EMA(barDs.getCloseDataSeries(), 252, 2)[1],
             93.16))  # Original value 93.15
     self.assertTrue(
         compare(
             indicator.EMA(barDs.getCloseDataSeries(), 252, 2)[2],
             93.97))  # Original value 93.96
     self.assertTrue(
         compare(
             indicator.EMA(barDs.getCloseDataSeries(), 252, 2)[-1],
             108.22))  # Original value 108.21
     self.assertTrue(
         compare(
             indicator.EMA(barDs.getCloseDataSeries(), 252, 10)[9],
             93.23))  # Original value 93.22