Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Intra-day strategy question

I wanted to build minute-based trading strategy, but found out that after evaluation of a condition the trade can happen only at the close of the next bar. I attach simple code below just for illustration. In it, I look at the current minute bar and if it is higher than some threshold, I open a long position RIGHT AT THE CLOSE OF THE CURRENT BAR. But it actually opens it at the close of the next one. Is it possible yet to open as I wanted or at least at the opening price of the next bar or there no such mechanics?

Thank you!

def initialize(context):  
    context.asset = sid(8554)     # spy  
    context.threshold = 0.20  
    context.opened_position = False  
    set_slippage(slippage.FixedSlippage(spread=0.00))  
def before_trading_start(context, data):  
    context.minute = 0  
def handle_data(context, data):  
    if context.minute > 1:  
        price = data.history(context.asset, "price", 2, "1m")  
        if context.opened_position:  
            order_target_percent(context.asset, 0)  
            context.opened_position = False  
            print 'Closing at {}'.format(price[1])  
        if ( (price[1] - price[0]) > context.threshold and not context.opened_position):  
            order_target_percent(context.asset, 1)  
            context.opened_position = True  
            print 'Opening at {}'.format(price[1])  
    context.minute += 1  
2 responses

Yes, you can do what you explained. It's actually the default behavior of the Quantopian backtest engine (ie Zipline). Orders placed in a given bar are filled at the close of that bar**. The bar close price is used by the built-in slippage models to calculate a fill price. One point of confusion however may be what 'current' prices are.

One can never really get prices for the 'current bar' without interjecting lookahead bias. The only data one can know is previous data (ie data from the last bar). One can't even know the open values for the current bar. There is no guarantee when the open order of a bar actually took place (ie the first order for a bar). Consider a bar with an interval [9:32, 9:33) (meaning the half-open interval greater than or equal to time 9:32 and less than time 9:33). The first order during that bar may have executed at 9:32:35 (35 seconds after the beginning of the bar). If one's code ran at the very beginning of the bar then this 'open' order would not have occurred yet. The 'current' prices on Quantopian therefore refer to data which would be known at the current bar which are the previous bars prices and volume.

Let's see how this looks as orders are placed. Consider a simple algo which places an order for a single share of SPY. Then, if a single share is held, then the algo sells that share (back to zero). Repeat buying and selling like this throughout the day. Attached is an algo which does that. Below is the log output.


2011-01-04 08:31 handle_data:20 INFO  
2011-01-04 08:31 handle_data:23 INFO previous bar SPY close price: 127.32  
2011-01-04 08:31 handle_data:29 INFO No SPY positions. Ordering SPY  
2011-01-04 08:32 handle_data:20 INFO  
2011-01-04 08:32 handle_data:23 INFO previous bar SPY close price: 127.23  
2011-01-04 08:32 handle_data:35 INFO bought SPY at 127.23  
2011-01-04 08:32 handle_data:37 INFO selling SPY  
2011-01-04 08:33 handle_data:20 INFO  
2011-01-04 08:33 handle_data:23 INFO previous bar SPY close price: 127.276  
2011-01-04 08:33 handle_data:29 INFO No SPY positions. Ordering SPY  
2011-01-04 08:34 handle_data:20 INFO  
2011-01-04 08:34 handle_data:23 INFO previous bar SPY close price: 127.22  
2011-01-04 08:34 handle_data:35 INFO bought SPY at 127.22  
2011-01-04 08:34 handle_data:37 INFO selling SPY

It begins at the bar labeled "2011-01-04 08:31". This is the interval [8:31, 8:32) (these times are US Central). It orders a share of SPY.
The next bar "8:32" shows the previous bar (ie 8:31) close price of 127.23. It also shows that one share of SPY has filled at 127.23. It places an order to sell the share of SPY.
The next bar "8:33" shows no SPY positions. The previous sell order filled at the end of the previous bar.
The next bar "8:34" shows the previous bar (ie 8:33) close price of 127.22. It also shows that one share of SPY has filled at 127.22.
The next bar "8:35" shows no SPY positions. The sequence repeats...

This demonstrates the ordering and filling sequence. For another more detailed explanation of how the Quantopian backtest engine (ie Zipline) models orders take a look at this post. (https://www.quantopian.com/posts/newb-question-on-how-orders-are-modelled).

** One small detail is that, technically, orders are filled at the very beginning of a bar. However, functionally this is identical to filling at the close of the previous bar.

Hope this helps with your code.

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.

Dan, thank you for your prompt reply. I understand the issue with look-ahead bias. I just needed the code to make quick feasibility assessment and if the strategy makes any sense at all, to look into details and fight biases.
I looked at your code and understand it, thank you. However, after evaluation of conditions based on the closure of the current bar, when I immediately open market position, it does it using the closing price of the next bar, or, as you mentioned, technically - at the open of the second next from currently closed bar. And I need the code to start position at the closure of the current bar (yes, that's look-ahead) or at the opening price right after this one (yes, the price may not necessarily open at the same level as the previous bar closed - I understand that). In reality, when I do manual trading, I am able to evaluate conditions on the last seconds of the minute and open a position at the end of that same minute. Again, I understand the technical problems and all the biases with my code, I just needed it for quick evaluation.