Pyalgotrade 出错
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()
我收到以下错误:
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'
我无法找出我的代码的问题,pyalgotrade 上的教程对我没有帮助。
问题是您使用的是常规 Feed class 而不是 BarFeed。试试这个:https://github.com/gbeced/pyalgotrade/blob/master/pyalgotrade/barfeed/csvfeed.py#L190
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()
我收到以下错误:
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'
我无法找出我的代码的问题,pyalgotrade 上的教程对我没有帮助。
问题是您使用的是常规 Feed class 而不是 BarFeed。试试这个:https://github.com/gbeced/pyalgotrade/blob/master/pyalgotrade/barfeed/csvfeed.py#L190