I'm experimenting with a strategy that uses limit orders to enter / exit positions at the bid and ask, and backtesting against tick data.
I noticed that the limit orders will immediately fill on the next observed tick with either bid or ask at the limit price. Example, Limit Buy to enter at 4350.00 on the S&P500 EMini, where the last Bid is 4350.00 and the last Ask is 4350.25. The order appears to fill at the next tick with Bid ≤ 4350.00, which is not realistic as there is likely additional volume in the queue ahead of my Limit Order, and in reality my order might not have even made it to the exchange by the next tick only a few milliseconds later. Because of this, the backtest exhibits unrealistically fast order placement and fill, thus taking significantly more trades than are realistically possible for my alpha and latency infrastructure, giving a false impression of the strategy's capacity.
Some orders fill as soon as they are submitted rather than queuing / delayingA slippage and fees model won't accurately model what I am looking to show (since I am using limit orders). Rather, I need a way to model latency or queuing, but am unsure how to interface with the Limit Order fill model. Is there any way to:
- Add a delay for limit orders to fill? ie - with tick data occurring many times per second, the limit order should be “active” for > N milliseconds before it can possibly be filled? example, I placed the order on the tick where self.Time = 2021-07-17 12:17:15.014, rather than allowing it to fill on the next timestamp if the price is right, I can force it to wait until, say, 600 ms have elapsed, so it will only fill if the price is right and self.Time ≥ 2021-07-17 12:17:15.614
- Coerce limit orders to fill only if the price goes past the limit order price? ie - if you have a Limit Buy at 4350.00, you will only get filled if the Bid is observed at ≤ 4349.75 rather than ≤ 4350.00
- Utilize any sort of latency model?
- Impart some other form of constraint to limit order queuing / filling behavior?
My thinking is to use a CustomFillModel that overrides the LimitFill method, but because it looks like the FillModel takes the OrderEvent object, that it's simply transforming the fill into a slippage rather than taking in a Slice or Data or Tick in order to determine if the fill (OrderEvent) was allowed, so I'm not sure if that would work.
Louis Szeto
Hi Jackson
You're right about building your own custom filling model, then set it for security objects. Please check this example code to see how does it work. According to your need, you may set only a certain number/proportion if larger than some size of your ordering quantity can be filled at once and some will have to wait till the next timepoint. For example, in the mentioned example, you can set fill.FillQuantity in line 60 as:
This will chop the order to be filled in pieces and attaining time delay as you wish.
Best,
Louis Szeto
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.
Jackson Graves
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!