本文整理汇总了Python中pyalgotrade.technical.cross.cross_above函数的典型用法代码示例。如果您正苦于以下问题:Python cross_above函数的具体用法?Python cross_above怎么用?Python cross_above使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cross_above函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:Greenwicher,项目名称:pyalgotrade,代码行数:12,代码来源:technical_cross_test.py
示例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
开发者ID:willyfestugatto,项目名称:riobravo,代码行数:34,代码来源:smacrossalgotrade.py
示例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
开发者ID:dongdong2233,项目名称:fx_systrade,代码行数:28,代码来源:pyalgo_fx_orig.py
示例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)
开发者ID:RyanEggert,项目名称:financial-signal-processing,代码行数:26,代码来源:strategies.py
示例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)
开发者ID:charnugagoo,项目名称:pyalgotrade,代码行数:8,代码来源:multi_instrument_strategy_test.py
示例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)
开发者ID:deamoon,项目名称:meteopt,代码行数:8,代码来源:stoploss.py
示例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()
开发者ID:sv112,项目名称:pyAlgoTrade,代码行数:8,代码来源:sma_crossover.py
示例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()
开发者ID:greatGregLiu,项目名称:pytrade,代码行数:9,代码来源:smacross_strategy.py
示例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()
开发者ID:luosz,项目名称:quant,代码行数:10,代码来源:my_sma_crossover.py
示例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
开发者ID:llmofang,项目名称:backtest,代码行数:11,代码来源:bitcoinbacktest.py
示例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
开发者ID:llmofang,项目名称:backtest,代码行数:11,代码来源:bollinger_band.py
示例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()
开发者ID:hezhenke,项目名称:AshareBackTest,代码行数:11,代码来源:testPosition.py
示例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()
开发者ID:lagisettyk,项目名称:roboquant,代码行数:21,代码来源:simple_strategy.py
示例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)
开发者ID:Greenwicher,项目名称:pyalgotrade,代码行数:26,代码来源:technical_cross_test.py
示例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()
开发者ID:deamoon,项目名称:meteopt,代码行数:13,代码来源:smacross_strategy.py
示例16: calcSignal
def calcSignal(self):
self.buySignal,self.sellSignal = {},{}
for instrument in self.instruments:
self.buySignal[instrument],self.sellSignal[instrument] = False,False
#if(self.longAllowed):
if self.longPosition[instrument] is None:
#mid 无多仓,检查是否需要开多仓
if cross.cross_above(self.__sma[instrument], self.__lma[instrument]) > 0:
self.buySignal[instrument] = True
#if(self.shortAllowed ):
if self.shortPosition[instrument] is None:
if cross.cross_below(self.__sma[instrument], self.__lma[instrument]) > 0:
self.sellSignal[instrument] = True
开发者ID:UpSea,项目名称:PyAlgoTradeMid,代码行数:13,代码来源:Signal.py
示例17: enterLongSignal
def enterLongSignal (self) :
if self.__lastLongPos is not None:
if self.__barNum-self.__lastLongPos<60:
return 0
if self.__UpperBand[-1-self.__circ] is None:
return 0
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 1
else:
return 0
开发者ID:llmofang,项目名称:backtest,代码行数:14,代码来源:bollinger_band.py
示例18: onBars
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)
开发者ID:Greenwicher,项目名称:pyalgotrade,代码行数:14,代码来源:smacrossover_strategy_test.py
示例19: exitShortSignal
def exitShortSignal(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
elif self.__macd[-1]<=self.__LowerBand[-1] and self.__macdMin==self.__macd[-1]:
return True
elif self.__macd[-1]<=self.__LowerBand[-1]*1.001 and self.__macd[-1]<0.11:
return True
else:
return False
开发者ID:llmofang,项目名称:backtest,代码行数:15,代码来源:bollingerband_macd.py
示例20: onBars
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()
#self.info("sell %s" % (bars.getDateTime()))
if self.__position is None:
if cross.cross_above(self.__ma1, self.__ma2) > 0:
shares = int(self.getBroker().getEquity() * 0.2 / bars[self.__instrument].getPrice())
self.__position = self.enterLong(self.__instrument, shares)
开发者ID:zhangkj,项目名称:pyalgotrade-cn,代码行数:15,代码来源:doubleMA.py
注:本文中的pyalgotrade.technical.cross.cross_above函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论