2

以下代码在 python shell 中运行良好,显示提要对象的内容:

from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed


class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        strategy.BacktestingStrategy.__init__(self, feed)
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info(bar.getClose())



feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl","data/bistampTicker.csv")

myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()

但是,它在 Django 视图中的执行会导致以下错误:

'function' object has no attribute 'BacktestingStrategy'

其中 BacktestingStrategy 是在 python 模块的 strategy 文件夹内的 __init__.py 文件中定义的类,位于 python 路径内。

我对这个问题的理解是 django 没有读取 __ init__.py 文件,因此没有正确导入模块(一个 pyalgotrade 模块)。

有没有办法告诉 Django 这样做?

在此先感谢,并对这个愚蠢的问题感到抱歉。

干杯

4

4 回答 4

2

Django 只是 Python:在读取模块方面,在普通 Python 中没有发生在 Django 中不会发生的任何事情。

无论如何,您对问题的诊断是错误的:未能读取模块不会给出您看到的错误。错误消息说函数对象不包含属性 TestingStrategy。这意味着您已将某处重新定义strategy为函数,而不是您最初导入的模块。

于 2014-08-27T18:07:32.653 回答
1

您正在从那里定义strategy的模块导入函数。pyalgotrade子模块 pyalgotrade.strategy被此功能隐藏。

可能你想写:

import pyalgotrade.strategy as strategy

在第一行。

于 2014-08-27T18:14:23.967 回答
1

修改库并不是真正的解决方案,它只是一个 hack。看看你得到的错误:

'function' object has no attribute 'BacktestingStrategy'

这不是导入问题:您正在将某个地方重新定义strategy为函数。不仅如此,我刚刚安装pyalgotrade并顺利完成了以下操作:

>>> from pyalgotrade import strategy
>>> strategy.BacktestingStrategy
<class 'pyalgotrade.strategy.BacktestingStrategy'>

查看您导入的其他任何内容,并确保您的所有名称都是正确的。

请记住,Django 只是 python;它没有做任何不同的事情,做错了什么。(这给了你一个很好的学习机会!)

于 2014-08-27T18:52:12.843 回答
0

尝试这样做:

from pyalgotrade import strategy
print strategy

让我们知道你得到了什么。

于 2014-08-30T01:05:20.517 回答