pythondjangopython-2.7pyalgotrade

Importing python module in Django: __init__.py classes not loaded


The following code works fine in a python shell, displaying the content of the feed object:

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

However, its execution in a Django views leads to the following error:

'function' object has no attribute 'BacktestingStrategy'

Where the BacktestingStrategy is a class defined in the __ init__.py file inside the strategy folder of the python module, inside the python path.

My understanding of the problem is that django doesn't read the __ init__.py file, thus not importing the module correctly (a pyalgotrade module).

Is there a way to tell Django to do so?

Thanks in advance and sorry for the noobish question.

Cheers


Solution

  • Modifying the library isn't really a solution, its just a hack. Have a look at the error you're getting:

    'function' object has no attribute 'BacktestingStrategy'
    

    This isn't an import problem: somewhere you are redefining strategy as a function. Not only that, I just installed pyalgotrade and did the following without a hitch:

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

    Have a look at anything else you've imported and make sure you have all of your names straight.

    Remember, Django is just python; it isn't doing anything different, you are doing something wrong. (Which gives you a wonderful opportunity to learn something!)