下面是一个SMA交叉策略的例子,我们使用self.setUseAdjustedValues(True)的原因是什么?它是如何工作的?
from pyalgotrade import strategy
from pyalgotrade.technical import ma
from pyalgotrade.technical import cross
class SMACrossOver(strategy.BacktestingStrategy):
def __init__(self, feed, instrument, smaPeriod):
strategy.BacktestingStrategy.__init__(self, feed)
self.__instrument = instrument
self.__position = None
# We'll use adjusted close values instead of regular close values.
self.setUseAdjustedValues(True)
self.__prices = feed[instrument].getPriceDataSeries()
self.__sma = ma.SMA(self.__prices, smaPeriod)
def getSMA(self):
return self.__sma
def onEnterCanceled(self, position):
self.__position = None
def onExitOk(self, position):
self.__position = None
def onExitCanceled(self, position):
# If the exit was canceled, re-submit it.
self.__position.exitMarket()
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()发布于 2015-04-06 01:17:09
如果您使用的是定期收盘价,而不是调整后的收盘价,那么您的策略可能会对价格变化做出反应,而这些价格变化实际上是股票分割的结果,而不是常规交易活动引起的价格变化。
发布于 2019-03-02 12:26:57
据我所知,并试图简化它,假设一份价格是100。
->第二天的股票分成1:2意味着2股,每股50股。这种价格变化并不是由于交易活动造成的,也没有交易涉及到更低的价格。因此,setUseAdjustedValues(True)处理这种情况。
https://stackoverflow.com/questions/29454419
复制相似问题