AttributeError: 'float'对象没有'getLow'属性,使用Pyalgotrade时出错
我正在尝试用Python编写一个随机振荡器,使用的是Pyalgotrade库里的列表功能。
Pyalgotrade库是一个用来回测股票交易策略的Python库。假设你有一个交易策略的想法,想用历史数据来评估一下这个策略的表现,看看它会如何运作。PyalgoTrade可以让你以最小的努力实现这个目标。
Python代码大致是这样的:
from pyalgotrade.tools import yahoofinance
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import stoch
from pyalgotrade import dataseries
from pyalgotrade.technical import ma
from pyalgotrade import technical
from pyalgotrade.technical import highlow
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
strategy.BacktestingStrategy.__init__(self, feed)
self.__stoch = stoch.StochasticOscillator(feed[instrument].getCloseDataSeries(),20, dSMAPeriod=3, maxLen=3)
self.__instrument = instrument
def onBars(self, bars):
bar = bars[self.__instrument]
self.info("%s %s" % (bar.getClose(), self.__stoch[-1]))
# Downdload then Load the yahoo feed from the CSV file
yahoofinance.download_daily_bars('AAPL', 2013, 'aapl.csv')
feed = yahoofeed.Feed()
feed.addBarsFromCSV("AAPL", "aapl.csv")
# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "AAPL")
myStrategy.run()
出现的错误是这样的,包括所有的追踪信息。
Traceback (most recent call last):
File "/Users/johnhenry/Desktop/simple_strategy.py", line 47, in <module>
myStrategy.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/strategy/__init__.py", line 519, in run
self.__dispatcher.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 102, in run
eof, eventsDispatched = self.__dispatch()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 90, in __dispatch
if self.__dispatchSubject(subject, smallestDateTime):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dispatcher.py", line 68, in __dispatchSubject
ret = subject.dispatch() is True
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/feed/__init__.py", line 101, in dispatch
dateTime, values = self.getNextValuesAndUpdateDS()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/feed/__init__.py", line 85, in getNextValuesAndUpdateDS
ds.appendWithDateTime(dateTime, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dataseries/bards.py", line 49, in appendWithDateTime
self.__closeDS.appendWithDateTime(dateTime, value.getClose())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/dataseries/__init__.py", line 134, in appendWithDateTime
self.getNewValueEvent().emit(self, dateTime, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/observer.py", line 59, in emit
handler(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/__init__.py", line 89, in __onNewValue
newValue = self.__eventWindow.getValue()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/stoch.py", line 60, in getValue
lowestLow, highestHigh = get_low_high_values(self.__barWrapper, self.getValues())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/stoch.py", line 42, in get_low_high_values
lowestLow = barWrapper.getLow(currBar)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyalgotrade/technical/stoch.py", line 31, in getLow
return bar_.getLow(self.__useAdjusted)
AttributeError: 'float' object has no attribute 'getLow'
1 个回答
0
你没有正确地构建随机振荡器(Stochastic Oscillator)。你传入的是收盘价的数据序列,但文档中说明你需要传入的是柱状图的数据序列:
self.__stoch = stoch.StochasticOscillator(feed[instrument], 20, dSMAPeriod=3, maxLen=3)