Lean icon indicating copy to clipboard operation
Lean copied to clipboard

Invalid/Cancel Market-On-Open and Market-On-Close

Open AlexCatarino opened this issue 11 months ago • 0 comments

Expected Behavior

If the MOO doesn't fill with the first bar of day, it should be invalid (IB cancels this order) If the MOC doesn't fill with the last bar of the day, it should be invalid.

Actual Behavior

If the MOO fills with any bar after the opening. If the MOC fills on the next open

Reproding the problem


class LogicalFluorescentPinkFox(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2023, 3, 1)
        self.set_start_date(2023, 3, 5)
        self.set_cash(100000)
        self.asset_symbol = self.add_equity("EEM", Resolution.MINUTE).symbol

        self.schedule.on(self.date_rules.every_day(self.asset_symbol), 
                            self.time_rules.after_market_open(self.asset_symbol, -60), 
                            self._place_market_on_open)
        self.schedule.on(self.date_rules.every_day(self.asset_symbol), 
                            self.time_rules.before_market_close(self.asset_symbol, 60), 
                            self._place_market_on_close
                            )


    def on_data(self, data: Slice):
        pass

    def _place_market_on_open(self):
        self.market_on_open_order(symbol=self.asset_symbol, quantity=-1)

    def _place_market_on_close(self):
        self.market_on_open_order(symbol=self.asset_symbol, quantity=1)

Potential Solution

Check the bar timestamp to set the status to OrderStatus.Canceled.

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues
  • [x] I have provided detailed steps to reproduce the issue

AlexCatarino avatar Jan 30 '25 16:01 AlexCatarino