Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order rejection when exiting position with strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Order rejection when exiting position with strategy

    Hello, I am building a custom multi instrument (MES and CL) strategy with the following rules :

    Code:
    if (Positions[BarsInProgress].MarketPosition == MarketPosition.Flat)
    {
        if (allowedTradingHoursStart <= DateTime.Now.Hour && DateTime.Now.Hour < allowedTradingHoursEnd)
        {
            tpInTicks = (DateTime.Now.Hour < usSessionHour) ? 20 : 30;
          
            if (CheckIfCanEnterLongPosition())
            {
                Print("Entering Short on " + BarsInProgress);
                string signalName = "EnterLong_" + BarsInProgress;
                SetProfitTarget(CalculationMode.Ticks, tpInTicks);
                EnterLong(BarsInProgress, quantities[BarsInProgress], signalName);
            }
            else if (CheckIfCanEnterShortPosition())
            {
                Print("Entering Short on " + BarsInProgress);
                string signalName = "EnterShort_" + BarsInProgress;
                SetProfitTarget(CalculationMode.Ticks, tpInTicks);
                EnterShort(BarsInProgress, quantities[BarsInProgress], signalName);
            }
            LogFunction();
        }
    }
    else if (Positions[BarsInProgress].MarketPosition == MarketPosition.Long)
    {
        Print("We are currently Long on " + BarsInProgress);
        if (CheckIfCanStopLongPosition(StopMethod.Method2) || DateTime.Now.Hour >= 22)
        {
            Print("Exiting Long on "+ BarsInProgress);
            string fromEntrySignalName = "EnterLong_" + BarsInProgress;
            string signalName = "ExitLong_" + BarsInProgress;
            ExitLong(BarsInProgress, Positions[BarsInProgress].Quantity, signalName, fromEntrySignalName);
        }
    }
    else if (Positions[BarsInProgress].MarketPosition == MarketPosition.Short)
    {
        Print("We are currently Short on "+BarsInProgress);
        if (CheckIfCanStopShortPosition(StopMethod.Method2) || DateTime.Now.Hour >= 22)
        {
            Print("Exiting Short on " + BarsInProgress);
            string fromEntrySignalName = "EnterShort_" + BarsInProgress;
            string signalName = "ExitShort_" + BarsInProgress;
            ExitShort(BarsInProgress, Positions[BarsInProgress].Quantity, signalName, fromEntrySignalName);
        }
    }​
    When the strategy tries to exit position i get an error that forces my script to stop. Here are the logs :

    Code:
    27/02/2025 12:34 Default Disabling NinjaScript strategy 'DTPStrategy/351610424'
    27/02/2025 12:34 Order S1Feb2611109553, Rejected at RMS - Total sell quantity of contract would exceed its limit affected Order: Sell 4 Market
    27/02/2025 12:34 Default Strategy 'DTPStrategy/351610424' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
    27/02/2025 12:34 Order Order='277567078/S1Feb2611109553' Name='ExitLong_0' New state='Rejected' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=0 Quantity=4 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Order rejected' Native error='Rejected at RMS - Total sell quantity of contract would exceed its limit'
    27/02/2025 12:34 Order Order='0de1bbd047854c48bd056916af15ddf4/S1Feb2611109553' Name='ExitLong_0' New state='Submitted' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=0 Quantity=4 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:34 NinjaScript NinjaScript strategy 'DTPStrategy/351610424' submitting order
    27/02/2025 12:21 Order Order='277565883/S1Feb2611109553' Name='Profit target' New state='Working' Instrument='MES 03-25' Action='Sell' Limit price=6013 Stop price=0 Quantity=4 Type='Limit' Time in force=GTC Oco='1479836|277565881|277565881' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 Order Order='277565883/S1Feb2611109553' Name='Profit target' New state='Accepted' Instrument='MES 03-25' Action='Sell' Limit price=6013 Stop price=0 Quantity=4 Type='Limit' Time in force=GTC Oco='1479836|277565881|277565881' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 Order Order='277565882/S1Feb2611109553' Name='Stop loss' New state='Accepted' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=5983 Quantity=4 Type='Stop Market' Time in force=GTC Oco='1479836|277565881|277565881' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 Order Order='c0b1ddfa14774b1989225040658d25c7/S1Feb2611109553' Name='Profit target' New state='Submitted' Instrument='MES 03-25' Action='Sell' Limit price=6013 Stop price=0 Quantity=4 Type='Limit' Time in force=GTC Oco='1479836|277565881|277565881' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 Order Order='82bf5cc5918a4c41b6c9a19226a19f97/S1Feb2611109553' Name='Stop loss' New state='Submitted' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=5983 Quantity=4 Type='Stop Market' Time in force=GTC Oco='1479836|277565881|277565881' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 NinjaScript NinjaScript strategy 'DTPStrategy/351610424' submitting order
    27/02/2025 12:21 NinjaScript NinjaScript strategy 'DTPStrategy/351610424' submitting order
    27/02/2025 12:21 Execution Execution='1479836|277565881|277565881' Instrument='MES 03-25' Account='S1Feb2611109553' Exchange=Globex Price=6008 Quantity=4 Market position=Long Operation=Operation_Add Order='277565881' Time='2/27/2025 12:21 '
    27/02/2025 12:21 Order Order='277565881/S1Feb2611109553' Name='EnterLong_0' New state='Filled' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=4 Type='Market' Time in force=GTC Oco='' Filled=4 Fill price=6008 Error='No error' Native error=''
    27/02/2025 12:21 Position Instrument='MES 03-25' Account='S1Feb2611109553' Average price=6008 Quantity=4 Market position=Long Operation=Operation_Add
    27/02/2025 12:21 Order Order='277565881/S1Feb2611109553' Name='EnterLong_0' New state='Working' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=4 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 Order Order='277565881/S1Feb2611109553' Name='EnterLong_0' New state='Accepted' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=4 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 Order Order='cb53fa9d4bf743008a7b4ceb45a5918c/S1Feb2611109553' Name='EnterLong_0' New state='Submitted' Instrument='MES 03-25' Action='Buy' Limit price=0 Stop price=0 Quantity=4 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    27/02/2025 12:21 NinjaScript NinjaScript strategy 'DTPStrategy/351610424' submitting order
    27/02/2025 12:15 Default Enabling NinjaScript strategy 'DTPStrategy/351610424' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
    ​
    My broker allow me to use at most 5 contracts at a time on my account and it seems the strategy exceed the total sell contracts when submiting an exit order but I always use 4 contracts on MES and 1 contract on CL.
    Last edited by Tessan; 02-27-2025, 07:42 AM.

    #2
    Hello Tessan,

    27/02/2025 12:34 Order Order='277567078' Name='ExitLong_0' New state='Rejected' Instrument='MES 03-25' Action='Sell' Limit price=0 Stop price=0 Quantity=4 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Order rejected' Native error='Rejected at RMS - Total sell quantity of contract would exceed its limit'

    RMS stands for Risk Management System.
    • So, these orders were rejected by your Broker or Evaluation Provider's Risk Management System
    • This normally means the user has defined a Risk setting and is being limited by it.
      • From the error, it sounds like you set a Max order quantity.
    You would want to reach out to your broker or evaluation provider and ask them why this message is appearing and what needs to be done to resolve it moving forward.
    • Whatever Broker or Evaluation provider the orders are being sent to, that is who you would want to reach out to for more detailed information.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      According to the logs it seems the script sent 2 buy orders at the same moment. Maybe it's an issue that allow too much position size. How can this be possible knowing I call EnterLong() only 1 time?

      Comment


        #4
        Hello Tessan,

        Not information is provided.

        The order could be manual or from another strategy. The script could be running on each tick and sending multiple orders before the position has changed. There could be code you didn't include..

        To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that triggers the action.
        The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.
        The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

        Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.
        After enabling TraceOrders remove the instance of the strategy from the Configured list in the Strategies window and add a new instance of the strategy from the Available list.
        Last, print the order.ToString() at the top of the OnOrderUpdate() override, as this will show us when orders become working and are being filled, and print the GetCurrentAsk() and GetCurrentBid() in OnOrderUpdate() as well so we can see what the market price is at the time of a fill.

        I am happy to assist you with analyzing the output from the output window.

        Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

        Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


        Let me know the date and time the behavior occurred or when you are expecting the behavior to occur.

        Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.​
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        42 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        64 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X