Back to Community
confused with order execution time
class PerStockSpreadSlippage(slippage.SlippageModel):

    # We specify the constructor so that we can pass state to this class, but this is optional.  
    def __init__(self, spreads):  
        # Store a dictionary of spreads, keyed by sid.  
        self.spreads = spreads

    def process_order(self, trade_bar, order):  
        spread = self.spreads[order.sid]  
        # in this model, the slippage is going to be half of the spread for the particular stock  
        slip_amount = spread / 2  
        new_price = trade_bar.price + (slip_amount * order.direction)

        log.info('executing order ' + str(trade_bar.sid) + ' stock bar price: ' + str(trade_bar.price) +  
            ' and trade executes at: ' + str(new_price))

        # Create the transaction using the new price we've calculated.  
        return slippage.create_transaction(  
            trade_bar,  
            order,  
            new_price,  
            order.amount  
        )

def initialize(context):  
    spreads = {  
        sid(24): 0.05,  
        sid(3766): 0.08  
    }

    context.position_entered = {  
        sid(24): False,  
        sid(3766): False  
    }

    set_slippage(PerStockSpreadSlippage(spreads))


def handle_data(context, data):

    for stock in data:  
        #log.info(stock)  
        if not context.position_entered[stock]:  
            order(stock, 100)  
            log.info('placing market order for ' + str(stock) + ' at price ' + str(data[stock].price))  
            context.position_entered[stock] = True  

I set the period to 01/04/2014 to 02/14/2014. The algorithm is simple. Buy and hold. And I run it @ daily basis. In the full backtest window, I saw the transaction happened at 01/07/2014. Why there's no trade at 01/06/2014?
And the log information is odd to me.
2014-01-06handle_data:47INFOplacing market order for Security(24 [AAPL]) at price 543.949
2014-01-06handle_data:47INFOplacing market order for Security(3766 [IBM]) at price 186.01
2014-01-07process_order:17INFOexecuting order Security(24 [AAPL]) stock bar price: 540.14 and trade executes at: 540.165
2014-01-07process_order:17INFOexecuting order Security(3766 [IBM]) stock bar price: 189.71 and trade executes at: 189.75

How to explain the huge difference between the price of market order and real execution price? why send an order at 01/06 and execute at 01/07? Do I misunderstand something?
Help Plz

9 responses

Hello Eric,

At a minimum, the order will be filled one minute/day after it is submitted, when backtesting. So, an order submitted during minute 0 won't be filled until minute 1. I am not so familiar with the various new order types, but the basic idea is that you are submitting at the close of the bar, and the fill will be no sooner than at the close of the next bar. So, if you are running in daily mode, submitting on 1/6/2014 with filling on 1/7/2014 makes sense.

In some of the Quantopian docs. and examples, the distinction between submission and fulfillment is kinda blurred.

Grant

Hello Eric,

In your daily algo the orders are submitted on Mon 6th so the backtester uses the close on the next day, Tue 7th, as the execution price. As these are both long trades your slippage model adds 0.025 and 0.04 to the close to give you the execution price. Everything is a expected.

These are the transactions shown in the backtest:

Transactions List 1 - 2 of 2  
DATE                SECURITY     TRANSACTION    # SHARES PRICE $    AMOUNT  
2014-01-07 00:00:00 AAPL         BUY            100      $540.20    $54,019.50  
2014-01-07 00:00:00 IBM          BUY            100      $189.78    $18,978.0  

These prices reflect the default commission of 0.03 per share.

P.

@Peter @Grant, But then the whole backtest does not make sense. WHY SEND A ORDER AND EXECUTE IT AT TOMORROW CLOSE TIME?

The whole logic should be when your algorithm condition is met, send an order and execute it immediately. If u execute it one bar later, It is not CONSISTENT with what is should to be.
As shown in the example, i send an order because I want to trade it now, not wait a whole day for it to execute.

Use minute bar resolution to get intraday orders and fills.

Hello Eric,

The Quantopian backtester has two modes, daily and minutely. Only minutely can be used for their simulated live trading and paper/live (real money) trading with Interactive Brokers. So, in practice, daily is kinda handy for quick de-bugging and conceptual work, but if you want to transition smoothly into actual trading, you'll need to learn how to deal with minute bars. It appears that you are already know some Python programming, so you should have no problem making the transition to minutely data (personally, I no longer bother with daily backtests, unless I am providing assistance).

Note, however, that when trading with paper/real money at Interactive Brokers, my understanding is that orders typically get fulfilled within a few seconds of the order line in the program (another call to handle_data is not required for fulfillment, since the order gets sent immediately).

Hang in there...the learning curve is sorta steep.

Grant

@Grant I understand what u said.First thank you for all your inspiring post. I will read through later. The thing I'm trying to do is test some gap strategy .A lot are based on buy at close , and sell at tomorrow open.(or sell at open, buy back at close) and so on. That's why time POINT for me is very important. I still don't know why quantopian did the executing the next bar after u send the order( which does not make sense to me).Now I figured something out after reading the post https://www.quantopian.com/posts/intraday-strategy-gap-play-please-help
Basically I just check at 3:59 and send an order -->my order will be executed at the last bar, which is what I need.
There's still some problem at market open as the first bar I can execute my order is the second mimute bar.

In conclusion, I thought At least delay a bar to execute a trade for some gap strategy is terrible. Why? Because I only have access to daily data from yahoo finance. So when I did backtest myself, I only consider the open price and close price. (So theoretically i get the right time POINT).

But maybe U are right. The paper money mode may be different. Maybe under the paper money mode the order executes immediately because of real time data?

Hello Grant:
It looks like the quantopian defaults an GTC order at market close time. Do you know is this the case for paper trading at quantopian?

Eric,

According to the help page, Live Trading (with Interactive Brokers) section:

-- All open orders are cancelled at end-of-day.
-- Orders made in the last minute of the trading day will be automatically cancelled.

I don't know if this is the case for the Quantopian paper trading simulator (but presumably it should match the live trading rules).

Quantopian folks, can you clarify?

Grant

That's accurate Grant.

Erik, replied on your other post here: https://www.quantopian.com/posts/at-jessica-stauth-question-about-trade-at-specified-time

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.