Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
@Jessica Stauth Question about trade at specified time

https://www.quantopian.com/posts/intraday-strategy-gap-play-please-help

Thx for the codes. Very helpful. Yet I have some problems here.
if i print all the time I get
2014-02-03PRINT2014-02-03 15:59:00-05:00
2014-02-03PRINT2014-02-03 16:00:00-05:00
2014-02-04PRINT2014-02-04 09:31:00-05:00
2014-02-04PRINT2014-02-04 09:32:00-05:00

In the previous post u wrote "Each day at the 'close' (3:59pm) the algo closes out all existing positions."
And the code is

elif exchange_time.hour == 15 and exchange_time.minute == 59:  
    log.info('closing positions EOD')  
    for stock in data:  
        order_target_percent(stock,0)  

Can i say the code actually check the minute bar between(3:58-3:59) and trade at (4:00)?
if exchange_time.hour == 9 and exchange_time.minute == 31: And @ market open it check the minute bar between(9:30-9:31) and trade at(9:32)?

Another problem is i thought the logic above is not symmetric. To say u can trade at market close(4:00), But u can't trade at market open(have to trade @9:32 instead of 9:31). Could u think a way to execute the trade at (9:31)?

Appreciate for any ideas.

1 response

Correction: I've updated the post with an edit to clarify 'end of day' behavior does differ between live trading and backtest. The post is accurate now.

Hi Eric,

The way minute-level simulations (and live trading) works is based on aggregated minute bars of trade prices sourced from Nanex (so in the OHLCV bar, O = open trade for that minute, H = highest traded price for that minute, and so on). Each minute we get a new bar of trade data and the handle_data() method is called.

The other key piece of information is that orders placed in one call to handle_data, say on pricing bar 9:31am, are 'filled' on the next bar, 9.32am.

In simulation this means that we use the price and volume in the 9:32 am bar to simulate the order fill, depending on the slippage model selected the algo is 'at best' filled at the close price for the 9.32am bar. [N.B. this s a design decision in how we have built our backtester, it is a conservative choice with the intent of erring on the side of caution w.r.t. assumed execution speed.)

In live trading of course our simulator is not setting the fill price, we are submitting your order to the broker when it is placed and it is filled (via IB paper trading or in 'real life') as determined by the broker, typically within 1 second, and the broker transmits that information along with the fill price back to us. The number of shares filled and the fill price are available to the algorithm along with the next 1 minute bar of pricing data. And the beat goes on.

So to answer your questions above in order of market open, then market close:

trading at the open: if you are most concerned with the pricing you are getting in simulation then no, there is no way to force your algo get the simulated 9:31am 'first traded' price, you will at earliest get the 9:32am last traded price. If you are most concerned with the actual price you'll get from IB, I can tell you (and you can verify with an IB demo account) that if your algo places a trade in the first minute of trading you'll end up getting a broker fill at something close to the 9:31 am price.

trading at the close: In practice, to trade as close to the close as possible you should place orders at 3:59pm (or 1 minute prior to close). In live trading we cancel any open orders at 4pm/market close each day. N.B. In the backtester, orders placed at 4pm exactly will actually be executed on the next bar, at 9:31am the next morning. This is likely not the intended outcome in some cases and we're looking at the best way to resolve this difference between the backtester and live trading.

One additional note, we have heard loud and clear that for lower frequency strategies it would be really nice to let the algorithm writer more seamlessly execute daily order types (VWAP over the day, MOC, etc) and save the user the code writing (or cutting/pasting) associated with minute granularity when it is not really needed. I think that makes a lot of sense and that its something we'd support eventually, but no ETA on that at this point.

Best regards,
Jess
p.s. you can also @ me via: [email protected], [email protected] or @jstauth on twitter

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.