NameError: 使用Pyalgotrade时全局名称'指标'未定义

0 投票
1 回答
892 浏览
提问于 2025-04-18 02:09

我正在尝试用Python编写一个终极振荡器,使用的是Pyalgotrade库里的列表功能。

我的代码如下:

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 import talibext
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.__ultosc = indicator.ULTOSC(barDs, 36)

    bar = bars[self.__instrument]
    self.info("%0.2f, %0.2f" % (bar.getClose(), self.__ultosc[-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()

然后我遇到了这样的错误:

  File "/Users/johnhenry/Desktop/untitled.py", line 23, in onBars
    self.__ultosc = indicator.ULTOSC(barDs, 36)
NameError: global name 'indicator' is not defined

这个函数可以在这里找到:http://gbeced.github.io/pyalgotrade/docs/v0.15/html/talib.html

终极振荡器的用法:

pyalgotrade.talibext.indicator.ULTOSC(barDs, count, timeperiod1=-2147483648, timeperiod2=-2147483648, timeperiod3=-2147483648)

1 个回答

0

你没有导入 indicator,也没有通过它所在的模块来引用它。把这个:

self.__ultosc = indicator.ULTOSC(barDs, 36)

改成:

self.__ultosc = talibext.indicator.ULTOSC(barDs, 36)

这样就可以了。

撰写回答