def onBars(self, bars): self.__si += 1 # Wait for enough bars to be available to calculate a SMA. if self.__sma[30][-1] is None: return bar = bars[self.__instrument] self.__mprice = bar.getPrice() # If a position was not opened, check if we should enter a long # position. if self.__position is None: if cross.cross_above(self.__sma[10], self.__sma[30]) > 0: self.__jsl.append([self.__si - 1]) mbroker = self.getBroker() shares = mbroker.getCash() / bar.getPrice() * 0.95 # self.__position = self.marketOrder(self.__instrument, self.__shares) print("buy%.2f in %.2f use %d" % (shares, bar.getPrice(), mbroker.getCash())) self.__position = self.enterLong(self.__instrument, shares, True) # Check if we have to exit the position. # elif not self.__position.exitActive() and # cross.cross_below(self.__prices, self.__sma[10]) > 0: elif not self.__position.exitActive() and ( self.__position.getReturn() > 0.3 or cross.cross_below(self.__sma[10], self.__sma[30]) > 0): self.__position.exitMarket() self.__jsl[-1].append(self.__si - 1)
def onBars(self, bars): # Wait for enough bars to be available to calculate a SMA. bar = bars[self.__instrument] if self.getFeed().isHistory(): return if self.__sma[60][-1] is None: return logger.info( "onBars %s:%s: close:%.2f" % (self.__instrument, bar.getDateTimeLocal(), bar.getPrice())) bar = bars[self.__instrument] # If a position was not opened, check if we should enter a long # position. if self.__position is None: if cross.cross_above(self.__sma[10], self.__sma[30]) > 0: mbroker = self.getBroker() shares = mbroker.getCash() / bar.getPrice() * 0.9 self.__position = self.enterLongLimit(self.__instrument, bar.getPrice(), shares, True) # Check if we have to exit the position. elif not self.__position.exitActive() and cross.cross_below( self.__sma[10], self.__sma[30]) > 0: self.__position.exitLimit(bar.getPrice())
def onBars(self, bars): # If a position was not opened, check if we should enter a long position. if self.__position is None: if cross.cross_above(self.__prices, self.__sma) > 0: shares = int(self.getBroker().getCash() * 0.9 / bars[self.__instrument].getPrice()) # Enter a buy market order. The order is good till canceled. self.__position = self.enterLong(self.__instrument, shares, True) # Check if we have to exit the position. elif not self.__position.exitActive() and cross.cross_below(self.__prices, self.__sma) > 0: self.__position.exitMarket()
def onBars(self, bars): if bars.getBar(self.__lead): if cross.cross_above(self.__adjClose, self.__slowSMA) == 1 and self.__pos is None: shares = self.__calculatePosSize() if shares: self.__pos = self.enterLong(self.__lag, shares) elif cross.cross_below( self.__adjClose, self.__fastSMA) == 1 and self.__pos is not None: self.__pos.exitMarket()
def onBars(self, bars): bar = bars.getBar("orcl") self.printDebug("%s: O=%s H=%s L=%s C=%s" % (bar.getDateTime(), bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose())) if cross.cross_above(self.__fastSMADS, self.__slowSMADS) == 1: if self.__shortPos: self.exitShortPosition(bars, self.__shortPos) assert(self.__longPos is None) self.__longPos = self.enterLongPosition(bars) elif cross.cross_below(self.__fastSMADS, self.__slowSMADS) == 1: if self.__longPos: self.exitLongPosition(bars, self.__longPos) assert(self.__shortPos is None) self.__shortPos = self.enterShortPosition(bars)
def onBars(self, bars): bar = bars[self.__instrument] self.info("Price: %s. Volume: %s." % (bar.getClose(), bar.getVolume())) # Wait until we get the current bid/ask prices. if self.__ask is None: return # If a position was not opened, check if we should enter a long position. if self.__position is None: if cross.cross_above(self.__prices, self.__sma) > 0: self.info("Entry signal. Buy at %s" % (self.__ask)) self.__position = self.enterLongLimit(self.__instrument, self.__ask, self.__posSize, True) # Check if we have to close the position. elif not self.__position.exitActive() and cross.cross_below(self.__prices, self.__sma) > 0: self.info("Exit signal. Sell at %s" % (self.__bid)) self.__position.exitLimit(self.__bid)
def onBars(self, bars): # If a position was not opened, check if we should enter a long # position. if self.__ma2[-1] is None: return if self.__position is not None: if not self.__position.exitActive() and cross.cross_below(self.__ma1, self.__ma2) > 0: self.__position.exitMarket() if self.__position is None: if self.buyCon1() and self.buyCon2(): shares = int(self.getBroker().getCash() * 0.2 / bars[self.__instrument].getPrice()) self.__position = self.enterLong(self.__instrument, shares) print(bars[self.__instrument].getDateTime(), bars[self.__instrument].getPrice())
def testCrossBelowMany(self): count = 100 values1 = [0 for i in range(count)] values2 = [-1 if i % 2 == 0 else 1 for i in range(count)] # Check first value self.assertEqual(cross.cross_below(values1, values2, 0, 0), 0) # Check every 2 values. period = 2 for i in range(1, count): if i % 2 == 0: self.assertEqual( cross.cross_below(values1, values2, i - period + 1, i + 1), 0) else: self.assertEqual( cross.cross_below(values1, values2, i - period + 1, i + 1), 1) # Check every 4 values. period = 4 for i in range(3, count): if i % 2 == 0: self.assertEqual( cross.cross_below(values1, values2, i - period + 1, i + 1), 1) else: self.assertEqual( cross.cross_below(values1, values2, i - period + 1, i + 1), 2) # Check for all values. self.assertEqual(cross.cross_below(values1, values2, 0, count), count / 2)
def exitShortSignal(self): return cross.cross_below( self.__priceDS, self.__exitSMA) and not self.__shortPos.exitActive()
def exitLongSignal(self): if cross.cross_below( self.__mass, self.__masl) > 0 and not self.__longPos.exitActive(): return True
def enterShortSignal(self): if cross.cross_below(self.__mals, self.__mall) > 0: return True
def sellCon1(self): if cross.cross_below(self.__ma1, self.__ma2) > 0: return True
def onBars(self, bars): # If a position was not opened, check if we should enter a long # position. bar = bars[self.__instrument] # filter datetime ####################################################################### filterCon = len(self.__close) < max( self.__bollingerlength, self.__malength, self.__closelength) self.__filterCon.append(filterCon) if filterCon: return # record position ####################################################################### if self.__longPos is not None and self.__shortPos is None: self.__p.append(1) elif self.__longPos is None and self.__shortPos is not None: self.__p.append(-1) else: self.__p.append(0) # calculate ccMA ####################################################################### ccMACon1 = self.__longPos is not None or self.__shortPos is not None ccMACon2 = self.__malength > self.__ccMAlength if ccMACon1 and ccMACon2: self.__malength = self.__malength - 1 elif not ccMACon1: self.__malength = 50 self.__ccMA = np.mean(self.__close[-self.__malength:]) self.__MA.append(self.__ccMA) self.__ccMACon1.append(ccMACon1) self.__ccMACon2.append(ccMACon2) # open and close condition ####################################################################### self.__enterLong1 = (cross.cross_above(self.__high, self.__UpperBand) > 0) self.__enterLong2 = (bar.getClose() >= max( self.__close[-self.__closelength:])) self.__enter = ((self.__UpperBand[-1] - self.__LowerBand[-1]) / bar.getClose() > float(self.__space) / 1000) # print (self.__UpperBand[-1] - self.__LowerBand[-1]) / bar.getClose() self.__enterShort1 = cross.cross_below(self.__low, self.__LowerBand) > 0 self.__enterShort2 = bar.getClose() <= min( self.__close[-self.__closelength:]) self.__exitLong1 = (bar.getClose() < self.__ccMA) self.__exitLong2 = (self.__ccMA < self.__UpperBand[-1]) self.__exitShort1 = (bar.getClose() > self.__ccMA) self.__exitShort2 = (self.__ccMA > self.__LowerBand[-1]) self.__enterCon.append(self.__enter) self.__enterLongCon1.append(self.__enterLong1) self.__enterLongCon2.append(self.__enterLong2) self.__enterShortCon1.append(self.__enterShort1) self.__enterShortCon2.append(self.__enterShort2) self.__exitLongCon1.append(self.__exitLong1) self.__exitLongCon2.append(self.__exitLong2) self.__exitShortCon1.append(self.__exitShort1) self.__exitShortCon2.append(self.__exitShort2) # open and close ####################################################################### if self.__longPos is not None: if self.exitLongSignal(): self.__longPos.exitMarket() # if self.__shortPos is not None: # print 11 # self.info("intend long close") # print (self.__UpperBand[-1] - self.__LowerBand[-1]) / bar.getClose() elif self.__shortPos is not None: if self.exitShortSignal(): self.__shortPos.exitMarket() # self.info("intend short close") # print (self.__UpperBand[-1] - self.__LowerBand[-1]) / bar.getClose() else: if self.enterLongSignal(): shares = int(self.getBroker().getCash() * 0.2 / bars[self.__instrument].getPrice()) self.__longPos = self.enterLong(self.__instrument, shares) # self.info("intend long open") # print (self.__UpperBand[-1] - self.__LowerBand[-1]) / bar.getClose() elif self.enterShortSignal(): shares = int(self.getBroker().getCash() * 0.2 / bars[self.__instrument].getPrice()) self.__shortPos = self.enterShort(self.__instrument, shares)
def testCrossBelowOnce(self): values1 = self.__buildSeqDS([2, 2, 2, 2, 2, 2, 2]) values2 = self.__buildSeqDS([1, 1, 1, 10, 1, 1, 1]) # Check every 2 values. self.assertEqual(cross.cross_below(values1, values2, 0, 2), 0) self.assertEqual(cross.cross_below(values1, values2, 1, 3), 0) self.assertEqual(cross.cross_below(values1, values2, 2, 4), 1) self.assertEqual(cross.cross_below(values1, values2, 3, 5), 0) self.assertEqual(cross.cross_below(values1, values2, 4, 6), 0) self.assertEqual(cross.cross_below(values1, values2, 5, 7), 0) # Check every 3 values. self.assertEqual(cross.cross_below(values1, values2, 0, 3), 0) self.assertEqual(cross.cross_below(values1, values2, 1, 4), 1) self.assertEqual(cross.cross_below(values1, values2, 2, 5), 1) self.assertEqual(cross.cross_below(values1, values2, 3, 6), 0) self.assertEqual(cross.cross_below(values1, values2, 4, 7), 0) # Check for all values. self.assertEqual(cross.cross_below(values1, values2, 0, 7), 1) self.assertEqual(cross.cross_below(values1, values2, 0, -1), 1)