我正在使用python来设计一些交易策略。我的图书馆主要包括Pyalgotrade和TA-lib。我从下面的代码中得到了一个错误。
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
from pyalgotrade import bar
from pyalgotrade.talibext import indicator
import numpy
import talib
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
strategy.BacktestingStrategy.__init__(self, feed)
self.__instrument = instrument
barDs = self.getFeed().getDataSeries("002389.SZ")
self.__fastk, self.__fastd = indicator.STOCHF(barDs, 24, 3)
self.__bias = self.__fastk * 25
self.__DD = indicator.EMA(self.__bias, 4)
self.__LL = (self.__DD - indicator.LOW(self.__DD,21))/(indicator.MAX(self.__DD,21) - indicator.LOW(self.__DD,21))
def onBars(self, bars):
bar = bars[self.__instrument]
self.info("%0.2f, %0.2f" % (bar.getClose(), self.__LL[-1]))
# Downdload then Load the yahoo feed from the CSV file
yahoofinance.download_daily_bars('002389.SZ', 2013, '002389.csv')
feed = yahoofeed.Feed()
feed.addBarsFromCSV("002389.SZ", "002389.csv")
# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "002389.SZ")
myStrategy.run()
错误是:
>>> ================================ RESTART ================================
>>>
>>>
什么都没打印出来。
我认为错误可能是由名为self ._ LL的变量引起的。但我不知道如何以正确的方式写自我。 _DD和self .__ LL。有人能给我一个帮助吗?
答案 0 :(得分:0)
self.__position is None
不会创建该属性。使用self.__position = None
。还要记住,具有2个前导下划线的属性被视为“私有”(至少在CPython实现中),并且为防止不期望的,最终访问被内部损坏到_<ClassName>__<AttributeName>
。
修正第run_strategy()
行。此时类MyStrategy
尚未完全定义。