This code is just for learning purpose, and I'm wondering why the first order executed on the first day is the sell at market close order not the buy at market open? Any one can advise how to fix it?
And why when placing market on close order 5 or 10 minutes before market close time, it doesn't execute, however as per algorithm reference it says 2 min before market close should be enough?
class DailyBuySellSPY(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 11, 1) # Set Start Date
self.SetEndDate(2021, 11, 30) # Set End Date
self.SetCash(100000) # Set Strategy Cash
Symbol = "SPY"
self.symbol = self.AddEquity(Symbol).Symbol
self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.AfterMarketOpen (self.symbol, -30), self.ExecuteBuy)
self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.BeforeMarketClose (self.symbol, 30), self.ExecuteSell)
def ExecuteBuy (self):
self.MarketOnOpenOrder(self.symbol, 100)
def ExecuteSell (self):
self.MarketOnCloseOrder(self.symbol, -100)
Fred Painchaud
Hi Mostafa,
1- The answer for your first question is in the logs:
SPY: The security does not have an accurate price as it has not yet received a bar of data. Before placing a trade (or using SetHoldings) warm up your algorithm with SetWarmup, or use slice.Contains(symbol) to confirm the Slice object has price before using the data. Data does not necessarily all arrive at the same time so your algorithm should confirm the data is ready before using it. In live trading this can mean you do not have an active subscription to the asset class you're trying to trade. If using custom data make sure you've set the 'Value' property.
I.e., you need to have received some data prior to placing a first order.
2- For your second question, I do see executed sell orders, 30 mins before close. See attached backtest.
Note that on Nov 26th, you do sell at 12h30 since on that day, market closed at 13h00.
Fred
Fred Painchaud
Hi again,
Just noticed that your post said 10 minutes but your code said 30 minutes.
So, the 2 minutes is for most brokers (live trading) to be able to fulfill your order. But on busy assets on busy days, 2 minutes can be a close call.
If you lower the code to 10 minutes, you will get this error in the log:
MarketOnClose orders must be placed within 00:15:30 before market close. Override this TimeSpan buffer by setting Orders.MarketOnCloseOrder.SubmissionTimeBuffer in QCAlgorithm.Initialize().
By default, it is 15m30s. Just so it is more than 15 minutes…
So, 30 minutes was ok, 10, not.
Fred
Mostafa Omran
Thanks Fred, that was very helpful.
Mostafa Omran
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!