Here's some code to play with. The log output is:
2015-08-21 PRINT 59.299565
End of logs.
Try this:
# if context.build:
# context.build = False
# return
The algo will fail the build, with a confusing build error:
--- Error Execution timeout.
It seems that the build allows for about 20 seconds max. dwell in before_trading_start(), so maybe the Q engineers are trying to build in some margin, since they can't guarantee how fast code will execute at any given time?
My hunch is that before_trading_start() allows 60 seconds of computation time (if you apply the trick to make it through the build), since if I tweak up the dwell time by setting context.iterations = 6*1000000000, I obtain:
TimeoutException: Call to before_trading_start timed out
There was a runtime error on line 18.
I recall asking about before_trading_start() and the allowed dwell, and I think that answer was consistent with my findings today. So, there is nothing gained by using before_trading_start(), unless you need results before 9:31 am when Q trading starts (in this case, as Simon points out, I think you just need to store the trailing window of OHLCV bars in context, although I'd have to test if you can capture the final bar of the prior day).
I'm wondering, though, when before_trading_start() runs in live trading? If it is at 8 am, then there's a lot of wasted time that could be used for computations.
Maybe the Q team can shed some light on their design choices for before_trading_start()?
import time
def initialize(context):
context.stocks = sid(24)
context.iterations = 5*1000000000
context.build = True
def before_trading_start(context):
if context.build:
context.build = False
return
start = time.clock()
for k in xrange(context.iterations):
pass
elapsed = time.clock() - start
print elapsed
def handle_data(context, data):
pass