设计交易策略时python出现未知错误

2024-04-27 05:10:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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()

错误是:

^{pr2}$

没有打印出来。在

我认为这个错误可能是由名为self的变量引起的。但我不知道怎么写自己。以正确的方式添加和自我。有人能帮我个忙吗?在


Tags: thefromimportselffeed错误bardd
1条回答
网友
1楼 · 发布于 2024-04-27 05:10:30

self.__position is None不创建属性。使用self.__position = None。还要记住,带有2个前导下划线的属性被认为是“私有的”(至少在CPython实现中是这样),为了防止不希望的访问,最终的访问在内部被混乱成_<ClassName>__<AttributeName>。在

修复run_strategy()行。此时,MyStrategy类尚未完全定义。在

相关问题 更多 >