QuantConnect / QuantConnect/Lean

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

Open
#8,566 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
C#
Stars
21.7k
Forks
5.3k
Avg merge
2d 22h
Merged PRs (30d)
34

Description

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
  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
  • I have provided detailed steps to reproduce the issue

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the supplied QCAlgorithm and inspect the market_on_open_order and market_on_close_order entry points, focusing on bar timestamps and order status transitions. Done means an unfilled MOO is canceled after the first bar and an unfilled MOC is canceled after the last bar rather than filling later.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
backend, fintech-quant
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.