Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trade Execution Lag in Backtesting

Hi everyone,

I am new to quantopian and I am just testing some algos in the IDE and I get extremely different results with respect to the ipython notebook.

In particular, I would like to know what is the lag in the execution for an order. To be more specific, if I have a trading signal at time t-zero, such trading signal will be translated in a trading order at t-one or at t-zero? In real trading, if I have a trading signal at 12:00:00 it will be executed a tick later, so some milliseconds later than 12. In the quantopian IDE for the back testing is it the same? or it will be executed at 12:00:01 price? in this case the difference will be remarkable. A minute is a huge amount of time.

Thanks

4 responses

Trade execution in the IDE backtest environment is at the minute resolution. The logic only has visibility to data which happened at or before the end of the previous minute. Any orders placed in a given minute are executed after the current minute but before the next minute.

So, an example at time 9:31 ...

9:31:00  
algorithm runs and has visibility to all data (ie prices and volumes) BEFORE time 9:31:00  
algorithm places an order  
order status is 'unfilled' through entire minute

9:32:00 BEFORE any algorithm logic is run  
backtest engine looks at any unfilled orders (eg orders placed in the previous minute)  
backtest engine determines qty and price to fill any orders based upon the slippage model set in the algorithm  
backtest engine updates all portfolio and account data based upon qty and prices above

9:32:00  
algorithm logic sees order as 'filled'

The default slippage model uses the last close price as the fill price (ie the 9:31:59 close price above). However a custom slippage model can be substituted which could use other prices. Note however, that the Q database only has minute pricing. It's not possible to, say, get the price at 9:31:30.

Attached is a backtest showing this behavior. Look at the logs to see when an order is placed and when it is filled.

Dan, Thank you very much for your response. So I can conclude that I do have a 1 minute lag in trading execution in the back testing. Will it be the same in paper trading?

Yes, paper trading uses the same logic and data as the backtest engine.

Live trading is a tiny bit different. It's my understanding that orders are sent to the broker in 'real time' as the algorithm executes (ie doesn't hold the order to the end of the minute). However, there's no guarantee when within a minute your algorithm actually executes (could be 9:31:01 or 9:31:38) also, there is no guarantee when the order actually get's 'registered' or accepted by the broker. That depends upon the API calls happening behind the scenes and is asynchronous to the algorithm logic. However, just like in backtesting, the only data available to the algorithm is at or before the close of the previous minute.

This can be quite a big limit for Quantopian and may be a good improvement suggestion for the Quantopian Team itself. Having 60/59 seconds lag when you test an algo on intraday data can have a tremendous impact and totally bias the results making them quite unrealistics at certain frequencies. I hope they will improve this feature because the platform is really good.

Thanks for your feedback