Daily-resolution market orders converted to MarketOnClose are rejected by brokerage models without MarketOnClose

Open
#10,042 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp

Research direction

Start with QCAlgorithm.ConvertMarketOrderIfRequired and the brokerage models' CanSubmitOrder supported order types, especially Tradier, Tastytrade, Public, and Webull. Run the supplied daily-resolution SPY reproduction with Tradier and inspect the order type and status. Done means converted orders are accepted and fill at the close when the original market order is supported.

Written by the indexing model from the issue text.

Description

Expected Behavior

In a backtest, a market order placed during market hours on an equity subscribed only at daily resolution is converted to MarketOnClose (or MarketOnOpen within the MOC buffer) by QCAlgorithm.ConvertMarketOrderIfRequired (#9534). The converted order should be accepted by the brokerage model whenever the original market order would have been, and fill at the close.

Actual Behavior

Brokerage models whose supported order types include Market but not MarketOnClose reject the converted order, so every such market order comes back Invalid:

BrokerageModel declared unable to submit order: [1] Warning - Code: NotSupported - The TradierBrokerageModel does not support MarketOnClose order type. Only supports [Limit,Market,StopMarket,StopLimit]

The user called MarketOrder, which the model supports, and the order never fills. Live trading does not convert, so the same algorithm behaves differently live and in backtest.

Models with the same supported-type pattern (from reading their CanSubmitOrder sets): TradierBrokerageModel (reproduced below), TastytradeBrokerageModel, PublicBrokerageModel, WebullBrokerageModel.

Potential Solution

A suggestion; whoever fixes it picks the approach: let a model accept a MarketOnClose/MarketOnOpen order that Lean created from a market order as a backtest execution proxy, or skip the conversion when the brokerage model doesn't support the target type.

Reproducing the Problem
public class DailyMarketOrderMocRepro : QCAlgorithm
{
    private Symbol _spy;
    public override void Initialize()
    {
        SetStartDate(2013, 10, 7);
        SetEndDate(2013, 10, 11);
        SetCash(100000);
        SetBrokerageModel(BrokerageName.TradierBrokerage);
        _spy = AddEquity("SPY", Resolution.Daily).Symbol;
        Schedule.On(DateRules.EveryDay(_spy), TimeRules.AfterMarketOpen(_spy, 30), () =>
        {
            var ticket = MarketOrder(_spy, 1);
            Log($"{Time} type={ticket.OrderType} status={ticket.Status}");
        });
    }
}

Output on master: every day logs type=MarketOnClose status=Invalid with the message above.

System Information

master (06eb5cb31)

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
Dominant language
C#
Stars
22.6k
Forks
5.4k
Avg merge
11h 39m
Merged PRs (30d)
104

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from QuantConnect/Lean

All issues in QuantConnect/Lean

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.