Example #1
0
    def onBars(self, bars):

        bar = bars[self.__instrument]

        if self.__mall[-1] is None:
            return

        self.testCon()

        if self.__longPos is not None:

            if self.exitLongSignal():
                self.__longPos.exitMarket()

        elif self.__shortPos is not None:

            if self.exitShortSignal():
                self.__shortPos.exitMarket()

        elif self.__longPos is None and self.__shortPos is None:
            if self.enterLongSignal():
                shares = int(self.getBroker().getEquity() * 0.2 /
                             bar.getPrice())
                self.__longPos = self.enterLong(self.__instrument, shares)

            elif self.enterShortSignal():
                shares = int(self.getBroker().getEquity() * 0.2 /
                             bar.getPrice())
                self.__shortPos = self.enterShort(self.__instrument, shares)
Example #2
0
    def onBars(self, bars):

        bar = bars[self.__instrument]
        
        if self.__mall[-1] is None:
            return
            
        self.testCon()            
            
        if self.__longPos is not None:

            if self.exitLongSignal():
                self.__longPos.exitMarket()
           
        elif self.__shortPos is not None:
            
            if self.exitShortSignal():
                self.__shortPos.exitMarket()

        elif self.__longPos is None and self.__shortPos is None:
            if self.enterLongSignal():
                shares = int(self.getBroker().getEquity() * 0.2 / bar.getPrice())
                self.__longPos = self.enterLong(self.__instrument, shares)
                
            elif self.enterShortSignal():
                shares = int(self.getBroker().getEquity() * 0.2 / bar.getPrice())
                self.__shortPos = self.enterShort(self.__instrument, shares)
Example #3
0
    def onBars(self, bars):
        if self.__sma[-1] is None:
            return
        bar = bars[self.__instrument]
        #self.info("close:%s sma:%s rsi:%s" % (bar.getClose(), self.__sma[-1], self.__rsi[-1]))

        if self.__position is None:
            if bar.getPrice() > self.__sma[-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 bar.getPrice() < self.__sma[-1] and not self.__position.exitActive():
            self.__position.exitMarket()
Example #4
0
    def onBars(self, bars):
        # Wait for enough bars to be available to calculate SMA and RSI.
        if self.__macd.getHistogram()[-1] is None or self.__macd.getSignal()[-1] is None  or self.__macd[-1] is None or self.__smaF[-1] is None or self.__smaS[-1] is None:
            return

        bar = bars[self.__instrument]
        if self.__longPos is not None:
            if self.exitLongSignal(bar):
                self.__longPos.exitMarket()
                self.__waitTime = self.__time
                print "exit long:"+str(bar.getPrice())
        elif self.__shortPos is not None:
            if self.exitShortSignal(bar):
                self.__shortPos.exitMarket()
                self.__waitTime = self.__time
                print "exit short:"+str(bar.getPrice())
        else:
            if self.__waitTime>0:
                self.__waitTime-=1
            elif self.enterLongSignal(bar):
                shares = int(self.getBroker().getCash() * 0.9 / bars[self.__instrument].getPrice())
                self.__longPos = self.enterLong(self.__instrument, shares, True)
                self.__PosPrice = bar.getPrice()
                print "enter long:"+str(bar.getPrice())
            elif self.enterShortSignal(bar):
                shares = int(self.getBroker().getCash() * 0.9 / bars[self.__instrument].getPrice())
                self.__shortPos = self.enterShort(self.__instrument, shares, True)
                self.__PosPrice = bar.getPrice()
                print "enter short:"+str(bar.getPrice())
Example #5
0
 def resampledOnBar(self, bars):
     bar = bars[self.__instrument] # pyalgotrade.bar.BasicBar object
     # print("Date: {} Close: {} PriceHigh: {}" .format(bar.getDateTime(), round(bar.getClose(),2), round(bar.getHigh(),2) ))
     shares = int(self.getBroker().getCash() * 0.5 / bar.getHigh()) # 80% of cash stock
     # print("this is current total cash {}" .format(round(self.getBroker().getCash(),2)))
     # print("shares is {}" .format(shares))
     print("Date: {} Open: {} Close: {} PriceLow: {} PriceHigh: {} ".format(bar.getDateTime(), round(bar.getOpen(), 2), round(bar.getClose(), 2),round(bar.getLow()), round(bar.getHigh())))
     if self.__position is None:
         self.__position = self.enterLong(self.__instrument, shares, False, True) # this to enter market order # pyalgotrade.strategy.position.LongPosition object
         print("enter long for {} shares at {} price".format(shares, bar.getPrice()))
         print("remaining cash is " + str(self.getBroker().getCash()))
         print("position is " + str(self.__position.getShares()))
     elif not self.__position.exitActive(): # Returns True if the exit order is active
         # if not exit orders being active
         self.__position.exitMarket(True) # this just submits the market order and self.__position becomes none
         print("exit for {} shares at {} price".format(self.__position.getShares(), bar.getPrice()))
         print("remaining cash is " + str(self.getBroker().getCash()))
         print("position is " + str(self.__position.getShares()))
Example #6
0
    def onBars(self, bars):
        try:
            symbol = bars.getInstruments()[0]
            bar = bars[symbol]
               
            self.__ticker.append((bar.getDateTime().strftime('%Y-%m-%d %H:%M:%S.%f'), symbol.split('coinigy-')[1], bar.getPrice(), bar.getVolume(), 1 if bar.isBuy() else 2))

            #logging.info('symbol: %r,  frequency: %r, volume: %r' %(symbol, bar.getFrequency(), bar.getVolume())) 

        except Exception as e:
            logging.error('%r', e)
        except:
            logging.error('Exception caught!')
Example #7
0
 def exitShortSignal(self,bar):
     #return cross.cross_below(self.__priceDS, self.__exitSMA) and not self.__shortPos.exitActive()
     return bar.getPrice() > self.__smaS[-1] and self.__smaF[-1] > self.__smaS[-1] and  self.__macd[-1]-self.getSignal()[-1] > 0  or bar.getPrice() - self.__PosPrice > self.__stopLoss or self.__PosPrice-bar.getPrice() > self.__targetProfit
Example #8
0
 def enterShortSignal(self, bar):
     #return bar.getPrice() < self.__entrySMA[-1] and self.__rsi[-1] >= self.__overBoughtThreshold
     return self.__smaF[-1] < self.__smaS[-1] and bar.getPrice() < self.__smaF[-1] and  self.__macd[-1]-self.getSignal()[-1] < 0
Example #9
0
 def exitLongSignal(self,bar):
     #return cross.cross_above(self.__priceDS, self.__exitSMA) and not self.__longPos.exitActive()
     return bar.getPrice() < self.__smaS[-1] and self.__smaF[-1] < self.__smaS[-1] and  self.__macd[-1]-self.getSignal()[-1] < 0 or self.__PosPrice - bar.getPrice() > self.__stopLoss or bar.getPrice() - self.__PosPrice > self.__targetProfit
Example #10
0
 def enterLongSignal(self, bar):
     #return bar.getPrice() > self.__entrySMA[-1] and self.__rsi[-1] <= self.__overSoldThreshold
     return self.__smaF[-1] > self.__smaS[-1] and bar.getPrice() >self.__smaF[-1] and  self.__macd[-1]-self.getSignal()[-1] > 0
Example #11
0
 def enterShortSignal(self, bar):
     return bar.getPrice() < self.__entrySMA[-1] and self.__rsi[
         -1] >= self.__overBoughtThreshold
Example #12
0
 def enterLongSignal(self, bar):
     return bar.getPrice(
     ) > self.__entrySMA[-1] and self.__rsi[-1] <= self.__overSoldThreshold