def onBars(self, bars):   
        # Wait for enough bars to be available to calculate a SMA.
        bar = bars[self.__instrument]
        
        #getfeed
        barDs = self.getFeed().getDataSeries("orcl")
        closeDs = self.getFeed().getDataSeries("orcl").getCloseDataSeries()
        volumeDs = self.getFeed().getDataSeries("orcl").getVolumeDataSeries()
        #Long entry (2): engulfing signal: engulfing where 100 for bullish engulfing;  -100 for bearish engulfing;   0 for no engulfing
        engulfing = indicator.CDLENGULFING(barDs,50000)
        
        #recognize decreasing trend by previous 3 price and 3 volume
        trenddropprice = indicator.ROC(closeDs,50000,timeperiod=3)
        trenddropvol = indicator.ROC(volumeDs,50000,timeperiod=2)
    
        #Long exit (2): zigzag function to recognize divergence 
        ##zigzag for Price
        pivots = peak_valley_pivots(closeDs, 0.2, -0.2)
        zigPrice = compute_segment_returns(np.array(closeDs), pivots)
        
        ##zigzag for Volume
        pivotV = peak_valley_pivots(volumeDs, 0.2, -0.2)
        zigVolume = compute_segment_returns(np.array(volumeDs), pivots)
        
        ##zigzag for RSI
        ###create rsi series
        if self.__rsi[-1] is None:
            self.x=np.append(self.x,[50])
        else:
            self.x=np.append(self.x,self.__rsi[-1])
        
        pivotRSI = peak_valley_pivots(self.x, 0.2, -0.2)


        zigVolume = compute_segment_returns(np.array(volumeDs), pivots)
        zigRSI = compute_segment_returns(self.x, pivots)
         
        #Long exit (1) action: Edit the 90% price if price goes up
        if self.__position is not None and not self.__position.exitActive() and bar.getPrice()> (self.stoplossprice/0.9):
            self.stoplossprice=bar.getPrice()*0.9
        
        # If a position was not opened, check if we should enter a long position.
        if self.__position is None:
            
            # Long entry (2) action: [candle stick (try bullish engulfing first)] AND [rsi below 50] AND [price increase with volume high]
            # Enter a buy market order for 10 shares. The order is good till canceled.
            if engulfing[-1]==100 and trenddropprice[-2]<-2 and self.__rsi[-1]<50 and trenddropvol[-1]>0:
                self.__position = self.enterLong(self.__instrument, 10, True)
                self.stoplossprice=  bar.getPrice()*0.9      #Stop Long loss
                self.longprice=bar.getPrice()                #calculate profit only
            
        # Check if we have to exit the position.
        #                                         Long exit (1) action AND Stop Long loss   Long exit (2)
        elif not self.__position.exitActive() and (bar.getPrice() < self.stoplossprice or (zigPrice[-1]>0 and zigVolume[-1]<=0)):
            self.__position.exitMarket()
Example #2
0
 def test_rise_fall_rise(self):
     data = np.array([1.0, 1.05, 1.1, 1.0, 0.9, 1.5])
     pivots = zigzag.peak_valley_pivots(data, 0.1, -0.1)
     assert_array_almost_equal(zigzag.compute_segment_returns(data, pivots),
                               np.array([0.1, -0.181818, 0.6666666]))
Example #3
0
 def test_strictly_decreasing(self):
     data = np.linspace(100.0, 1.0, 10)
     pivots = zigzag.peak_valley_pivots(data, 0.1, -0.1)
     assert_array_almost_equal(zigzag.compute_segment_returns(data, pivots),
                               np.array([-0.99]))
Example #4
0
 def test_rise_fall_rise(self):
     data = np.array([1.0, 1.05, 1.1, 1.0, 0.9, 1.5])
     pivots = zigzag.peak_valley_pivots(data, 0.1, -0.1)
     assert_array_almost_equal(zigzag.compute_segment_returns(data, pivots),
                               np.array([0.1, -0.181818, 0.6666666]))
Example #5
0
 def test_strictly_decreasing(self):
     data = np.linspace(100.0, 1.0, 10)
     pivots = zigzag.peak_valley_pivots(data, 0.1, -0.1)
     assert_array_almost_equal(zigzag.compute_segment_returns(data, pivots),
                               np.array([-0.99]))