Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
When do orders placed get "registered"

Hi, was wondering when an order() is called, does the backtest place the trade based on the price of the next set of minute data or the current one? Also, for real-time trading, does anyone have any experience between the magnitude of the time lag between fetching the data and actually filling the trade?

3 responses

Hello SH,

This script illustrates that the order is filled at the closing price of the next trading bar. This assumes that a custom slippage model is not applied (e.g. https://www.quantopian.com/posts/trade-at-the-open-slippage-model). With a custom model, you can shift the fulfillment price to the opening price of the next trading bar, for example.

Regarding real-time trading with Interactive Brokers (IB), I do not have hands-on experience, but Quantopian has reported that order fulfillment is asynchronous, so that as soon as an order is submitted via the algorithm, it will be sent to IB and filled immediately. Thus, it is typically well within a minute. If you are considering algorithms that rely on fussy pricing and timing at the minute or sub-minute level, I would recommend consulting with Quantopian to understand the details of their system.

Grant

def initialize(context):  
    context.spy = sid(8554)  
    set_slippage(slippage.FixedSlippage(spread=0.00))  
    set_commission(commission.PerTrade(cost=0.0))  
    context.cash_prior = context.portfolio.cash  
def handle_data(context, data):  
    # order(context.spy,1)  
    print get_datetime()  
    print context.cash_prior - context.portfolio.cash  
    print data[context.spy].close_price  
    context.cash_prior = context.portfolio.cash  
    order(context.spy, 1)  

Hi SH,

In the backtest, the trade gets placed based on the close price of the previous bar, not the current bar. If it submitted the order based on the current price, this would be look-ahead bias. For example, if you're running a call of handle_data at 3:01:00PM, and have data[stock].close_price. It can't know the close_price until the end of the minute bar (ie 3:01:59). Instead it returns the close price of the previous minute bar at 3:00:59.

Orders are then filled based on the slippage model. By default, you can trade up to 25% of the stock's transacted volume each bar. You can always change this value to better fit your strategy or create your own customized slippage model.

In live trading, handle_data() gets called every minute for which your securities traded. When your order is triggered, it's sent immediately to IB. It can be filled within seconds depending on the order size and stock liquidity. The order status is updated in the next minute, when your live algorithm dashboard is refreshed.

Cheers,
Alisa

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks for the clarification. In my post above, I probably should have said "This script illustrates that the order is filled at the closing price of the current trading bar (the final recorded price in the database at the end of the current simulated minute)." This is consistent with the log output of the algo I posted above. --Grant