本文整理汇总了Python中pyalgotrade.technical.cross.cross_above函数的典型用法代码示例。如果您正苦于以下问题:Python cross_above函数的具体用法?Python cross_above怎么用?Python cross_above使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cross_above函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCrossAboveWithSMA
def testCrossAboveWithSMA(self):
ds1 = dataseries.SequenceDataSeries()
ds2 = dataseries.SequenceDataSeries()
sma1 = ma.SMA(ds1, 15)
sma2 = ma.SMA(ds2, 25)
for i in range(100):
ds1.append(i)
ds2.append(50)
if i == 58:
self.assertEqual(cross.cross_above(sma1[:], sma2[:], -2, None), 1)
else:
self.assertEqual(cross.cross_above(sma1[:], sma2[:], -2, None), 0)
示例2: onBars
def onBars(self, bars):
closeDs = self.getFeed().getDataSeries("petr4").getCloseDataSeries()
self.sma17 = talibext.MA(closeDs,len(closeDs), timeperiod=5)
self.sma34 = talibext.MA(closeDs, len(closeDs), timeperiod=20)
bar_close = bars.getBar(self.__instrument).getClose()
bar_date = bars.getBar(self.__instrument).getDateTime()
if self.__position == None:
if cross.cross_above(self.sma17, self.sma34) > 0:
self.__position = self.enterLongLimit(self.__instrument, bar_close, 10, True)
self.stop_loss = bar_close * 0.97
self.stop_gain = bar_close * 1.05
self.last_buy_price = bar_close
self.last_buy_date = bar_date
print "%s - try buy at %6.2f - stop loss at %6.2f / stop gain at %6.2f"%(bar_date, bar_close, self.stop_loss, self.stop_gain)
elif bar_close <= self.stop_loss:
stop_execute = self.stop_loss * 0.995
self.__position.exit(self, stop_execute, stop_execute)
print "%s - stoppe d (loss) at %6.2f - stop loss: %6.2f"%(bar_date, stop_execute, self.stop_loss)
self.stop_loss = 0
self.stop_gain = 0
self.last_buy_price = 0
elif bar_close >= self.stop_gain and self.stop_gain > 0:
self.stop_loss = self.stop_gain * 0.99
self.stop_gain = self.stop_gain * 1.05
print "%s - at %6.2f raised stop loss to %6.2f and stop gain to %6.2f"%(bar_date, bar_close, self.stop_loss, self.stop_gain)
elif (bar_date - self.last_buy_date).days > 60:
self.__position.cancelExit()
print "%s - trend timed out"%(bar_date)
self.stop_loss = 0
self.stop_gain = 0
self.last_buy_price = 0
self.last_buy_date = 0
示例3: onBars
def onBars(self, bars):
if self.__position != None and self.__position.getReturn() < -0.05:
self.__position.exitMarket()
# If a position was not opened, check if we should enter a long position.
if self.__position is None:
ds = self.getFeed().getDataSeries(self.__instrument).getCloseDataSeries()
if self.touchBottom(): # and LINEARREG(ds, 20)[1] > 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.enterShort(self.__instrument, shares, True)
self.__pos_kind = "Short"
print str(bars[self.__instrument].getDateTime()) + " " + "buy Short"
elif self.touchTop(): # and LINEARREG(ds, 20)[1] < 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)
self.__pos_kind = "Long"
print str(bars[self.__instrument].getDateTime()) + " " + "buy Long"
return
# Check if we have to exit the position.
elif (not self.__position.exitActive()):
if self.__pos_kind == "Long" and (cross.cross_below(self.__prices, self.__sma, -2) > 0 or self.touchBottom()):
self.__position.exitMarket() #Long exit
print str(bars[self.__instrument].getDateTime()) + " " + "exit " + self.__pos_kind
elif self.__pos_kind == "Short" and (cross.cross_above(self.__prices, self.__sma, -2) > 0 or self.touchTop()):
self.__position.exitMarket() #Short exit
print str(bars[self.__instrument].getDateTime()) + " " + "exit " + self.__pos_kind
示例4: onBars
def onBars(self, bars):
for instrument, bar in bars.items():
infoout = None
if cross.cross_below(self.__shorttrix[instrument],
self.__longtrix[instrument]) > 0:
# If the derivative crosses above zero (deriv - -> +),
# buy the instrument.
now_cash, now_shares = self.inventory(instrument)
buyqty = self.buyamount(
instrument, bar.getClose(), bar.getVolume(), now_cash, .2)
self.marketOrder(instrument, buyqty)
infoout = 'Order %d shares of %s @$%.2f. COH $%.2f' % (
buyqty, instrument, bar.getClose(), now_cash)
elif cross.cross_above(self.__shorttrix[instrument],
self.__longtrix[instrument]) > 0:
# If the derivative crosses below zero (deriv + -> -),
# sell the instrument.
now_cash, now_shares = self.inventory(instrument)
if now_shares > 0:
# Sell all shares
self.marketOrder(instrument, -now_shares)
infoout = 'Sell %d shares of %s @$%.2f. COH $%.2f' % (
now_shares, instrument, bar.getClose(), now_cash)
if infoout and self.printinfo:
self.info(infoout)
示例5: onBars
def onBars(self, bars):
if bars.getBar(self.__lead):
if cross.cross_above(self.__adjClose, self.__slowSMA) == 1 and self.__pos == 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 != None:
self.exitPosition(self.__pos)
示例6: onBars
def onBars(self, bars):
shares = self.getBroker().getShares(self.__instrument)
# If a position was not opened, check if we should enter a long position.
if shares == 0:
if cross.cross_above(self.__closeDS, self.__sma) > 0:
# Enter a buy market order for 10 shares. The order is good till canceled.
self.order(self.__instrument, 10, goodTillCanceled=True)
示例7: onBars
def onBars(self, bars):
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())
self.__position = self.enterLong(self.__instrument, shares, True)
elif not self.__position.exitActive() and cross.cross_below(self.__prices,self.__sma) >0:
self.__position.exitMarket()
示例8: onBars
def onBars(self, bars):
# If a position was not opened, check if we should enter a long position.
if self.__position == None:
if cross.cross_above(self.__adjClose, self.__sma) > 0:
# Enter a buy market order for 10 shares. The order is good till canceled.
self.__position = self.enterLong(self.__instrument, 10, True)
# Check if we have to exit the position.
elif cross.cross_below(self.__adjClose, self.__sma) > 0:
self.__position.exit()
示例9: onBars
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.__exit_sma) > 0:
self.__position.exitMarket()
示例10: enterLongSignal
def enterLongSignal (self) :
if self.__UpperBand[-1-self.__circ] is None:
return False
m1 = 0
for i in range(self.__circ):
if self.__macd[-i-1] <= self.__LowerBand[-i-2]:
m1 += 1
if m1 >= self.__circ-1 and cross.cross_above(self.__macd,self.__LowerBand)>0:
return True
else:
return False
示例11: exitShortSignal
def exitShortSignal(self):
if self.__UpperBand[-1-self.__circ] is None:
return False
m1 = 0
for i in range(self.__circ):
if self.__close[-i-1] <= self.__LowerBand[-i-2]:
m1 += 1
if m1 >= self.__circ-1 and cross.cross_above(self.__close,self.__LowerBand)>0:
return True
else:
return False
示例12: handle_data
def handle_data(self,bars):
for instrument in bars.getInstruments():
# If a position was not opened, check if we should enter a long position.
if self.__position[instrument] is None:
if cross.cross_above(self.__prices[instrument], self.__sma[instrument]) > 0:
shares = int(self.getBroker().getCash() * 0.9 / bars[instrument].getPrice())
# Enter a buy market order. The order is good till canceled.
self.__position[instrument] = self.enterLong(instrument, shares, True)
# Check if we have to exit the position.
elif not self.__position[instrument].exitActive() and cross.cross_below(self.__prices[instrument], self.__sma[instrument]) > 0:
self.__position[instrument].exitMarket()
示例13: onBars
def onBars(self, bars):
inst0_cond = cross.cross_above(self.__prices[self.__instrumentList[0]], self.__sma[self.__instrumentList[0]])
inst2_cond = cross.cross_above(self.__prices[self.__instrumentList[2]], self.__sma[self.__instrumentList[2]])
# If a position was not opened, check if we should enter a long position.
if not self.__positions:
if inst0_cond > 0 or inst2_cond > 0:
if inst0_cond > 0:
shares_0 = int(self.getBroker().getCash() * 0.6 / bars[self.__instrumentList[0]].getPrice())
shares_2 = int(self.getBroker().getCash() * 0.3 / bars[self.__instrumentList[2]].getPrice())
else:
shares_0 = int(self.getBroker().getCash() * 0.3 / bars[self.__instrumentList[0]].getPrice())
shares_2 = int(self.getBroker().getCash() * 0.6 / bars[self.__instrumentList[2]].getPrice())
# Enter a buy market order. The order is good till canceled.
print "%%%%%$$$$: ", shares_0, shares_2
self.__positions[self.__instrumentList[0]] = self.enterLong(self.__instrumentList[0], int(shares_0), True)
self.__positions[self.__instrumentList[2]] =self.enterLong(self.__instrumentList[2], int(shares_2), True)
self.__activePosition = self.__instrumentList[0]
# Check if we have to exit the position.
elif cross.cross_below(self.__prices[self.__activePosition], self.__sma[self.__activePosition]) > 0:
self.__position[self.__instrumentList[0]].exitMarket()
self.__position[self.__instrumentList[2]].exitMarket()
示例14: testCrossAboveMany
def testCrossAboveMany(self):
count = 100
values1 = [-1 if i % 2 == 0 else 1 for i in range(count)]
values2 = [0 for i in range(count)]
# Check first value
self.assertEqual(cross.cross_above(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_above(values1, values2, i - period + 1, i + 1), 0)
else:
self.assertEqual(cross.cross_above(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_above(values1, values2, i - period + 1, i + 1), 1)
else:
self.assertEqual(cross.cross_above(values1, values2, i - period + 1, i + 1), 2)
# Check for all values.
self.assertEqual(cross.cross_above(values1, values2, 0, count), count / 2)
示例15: onBars
def onBars(self, bars):
if self.__position is None:
if cross.cross_above(self.__Close, self.__sma) > 0:
self.buyPrice = bars.getBar("btc").getClose()
quantity = self.getBroker().getCash() / bars.getBar("btc").getClose() * 0.99
self.__position = self.enterLong(self.__instrument, quantity)
self.numOrder += 1
elif cross.cross_below(self.__Close, self.__sma) > 0:
# if (abs(self.buyPrice - bars.getBar("btc").getClose()) > 0.002 * (bars.getBar("btc").getClose())):
if (abs(self.buyPrice - bars.getBar("btc").getClose()) > 10):
self.__position.exitMarket()