Pyalgotrade - TA-LIB - 指标 Returns "NONE"
Pyalgotrade - TA-LIB - Indicator Returns "NONE"
我正在与 Pyalgotrade 合作测试 python 中的交易策略。 Pyalgotrade 允许使用名为 TA-LIB 的库,这是一个技术分析库。出于某种原因,当我使用 PPO 指标时,它 returns "None"。该指标接受一些参数:(http://gbeced.github.io/pyalgotrade/docs/v0.12/html/talib.html)
我提供了一个输出片段,目前只是当天的收盘价以及该指标的输出。 'DDD' 是我一直在测试的代码。
我一直在努力让它工作的时间比我承认的要长。
我该如何解决这个问题?
输出:
2016-11-08 00:00:00 strategy [INFO] 13.56,None
2016-11-09 00:00:00 strategy [INFO] 13.77,None
2016-11-10 00:00:00 strategy [INFO] 14.06,None
2016-11-11 00:00:00 strategy [INFO] 14.71,None
2016-11-14 00:00:00 strategy [INFO] 14.3,None
2016-11-15 00:00:00 strategy [INFO] 13.91,None
这是我的代码:
from pyalgotrade import strategy
from pyalgotrade.tools import yahoofinance
from pyalgotrade import talibext
from pyalgotrade.talibext import indicator
import talib
import numpy
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
super(MyStrategy, self).__init__(feed, 1000)
self.__position = None
self.__instrument = instrument
self.setUseAdjustedValues(True)
self.__prices = feed[instrument].getPriceDataSeries()
self.__PPO = talibext.indicator.PPO(feed,0,12,26,9)
def onEnterOk(self, position):
execInfo = position.getEntryOrder().getExecutionInfo()
self.info("BUY at $%.2f" % (execInfo.getPrice()))
def onEnterCanceled(self, position):
self.__position = None
def onExitOk(self, position):
execInfo = position.getExitOrder().getExecutionInfo()
self.info("SELL at $%.2f" % (execInfo.getPrice()))
self.__position = None
def onExitCanceled(self, position):
# If the exit was canceled, re-submit it.
self.__position.exitMarket()
def onBars(self, bars):
bar = bars[self.__instrument]
self.info("%s,%s" % (bar.getClose(),self.__PPO))
def run_strategy(inst):
# Load the yahoo feed from the CSV file
feed = yahoofinance.build_feed([inst],2015,2016, ".")
# Evaluate the strategy with the feed.
myStrategy = MyStrategy(feed, inst)
myStrategy.run()
print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()
def main():
instruments = ['ddd']
for inst in instruments:
run_strategy(inst)
if __name__ == '__main__':
main()
你传递的参数有误。这是 PPO 函数签名:
def PPO(ds, count, fastperiod=-2**31, slowperiod=-2**31, matype=0):
ds
类型为BarDataSeries
,count
指定要从尾部计算多长的数据。计算成功会return一个numpy array
而 talibext 指标只计算一次,在输入新柱时不会计算新结果。
因此您需要在每次 onBars
调用中计算 PPO。
def __init__(self, feed, instrument):
...
self.__feed = feed
def onBars(self, bars):
feed = self.__feed
self.__PPO = talibext.indicator.PPO(feed[self.__instrument], len(feed[self.__instrument]),12,26, matype=0)
bar = bars[self.__instrument]
self.info("%s,%s" % (bar.getClose(),self.__PPO[-1]))
我正在与 Pyalgotrade 合作测试 python 中的交易策略。 Pyalgotrade 允许使用名为 TA-LIB 的库,这是一个技术分析库。出于某种原因,当我使用 PPO 指标时,它 returns "None"。该指标接受一些参数:(http://gbeced.github.io/pyalgotrade/docs/v0.12/html/talib.html)
我提供了一个输出片段,目前只是当天的收盘价以及该指标的输出。 'DDD' 是我一直在测试的代码。
我一直在努力让它工作的时间比我承认的要长。 我该如何解决这个问题?
输出:
2016-11-08 00:00:00 strategy [INFO] 13.56,None
2016-11-09 00:00:00 strategy [INFO] 13.77,None
2016-11-10 00:00:00 strategy [INFO] 14.06,None
2016-11-11 00:00:00 strategy [INFO] 14.71,None
2016-11-14 00:00:00 strategy [INFO] 14.3,None
2016-11-15 00:00:00 strategy [INFO] 13.91,None
这是我的代码:
from pyalgotrade import strategy
from pyalgotrade.tools import yahoofinance
from pyalgotrade import talibext
from pyalgotrade.talibext import indicator
import talib
import numpy
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
super(MyStrategy, self).__init__(feed, 1000)
self.__position = None
self.__instrument = instrument
self.setUseAdjustedValues(True)
self.__prices = feed[instrument].getPriceDataSeries()
self.__PPO = talibext.indicator.PPO(feed,0,12,26,9)
def onEnterOk(self, position):
execInfo = position.getEntryOrder().getExecutionInfo()
self.info("BUY at $%.2f" % (execInfo.getPrice()))
def onEnterCanceled(self, position):
self.__position = None
def onExitOk(self, position):
execInfo = position.getExitOrder().getExecutionInfo()
self.info("SELL at $%.2f" % (execInfo.getPrice()))
self.__position = None
def onExitCanceled(self, position):
# If the exit was canceled, re-submit it.
self.__position.exitMarket()
def onBars(self, bars):
bar = bars[self.__instrument]
self.info("%s,%s" % (bar.getClose(),self.__PPO))
def run_strategy(inst):
# Load the yahoo feed from the CSV file
feed = yahoofinance.build_feed([inst],2015,2016, ".")
# Evaluate the strategy with the feed.
myStrategy = MyStrategy(feed, inst)
myStrategy.run()
print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()
def main():
instruments = ['ddd']
for inst in instruments:
run_strategy(inst)
if __name__ == '__main__':
main()
你传递的参数有误。这是 PPO 函数签名:
def PPO(ds, count, fastperiod=-2**31, slowperiod=-2**31, matype=0):
ds
类型为BarDataSeries
,count
指定要从尾部计算多长的数据。计算成功会return一个numpy array
而 talibext 指标只计算一次,在输入新柱时不会计算新结果。
因此您需要在每次 onBars
调用中计算 PPO。
def __init__(self, feed, instrument):
...
self.__feed = feed
def onBars(self, bars):
feed = self.__feed
self.__PPO = talibext.indicator.PPO(feed[self.__instrument], len(feed[self.__instrument]),12,26, matype=0)
bar = bars[self.__instrument]
self.info("%s,%s" % (bar.getClose(),self.__PPO[-1]))