【问题标题】:ImportError from using the trading strategy in PyAlgoTrade tutorial在 PyAlgoTrade 教程中使用交易策略的 ImportError
【发布时间】:2014-04-10 22:52:10
【问题描述】:

我在 Pyalgotrade 中运行简单的 Python 编写的交易策略时遇到导入错误。

from pyalgotrade.tools import yahoofinance
yahoofinance.download_daily_bars('orcl', 2000, 'orcl-2000.csv')

from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import ma


class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed)
        # We want a 15 period SMA over the closing prices.
        self.__sma = ma.SMA(feed[instrument].getCloseDataSeries(), 15)
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info("%s %s" % (bar.getClose(), self.__sma[-1]))

# 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()

错误如下所示。

>>>Traceback (most recent call last):
   File "/Users/johnhenry/Desktop/pyalgotrade2.py", line 1, in <module>
    from pyalgotrade import strategy
   File "/Users/johnhenry/Desktop/pyalgotrade.py", line 1, in <module>
    from pyalgotrade import strategy
   ImportError: cannot import name strategy

我确定我有这个名为 pyalgotrade 的库。

【问题讨论】:

    标签: python pyalgotrade


    【解决方案1】:

    您将自己的程序命名为pyalgotrade.py

    File "/Users/johnhenry/Desktop/pyalgotrade.py"
    

    所以 Python 认为这就是你的意思。将您的程序重命名为其他名称,删除您可能拥有的所有 pyalgotrade.pycpyalgotrade.pyo 文件,然后重新启动您的解释器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 2023-02-20
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多