pythonpyalgotrade

ERROR with Pyalgotrade


from pyalgotrade import strategy
from pyalgotrade.feed import csvfeed
from pyalgotrade.technical import ma
from pyalgotrade.bar import Frequency

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed, 1000)
        # We want a 15 period SMA over the closing prices.
        self.__instrument = instrument
        self.__sma = ma.SMA(feed[instrument].getDataSeries(instrument), 15)

    def onBars(self, bars):
        bar = bars[self.__instrument]
        print "%s: %s %s" % (bar.getDateTime(), self.__sma[-1])

# Load the yahoo feed from the CSV file
feed = csvfeed.Feed("Date","%Y-%m-%d %H:%M")
feed.addValuesFromCSV("test.csv")
# Evaluate the strategy with the feed's bars.
rules = MyStrategy(feed, "Open")
rules.run()

I'm getting following error:

Traceback (most recent call last):
  File "algotrade.py", line 21, in <module>
    rules = MyStrategy(feed, "Open")
  File "algotrade.py", line 11, in __init__
    self.__sma = ma.SMA(feed[instrument].getDataSeries(instrument), 15)
AttributeError: 'SequenceDataSeries' object has no attribute 'getDataSeries'

I cant figure out the problem of my code and the tutorial on pyalgotrade is not helpful for me.


Solution

  • The problem is that you're using a regular Feed class instead of a BarFeed. Try using this: https://github.com/gbeced/pyalgotrade/blob/master/pyalgotrade/barfeed/csvfeed.py#L190