0

我尝试使用 Pyalgotrade 库中的列表函数在 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'
4

1 回答 1

0

您没有正确构建 StochasticOscillator。您传递了关闭数据序列,并且如文档中所述,您需要传递 bar 数据序列:

self.__stoch = stoch.StochasticOscillator(feed[instrument], 20, dSMAPeriod=3, maxLen=3)
于 2014-04-11T23:54:43.713 回答