我一直在谷歌搜索一段时间,但我仍然无法找到解决方案,甚至无法确定问题,老实说。
我的Python和Pyalgotrade的安装是正确的,正如成功导入所证实的那样。
尽管如此,我无法在教程中运行示例代码,它始终抛出:
AttributeError: MyStrategy instance has no attribute 'info'
以下是示例代码:
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
strategy.BacktestingStrategy.__init__(self, feed)
self.__instrument = instrument
def onBars(self, bars):
bar = bars[self.__instrument]
self.info(bar.getClose())
# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")
# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()
iPython Notebook输出:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-f786d1b471f7> in <module>()
18 # Evaluate the strategy with the feed's bars.
19 myStrategy = MyStrategy(feed, "orcl")
---> 20 myStrategy.run()
/usr/local/lib/python2.7/site-packages/pyalgotrade/strategy/__init__.pyc in run(self)
398 self.onStart()
399
--> 400 self.__dispatcher.run()
401
402 if self.__feed.getCurrentBars() != None:
/usr/local/lib/python2.7/site-packages/pyalgotrade/observer.pyc in run(self)
139 subject.start()
140
--> 141 while not self.__stopped and self.__dispatch():
142 pass
143 finally:
/usr/local/lib/python2.7/site-packages/pyalgotrade/observer.pyc in __dispatch(self)
131 nextDateTime = subject.peekDateTime()
132 if nextDateTime == None or nextDateTime == smallestDateTime:
--> 133 subject.dispatch()
134 return ret
135
/usr/local/lib/python2.7/site-packages/pyalgotrade/feed/__init__.pyc in dispatch(self)
95 dateTime, values = self.getNextValuesAndUpdateDS()
96 if dateTime != None:
---> 97 self.__event.emit(dateTime, values)
98
99 def getKeys(self):
/usr/local/lib/python2.7/site-packages/pyalgotrade/observer.pyc in emit(self, *parameters)
51 self.__emitting = True
52 for handler in self.__handlers:
---> 53 handler(*parameters)
54 self.__emitting = False
55 self.__applyChanges()
/usr/local/lib/python2.7/site-packages/pyalgotrade/strategy/__init__.pyc in __onBars(self, dateTime, bars)
386
387 # 1: Let the strategy process current bars and place orders.
--> 388 self.onBars(bars)
389
390 # 2: Place the necessary orders for positions marked to exit on session close.
<ipython-input-1-f786d1b471f7> in onBars(self, bars)
10 def onBars(self, bars):
11 bar = bars[self.__instrument]
---> 12 self.info(bar.getClose())
13
14 # Load the yahoo feed from the CSV file
AttributeError: MyStrategy instance has no attribute 'info'
有没有人至少暗示问题可能是什么?
答案 0 :(得分:0)
您使用的是哪个版本的PyAlgoTrade?
import pyalgotrade
print pyalgotrade.__version__
答案 1 :(得分:0)
复制自问题:
因此,我将PyAlgoTrade更新为0.15,现在示例代码正常工作。我还没有调查错误的原因,但可以肯定地说0.15工作正常。