How are stop market orders executed when testing on one hour timeframe ? In my opinion it's supposed to execute very close to the stop price with a random slippage if possible. However, I don't see this happening exactly.
See attached for a simple strategy that updates the stop loss every bar.
I'm using Lean from Visual Studio and using the results json file to plot the orders as well as the stop loss line. See below for three examples. The red arrow is where the order was filled. In the first two examples the order was filled relatively close to the stop loss line, however, the last example seem to get filled on the close of the candle and not at the stop loss line. I cannot share all the orders here as there are too many screenshots and unfortunately online platform cannot plot orders to this detail.
Franky
Ok so I've tracked one of the orders where the filled price is way below the stop price. It seems like everything is working 100%. The stop loss is registered correctly but executed price is way below the actual stop loss.
My only conclusion to this is that there is some kind of slippage algortihm dependent on volume for the candle that is simulating slippage ? Is this correct ?
Louis Szeto
Hi Franky
The stop market orders are executed at the end of the next bar (i.e. end of the current hour) if your stop price is reached in the previous bar. Even though your stop loss condition is met, the backtest cannot trade when there is no trade occurs (haven't received the bar of the current hour).
If your stop market order is liquidating long position, the stop order will execute when the current bar is received if the lowest price of the stock in the previous bar is below your stop price. Similarly, for liquidating short position except in this scenario, the trigger would be the highest price in the previous bar is higher than your stop price. However, in live trading, your stop order would still be placed when the previous bar was received, and that should be filled much quicker.
Since the bar data is only available when the whole bar is completed, this will not be a real-time action. We'd recommend using higher resolution like minute or second to reduce chances of delay price action for accurate backtesting.
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.
Franky
Hi Louis,
Thanks for the reply.
I understand the fact that the order can only be executed when the bar is completed. I'm not too worried about that as I don't execute more than one trade per bar. What I'm trying to understand is the price at which the market order gets filled. It seems like there is some algorithm or something that is factoring in slippage as sometimes the orders get filled at a very different price to what the stop loss was set. Do you have any info on this ? I don't think the order gets filled with the close/open price (which would be very disappointing if that was the case) as most of the times the order fill price is correct according to the stop.
Louis Szeto
Hi Franky,
The filled price of your stop order would be your stop price, given it must be within (bid) high/low of the trade/quote bar (default will be quotebar bid, if quotebar is not available, tradebar will be used), just the time of execution will be displayed as the end of the bar. This is triggered by trade/quote bar's (bid) low is below your stop price. You may use orderEvent.FillPrice in OnOrderEvent method to compare with your variable stop loss. The last trade bar and quote bar can be retrieved by the following code in OnOrderEvent:
We've attached a backtest for your reference.
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.
Franky
Hi Louis,
I'm still having issues where a Stop Market Order “price” and “fill price” are not the same during a backtest some of the times. See below. Any idea how to troubleshoot this ? My only guess is some form of slippage algorithm but my understanding is QuantConnect does not use slippage in backtests.
Louis Szeto
Hi Franky
Would you please tell us which day and time in that result coming from? Since the result of our testing is fine. Maybe you can also check the current bar to see if the high price is 1101, so that 1101 is already the best stop when reaching the stop price.
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.
Franky
Hi Louis,
There are many many examples I can show you. Bars where this happened:
30 April 2018 04:00
5 May 2018 21:00
etc… …
I only observe two scenarios:
- The stop market fills at the given price (which is correct)
- The stop market order fills at the close of the bar (this does not make any sense)
Technically there should be a third one where some slippage model is used to simulate slippage. I don't see that happening.
Louis Szeto
Hi Franky
The default slippage model is no slippage. A custom slippage model could be created to try to model the reality slippages. You may also create a custom fill model to control the filling behavior of the stop orders.
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.
Franky
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!