Experts,
I'm a QuantConnect beginner and have a question about when market orders are getting filled. I'm using the OnEndOfDay event to submit my market order so it was my understanding that the order should be submitted and filled the next trading day.
However, what seems to be happening is that the order is submitted the next day but then filled a day later. In the sample below, the order is created on 11/03 and filled on 11/05. 11/04 was not a holiday. I don't understand why the filled order event is not coming on 11/04. Any guidance appreciated. Thanks!
class TestStrategy1(QCAlgorithm):
def Initialize(self):
self.SetBenchmark("SPY")
self.SetStartDate(2022, 11, 1)
self.SetEndDate(2022, 12, 31)
def OnData(self, data: Slice):
# Wait for the algorithm to warm up
if self.IsWarmingUp:
return
# Only trade once a day
if self.previousDate is not None and self.previousDate.date() == self.Time.date():
return
self.previousDate = self.Time
def OnEndOfDay(self) -> None:
# Wait for the algorithm to warm up
if self.IsWarmingUp:
return
# Check entry conditions
self.checkTradeEntry(symbol)
def checkTradeEntry(self, symbol):
# Get prices
closePrice = self.Securities[symbol].Close
openPrice = self.Securities[symbol].Open
lowPrice = self.Securities[symbol].Low
highPrice = self.Securities[symbol].High
position = self.Portfolio[symbol].Quantity
if (condition is True):
quantity = 1
createdMarketOrder(symbol, quantity)
def createMarketOrder(self, symbol, quantity):
ticket = self.MarketOrder(symbol=symbol, quantity=quantity)
self.log(f"Created market order:\n{self.ticketToString(ticket)}")
def OnOrderEvent(self, orderevent):
# Handles the OnOrderEvent for order state changes
if orderevent.Status != OrderStatus.Filled:
return
self.log(f"Filled market order:\n{self.ticketToString(ticket)}")
def log(self, message):
# Internal logging method
self.Debug(f"{self.Time.date()}: {message}")
Output:
2022-11-03: Created market order: Type: Market Order, Price: 214.98, OrderId: 1, Status: Order submitted to the market, Symbol: TSLA, Quantity: -4.0
2022-11-05: Filled market order, ticket Type: Market Order, Price: 214.98, OrderId: 1, Status: Completed, Filled, Symbol: TSLA, Quantity: -4.0
Dutchman71
I may have found the issue. It seems like I was using a deprecated OnEndOfDay(self) method.
Once I used the overloaded method with the symbol as parameter, the orders were getting filled on the correct date.
Dutchman71
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!