Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Where to do heavy computations?

Hi,

I want to do some heavy computations that take around 20 minutes a day before trading starts based on past daily prices. Where is the good place to do this? Before trading starts has a time limit of 1 minute only. And pipeline is built for only screening stocks and is not useful for these computations. Any ideas?

I think Q should provide a method for us to do such calculations.

Best regards,
Pravin

2 responses

Hi Pravin,

I agree that there should be a facility to allow for longer calculations as I run into this situation quite a lot (since most of my strategies are of the "brute-force" variety). Although it's a bit tedious, what I do (as a work-around) is look to spread the calculation over many minutes by using calls to handle_data() and storing the intermediate results in context. This works well for a calculation with a large outer loop (i.e. iterating through prior days or many securities/pairs) that can be segmented "cleanly" at around 45 second boundaries (to stay well within the 50 second handle_data() timeout). You can use time.clock() to know when to break during each iteration.

Using this method has a few caveats:

  • May not be possible to trade during the first N minutes of the day until the dependent calculation completes
  • Can not be used if a single (atomic) function call lasts more than 50 seconds (i.e. a solver)
  • Backtesting period will be limited since an overall backtest will usually timeout after around 2 hours (as each day's lengthy calculation needs to be simulated)

Note that the before_trading_start() timeout has been increased to 5 minutes now (as part of the Pipeline API changes) although I think there are still some outstanding issues with using history() and set_universe() here.

Hope this helps!

Rob

Hi Pravin,

The before_trading_start() API has a 5-minute time limit. If you run the attached backtest, you can see for yourself.

Note that code execution time can vary and your algo will crash after 5 minutes in before_trading_start().

One thought is that maybe there is a way to use Cython. Scott mentions it in https://www.quantopian.com/posts/introducing-the-pipeline-api and I see it in zipline on https://github.com/quantopian/zipline/blob/master/zipline/lib/rank.pyx. So, Quantopian has it enabled in some fashion. However, if I try cimport cython in an algo, I get a syntax error.

Is there any way you can do the computation on a rolling basis (along the lines of the moving averages described on https://en.wikipedia.org/wiki/Moving_average)? You'd store the prior day's computation in context and only do a quick update in before_trading_start().