Hello everyone:
I setup a small project to trade SPY on daily resolution and was confused by the fill price of order. This is code snippets:
// in Initialize:
SetStartDate(2019, 11, 10);
SetEndDate(2020, 11, 10);
SetCash(100000000m);
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
// in OnData:
Setup a StopMarketOrder if we have nothing, and log every OnData event:
TradeBar bar = data[_symbol];
Log($"OnData Event: {this.Time}({this.Time.DayOfWeek}) {bar}");
if (!this.Portfolio[_symbol].Invested && _testOrderTicket == null)
{
decimal stopPrice = bar.Close * 1.02m;
_testOrderTicket = StopMarketOrder(_symbol, 100, stopPrice);
return;
}
and sell it immediately since it's for test only:
if (this.Portfolio[_symbol].Invested && _testOrderTicket != null)
{
MarketOrder(_symbol, -100);
_testOrderTicket = null;
}
and log the order events in OnOrderEvent:
Log($"OnOrderEvent: {orderEvent}");
in log files, I find something confusing:
2019-11-12 00:00:00 OnData Event: 11/12/2019 12:00:00 AM(Tuesday) SPY: O: 299.3374 H: 300.4182 L: 299.1816 C: 300.2332 V: 30463332
2019-11-12 00:00:00 OnOrderEvent: Time: 11/12/2019 05:00:00 OrderID: 1 EventID: 1 Symbol: SPY Status: Submitted Quantity: 100 StopPrice: 306.2379
2019-11-13 00:00:00 OnData Event: 11/13/2019 12:00:00 AM(Wednesday) SPY: O: 300.6227 H: 301.83 L: 300.0385 C: 300.8661 V: 43841640
Notice that in OnData Event we recorded the time: 11/12/2019 12:00:00 AM. However, in the later event OnOrderEvent, the time is 11/12/2019 05:00:00, it's smaller than the time in OnData event?
The order we subimitted on 2019-11-12 get filled on 2019-11-28:
2019-11-28 00:00:00 OnOrderEvent: Time: 11/28/2019 05:00:00 OrderID: 1 EventID: 2 Symbol: SPY Status: Filled Quantity: 100 FillQuantity: 100 FillPrice: 307.1755 USD StopPrice: 306.2379 OrderFee: 1 USD
2019-11-28 00:00:00 OnData Event: 11/28/2019 12:00:00 AM(Thursday) SPY: O: 306.3868 H: 307.1755 L: 306.0947 C: 307.1755 V: 40449133
2019-11-28 00:00:00 OnOrderEvent: Time: 11/28/2019 05:00:00 OrderID: 2 EventID: 1 Symbol: SPY Status: Submitted Quantity: -100
Notice that the Stop Price is 306.2379 and the fill Price is 307.1755, that is the Close price of the day 2019-11-28. However, the Open price(306.3868) has already greater than Stop price, It looks like that the order would be more possible to be filled with that Open price in living trade?
This is another order submit-fill pair in log file:
2019-12-21 00:00:00 OnOrderEvent: Time: 12/21/2019 05:00:00 OrderID: 5 EventID: 1 Symbol: SPY Status: Submitted Quantity: 100 StopPrice: 320.0991
……
2020-01-11 00:00:00 OnOrderEvent: Time: 01/11/2020 05:00:00 OrderID: 5 EventID: 2 Symbol: SPY Status: Filled Quantity: 100 FillQuantity: 100 FillPrice: 320.0991 USD StopPrice: 320.0991 OrderFee: 1 USD
2020-01-11 00:00:00 OnData Event: 1/11/2020 12:00:00 AM(Saturday) SPY: O: 320.3099 H: 320.4078 L: 318.2013 C: 318.6954 V: 46657905
This time, the order with Stop price 320.0991 is filled with requested price(320.0991), which is greater than the Close price 318.6954.
So this is the second question: what's the specification of order fill price rules in daily resolution trading? I can't find the specification in documentations.
I have attached the backtest. Could anyone help me on the above two questions? Thanks!
Code quant
I found another question:
After I test the code in WebIDE, I download LEAN to my local machine, and run the same code again. I notice that the time changed:
I WebIDE, the log is:
2019-11-12 00:00:00 OnData Event: 11/12/2019 12:00:00 AM(Tuesday) SPY: O: 299.3374 H: 300.4182 L: 299.1816 C: 300.2332 V: 30463332
2019-11-12 00:00:00 OnOrderEvent: Time: 11/12/2019 05:00:00 OrderID: 1 EventID: 1 Symbol: SPY Status: Submitted Quantity: 100 StopPrice: 306.2379
2019-11-13 00:00:00 OnData Event: 11/13/2019 12:00:00 AM(Wednesday) SPY: O: 300.6227 H: 301.83 L: 300.0385 C: 300.8661 V: 43841640
On my local machine, the log is (the difference is maked by bold italic text):
2019-11-12 00:00:00 OnData Event: 2019/11/12 0:00:00(Tuesday) SPY: O: 299.3374 H: 300.4182 L: 299.1816 C: 300.2332 V: 30463332
2019-11-12 00:00:00 OnOrderEvent: Time: 11/12/2019 05:00:00 OrderID: 1 EventID: 1 Symbol: SPY Status: Submitted Quantity: 100 StopPrice: 306.2379
2019-11-13 00:00:00 OnData Event: 2019/11/13 0:00:00(Wednesday) SPY: O: 300.6227 H: 301.83 L: 300.0385 C: 300.8661 V: 43841640
Please note that the time (12:00:00) in WebIDE is now 0:00:00 on local machine. Could anyone teach me that why there is such change? Thanks!
Louis Szeto
Hi Code quant
It is 12:00:00 AM, same as 00:00:00, so there is no backflow in time, both locally and in the web IDE.
That is similar concepts to previous question. The 11-28 12AM bar is consolidated for 11-27's trade day.
Since this is daily resolution data, it will be subscribing to trade bar. Stop market buy orders are filled at max(stop price, close price of daily bar). This is for assuming worst-case scenario since you trade at receiving the bar (end of the day), there is no way the algorithm knows whether your stop order is processed in the middle of the day.
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.
Code quant
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!