NameError:全局名称'指标'没有在Python中使用Pyalgotrade定义

时间:2014-04-10 06:15:19

标签: python global trading pyalgotrade

我尝试使用Pyalgotrade库中的list函数在python中编写Ultimate Oscillator。

我的代码如下:

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

找到

Ultimate Oscillator:

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

1 个答案:

答案 0 :(得分:0)

您没有导入indicator,也没有通过其定义的模块引用它。更改此信息:

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

分为:

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

应该没事。