AttributeError:' float'对象没有属性' getLow'在Python中使用Pyalgotrade

时间:2014-04-08 16:43:14

标签: python trading pyalgotrade

我尝试使用Pyalgotrade库中的list函数在python中编写一个随机振荡器。

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 :(得分:0)

您没有正确构建StochasticOscillator。您传递了关闭的数据集,并且如文档中所述,您需要传入条形数据集:

self.__stoch = stoch.StochasticOscillator(feed[instrument], 20, dSMAPeriod=3, maxLen=3)