Hello,
I am having trouble wrapping my head around how to accurately back test with market orders. I understand from the docs that Market Order will get filled immediately. The problem that I am encountering is that this is not accurate representation of how it will occur in a live environment. .
For example, for a basic stop and reverse strategy using a MA crossover with 1 min data resolution consolidated into 15 min bars, if, in the OnDataConsolidatedHandler, a buy signal is generated at the close of 1500(MST) and a market order is placed here, the OnOrderEvent will have a an order fill with data of 1500 with the price of the 1500 close. In the real world, with these times in the futures market, this would not be possible as it would have been filled sometime after 1600 when the market re-opened. If I understand the docs correctly, placing a limit would eliminate this problem, however that is not really the intended functionality as I do want this to be a market order.
It should be noted that I am intending for this to be a standard algorithm not really falling into the Alpha framework model. My question is, what would be the appropriate way to achieve theses results in a back test with Lean/QC? Where manual buy/sell signal generation occurs at the close of a bar, a market order is placed, and the market order should not actually be filled until the beginning of the next bar at that bars opening price at the very least.
Thank you,
Jared Broad
Hi Leland! If the order was submitted at 14.59.99 it would fill with the market close, but if using a consolidator it would likely arrive at 15.00.001 and it would be converted to a market on open order for when the future market next opens. We have partial day market hour support for each of the futures contracts so it should be modeled perfectly.
If you find a case where it's submitting it as a market order when the market is closed; please post the case to reproduce it so we can investigate.
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.
Leland Procell
Jared, Thank you for the quick reply. In my case I am using custom data imported from google drive. The specific example of using the closing bar of the session is not the entire issue I am confused about, that was just the extreme where I could see it potentially skewing the back testing results with inaccurate fills.
To simplify things, without using a consolidator, if there is an OnData method:
private int _barNumber = -1;
public void OnData(MyCustomDataType myCustomData)
{
_barNumber++;
if(_barNumber == 9)
{
MarketOrder(_symbol, 1);
}
}
So that a buy signal is generated on the 10th bar of data, the order would get filled at the 10th bars end time and closing price correct? In a live environment this would not be possible because we would not hit the OnData method, generate this signal, and send the market order until the bar has already closed. In a live environment and in a perfect scenario I would expect this to actually get filled .001 second later at the next bars opening price which is hopefully the same, but for various reasons could not be.
If it is easier I can attach a barebones back test to demonstrate this, but I do not think that it is an issue with the algorithm, I think it is an issue with how I understand QC intends to handle market orders in a back test. I am just wondering what I may be missing to have this behave more closely to what would happen in a live environment.
Thank you for your input
Leland Procell
To further express my question, if we have the code below to buy on the first bar of data we receive:
private int _barNumber = -1; public void OnData(MyCustomDataType myCustomData) { _barNumber++; if(_barNumber == 0) { MarketOrder(_symbol, 1); } }
And we are building data from a file that has standard Time | Open | High | Low | Close | Volume format as follows
34200000 | 2600.00 | 2602.00 | 2559.00 | 2602.00 | 2000
34260000 | 2605.00 | 2608.00 | 2605.00 | 2608.00 | 1500
Our market order would be created on the close of the first bar and we would get filled at 2602.00. Where in reality, since this bar session has already closed, our best case would be to get filled at the next bars opening price (without slippage) at 2605.00.
This jump of 3 points from intraday bar to bar is an extreme example but could happen in illiquid assets or times.
I would assume that there would be something similar to the Market On Open order, but instead of on the exchange open, for back testing purposes it would simulate a market fill at the next bars opening price.
Derek Melchin
Hi Leland,
The opening price of the next bar isn't available until the the next bar is complete (see docs). Thus when backtesting, trades are filled at the close of the current bar. The default fill model assumes immediate fills. It may not be perfect for illiquid assets, but users are free to adjust it to their needs.
See the attached backtest and notebook for reference.
Best,
Derek Melchin
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.
Leland Procell
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!