2

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

Bertrand
  • 23
  • 3
  • I searched for hours and found the solution minutes after submitting the post, modifying the library so that the classes of strategy are defined in strategy.py instead of strategy/__ init__.py. Not very elegant but does the job. – Bertrand Aug 27 '14 at 18:20

4 Answers4

2

Django is just Python: there is nothing that happens in plain Python that does not happen in Django as regards reading of modules.

In any case, your diagnosis of the problem is wrong: failing to read the module would not give the error you see. The error message says that a function object does not contain the attribute TestingStrategy. That would imply that somewhere you have redefined strategy to be a function, rather than the module you originally imported.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Well it does reach the directory of the module since it doesn't complain that strategy is not defined. It behaves as if the BacktestingStrategy class did'nt exist while it is defined in the __ init__.py file. Anyway, I solved it making a modified library where all the classes are defined in strategy.py instead of inside strategy/__ init__.py – Bertrand Aug 27 '14 at 18:20
  • 1
    I don't know how you got from my answer that I thought strategy was not defined. On the exact contrary, I said that you are *re*defining strategy within your module. – Daniel Roseman Aug 27 '14 at 18:24
  • Hmm, I am sure I did not redefine strategy and the same code with the same module works fine from a python shell. Since rewriting the strategy module in a single file strategy.py instead of __ init__.py inside a folder strategy/ works does it not show that Django did not load __ init__.py? The last four lines of the code above are executed by an http request. I suppose it is the reason why Django does not load __ init__.py. – Bertrand Aug 27 '14 at 18:41
1

You are importing the function strategy from the module pyalgotrade, which is defined there. The submodule pyalgotrade.strategy is hidden by this function.

Probably you wanted to write:

import pyalgotrade.strategy as strategy

in the first line.

Daniel
  • 42,087
  • 4
  • 55
  • 81
1

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

xj9
  • 3,381
  • 2
  • 24
  • 23
  • Hmm both you and Daniel Roseman were right, I did redefine strategy as a function some time ago elsewere in the file and forgot about it... Thanks for being patient with noobs! – Bertrand Aug 27 '14 at 19:02
0

Try doing this:

from pyalgotrade import strategy
print strategy

and let us know what you get.

Gabriel
  • 492
  • 3
  • 4