Hi guys!
I am very new to this community and was testing out my strategies. One of them requires extended hours trading - by looking at the document, I should setup the ticker like this:
self.spy = self.add_equity("SPY", Resolution.DAILY, extended_market_hours=True)
self.default_order_properties.time_in_force = TimeInForce.GOOD_TIL_CANCELED
self.default_order_properties.extended_regular_trading_hours = True
self.spy.set_fill_model(LatestPriceFillModel()) # IS THIS TRUE?As expected, `onData` arrives after market closes during backtesting (actually, exactly at 4 pm). I attempted to submit the order, with 0.5% potential slippage, during the event:
self.LimitOrder("SPY", 1, data["SPY"].close * 1.005)However, it appears that the backtesting system will only attempt to fill my owner only when the next candle arrives - but I want to assume it will get filled after market (which is extremely likely).
I found out that fill is only invoked on the next candle arrival.
I guess one way to fix this would be using consolidator (then candles will arrive even after market) - however, this just adds up complexity - hence my questions:
1. Is there any way to make LEAN just fill with previous candle price during extended hour?
2. Despite the behaviour during backtesting, can I safely assume that the order will get submitted in the extended hour?
Mia Alissi
Here’s a clear breakdown for 'TheQuantGuy':
Daily Resolution + Extended Hours Doesn't Fill Right After Close:
When you backtest using daily resolution and set
extended_market_hours=True, orders are NOT filled immediately after the regular market close (or at any specific intra-day time). This happens even if you use a custom fill model. The reason: in daily resolution, LEAN only processes one data bar per day, delivered after the full session (including extended hours) is over.Orders Only Evaluated on New Daily Bar:
In daily-resolution backtests, order fills are only considered/processed when a new daily bar is received, not at any intra-bar time. Even if extended hours are enabled, that daily bar contains the OHLC for the entire session (regular + extended), and orders are only processed once with the arrival of this bar at the end of the session.
Can't Force Intra-Bar Fills with Custom Fill Model or Flags:
Setting
extended_market_hours=Trueor a fill model likeLatestPriceFillModeldoes NOT allow you to force LEAN to fill orders at custom intra-bar times (like right after 4pm, or during specific after-hours ticks) unless you subscribe to a finer-resolution data feed. Daily bars simply don’t have that granularity – there’s just one bar per day.The Correct Way: Minute/Hourly Data for Extended Session Fills:
To enable order fills at the actual times during extended sessions, set your data resolution to
minuteorhour(andextended_market_hours=True). This way, you’ll get after-hours bars, and your orders can be evaluated much more granularly – so orders submitted after the close can be filled in the next relevant extended-hours bar.Consolidators Can’t Create Nonexistent Data:
Data consolidators only aggregate existing bars; they don’t “invent” new bars for times when no data exists. So, they can’t be used to create pseudo-intra-day fills from daily data.
Live Trading versus Backtest Behavior:
In live trading: If your brokerage and the asset support it, orders can be routed and potentially filled during extended sessions, depending on order type, liquidity, and market conditions. But in backtesting, orders are only evaluated when a new data 'slice' (bar) arrives via your subscription. There are no hidden ticks – just what's in the data feed.
Conclusion – Accurate Simulation Needs Finer Data:
To realistically simulate extended-hours fills in backtesting, subscribe to a finer data resolution (minute or even second). With daily resolution, this lack of intra-session fill timing is a fundamental, known, and unavoidable limitation of the LEAN engine and backtesting in general.
So:
Sources: Key Concepts - QuantConnect.com, Data Preparation - QuantConnect.com, Immediate Model - QuantConnect.com, Future Model - QuantConnect.com, Requesting Data - QuantConnect.com
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
TheQuantGuy
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!