Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ManagedRithmicIBFriendlyExample - close positions and orders

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

    ManagedRithmicIBFriendlyExample - close positions and orders

    Referring to ManagedRithmicIBFriendlyExample.cs example that Ninjatraders provides contained in RithmicIBFriendlyExamples.zip
    or referring to https://ninjatrader.com/support/help...utionupdate.ht m (Rithmic/Interactive Brokers Friendly Approach)

    I modified the ManagedRithmicIBFriendlyExample by adding "RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;"

    I am trying to close all positions and orders without stopping the strategy in case of rejection for "Sell stop or sell stop limit orders can't be placed above the market.".

    What's the best way to perform such action ?

    To the original code I added
    region new added code mycode #endregion new added code

    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
            {
                 #region new added code
    ​
               if (orderState == OrderState.Rejected)
                {
                    if (Position.Quantity == 0) // || sumFilledLong1 != 0
                    {
                        //using zero quantity
                        stopLossLong1 = ExitLongStopMarket(0, true, 0, 0.0, "StopLossLong1", "Long limit entry 1");
                        targetLong1 = ExitLongLimit(0, true, 0, 0.0, "TargetLong1", "Long limit entry 1");
    
                        //cancelling the order
                        CancelOrder(longEntry1);
    
                        //nulling any orders
                        longEntry1 = null;
                        CancelOrder(stopLossLong1);
                        stopLossLong1 = null;
                        CancelOrder(targetLong1);
                        targetLong1 = null;
                    }
    
                    if (Position.Quantity != 0) // || sumFilledLong1 != 0)
                    {
                        ExitLong();
                        longEntry1 = null;
    
                        CancelOrder(longEntry1);
                        longEntry1 = null;
    
                        CancelOrder(stopLossLong1);
                        stopLossLong1 = null;
    
                        CancelOrder(targetLong1);
                        targetLong1 = null;
                    }
                    sumFilledLong1 = 0;
                    LongEntry1Prices.Clear();
                    return;
                }
                 #endregion new added code
    
    
    
                 #region original code
                // Assign Order objects here
                // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
                if (order.Name == "Long limit entry 1")
                    longEntry1 = order;
                else if (order.Name == "StopLossLong1")
                    stopLossLong1 = order;
                else if (order.Name == "TargetLong1")
                    targetLong1 = order;
    
                // Null Entry order if filled or cancelled. We do not use the Order objects after the order is filled, so we can null it here
                if (longEntry1 != null && longEntry1 == order)
                {
                    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                        longEntry1 = null;
                    if (order.OrderState == OrderState.Filled)
                        longEntry1 = null;
                }
    
                // Null Target and Stop orders if filled or cancelled. We do not use the Order objects after the order is filled, so we can null them here
                if ((targetLong1 != null && targetLong1 == order)
                    || (stopLossLong1 != null && stopLossLong1 == order))
                {
                    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                        targetLong1 = stopLossLong1 = null;
                    if (order.OrderState == OrderState.Filled)
                        targetLong1 = stopLossLong1 = null;
    
                 #endregion original code
    
                } //if (orderState == OrderState.Rejected)
            } //OnOrderUpdate
    Is this approach conceptually correct?

    Which of the following methods of cancelling the stop/targets are correct or preferable?

    Are the two instructions below
    stopLossLong1 = null;
    CancelOrder(stopLossLong1);
    equivalent?

    And for curiosity, can one cancel the stop loss order by amending its quantity to zero as below?
    ChangeOrder(stopLossLong1, 0, 0.0, 0.0);


    Thank you
    Gio


    Last edited by giogio1; 11-26-2024, 06:16 AM.

    #2
    Hello Gio,

    You will need to use a valid price for the order.

    For a buy stop order, such as when exiting a short position or entering a long position, the stop price must be above the current ask price.
    For a sell stop order, such as when exiting a long position or entering a short position, the stop price must be below the current bid price.​

    The forum post below provides sample code on ensuring the stop price is a valid price.


    CancelOrder() is the proper method for cancelling an order. The order must be assigned to a variable from OnOrderUpdate() only, and must be in an Accepted or Working state to be cancelled.

    ChangeOrder() does not cancel an order. Attempting to set the price to 0 may result in an order error from the brokerage.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Perfect. Thank You !!
      Gio

      Comment


        #4
        Hello Chelsea,
        I've been implementing everything per your suggestion and things seem to work properly. I use RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects . I can close the positions and cancel the orders correctly.

        Unfortunately even though I am giving a margin of 3 ticks below the bid sometimes I still get the error "Sell stop or sell stop limit orders can't be placed above the market.".
        I am stopping all new trades after rejection.

        I tried to handle the error in all possible ways I could think off. It doesn't matter what I do, the strategy terminates. I read around that it should be possible to avoid termination but nothing works. I am printing the variables behavior at every step, I am debugging with Visual Studio and everything is in order.

        I tried everything, from performing no actions after the rejection, to closing all positions, to cancel orders or modifying the orders away from the bid. I tried them all in all combinations. I've done probably one hundred experiments. Nothing works.
        Is it theoretically possible or I am trying to do something that NinjaTrader impedes in the most absolute way that I am not aware of?


        I also tried to override the CloseStrategy() as indicated in the documentation " Overriding the Default CloseStrategy logic". Why?

        Is there a solution to this ?

        I hope to be able to have a direction to work on over this long weekend.
        Thank you in advance.
        Gio
        Last edited by giogio1; 11-27-2024, 05:37 AM.

        Comment


          #5
          Hello Gio,

          A fast moving market could move 3 ticks very rapidly. Maybe 5 ticks would have less rejections?

          In OnOrderUpdate() I recommend you print the GetCurrentAsk() and GetCurrentBid() and print the order.ToString().
          When the order is rejected is the order price on the right side of the ask or bid?
          If not, how far did the price move?

          Please save the output from the output window (right-click Save as) and attach this to your next post.


          Regarding the script disabling, are you certain the order is being rejected on submission, or is the order change being rejected with an unable to change order?

          Did you assign RealtimeErrorHandling from State.Configure (and not State.DataLoaded)?
          If you print RealtimeErrorHandling in OnBarUpdate() what is the value that prints?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thank you Chelsea.
            Many are the questions but I prefer to start from your lead.

            It is the original code with some prints of Bid and other state information, and write to file with PrintWrite(), a check to assure the price is 2 ticks below the bid with SafeStopPrice() and a MyCancelOrder() to assure cancellations are for orders in Working or Accepted state.

            RealtimeErrorHandling is set in SetDefaults (Configure.gives the same result) and OrderTracing is active.

            Optionally, the condition of TEST2 activates some attempts for cancellation but a rejection alone will terminate the strategy.

            In order to understand the situation I added the abort condition that can also assure that no operations will follow a rejection (except with TEST2 = true). I provide below the output with both Test2 false and true.

            I placed two ticks distance for testing because three ticks almost never produces a rejection. (My intention is to have a reliable two three ticks and later later also implement an entry stop rahter than limit but I suspect I'll need to implement an equivalent mechanism that will give a market order when a price is reached - I dislike the latency though - because one tick will cause rejection and stop the strategy.).

            For what I see it remains that nothing can stop a strategy from stopping in case of any rejection. I hope to be wrong in my observation.

            if possible, What is "MaxRestarts=4 in 5 minutes" is it a setting I can change? And I see that Overriding the Default CloseStrategy never get executed . Can this be changed?

            Thank you so much for your time in evaluating the situation, it is really appreciated,
            Gio


            This is the code:
            Code:
                    protected override void OnBarUpdate()
                    {
                        if (CurrentBar == 1) Print("RealtimeErrorHandling: " + RealtimeErrorHandling.ToString());
            
                        if (State == State.Historical)
                        {
                            return; // this code without this starts not flat. must understand why
                        }
            
                        if (abort)
                        {
                            return;
                        }
            
                        PrintWrite(CurrentBar + "  Bid: " + GetCurrentBid());
                        TimeOutsResumeTrading(); // This is specific to this Strategy to make it continue when price runs away from EntryPriceLimit
            
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                            if (longEntry1 == null && (BarsSinceExitExecution() > 5 || BarsSinceExitExecution() == -1))
                            {
                                PrintWrite("  Entry  @" + Time[0]);
                                EnterLongLimit(0, true, entryQuantity1, Low[0] - EntryDistance * TickSize, "Long limit entry 1");
                            }
                        }
                    }
            
                    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                    {
                        PrintWrite("  execUpdt1:   ex:" + execution);
                        PrintWrite("  execUpdt2:   q:" + quantity + " mp:" + marketPosition + "  name:" + execution.Name);
                        // Use execution.Name to identify the order, so we are not using execution.Order, which may not be up to date if an ExecutionUpdate is seen before an OrderUpdate in a partial fill
            
                        if (abort) return;
            
                        if (execution.Name == "Long limit entry 1")
                        {
                            // We sum the quantities of each execution making up the entry order
                            sumFilledLong1 += execution.Quantity;
            
                            if (LongEntry1Prices.IsNullOrEmpty())
                                LongEntry1Prices = new List<double>();
            
                            for (int i = 0; i < execution.Quantity; i++)
                                LongEntry1Prices.Add(execution.Price);
            
                            double averageEntryPrice = 0;
                            for (int i = 0; i < LongEntry1Prices.Count; i++)
                                averageEntryPrice += LongEntry1Prices[i];
                            averageEntryPrice /= LongEntry1Prices.Count;
            
                            PrintWrite("  execUpdt3:  q:" + quantity + " mp:" + marketPosition);
            
                            if (stopLossLong1 == null && targetLong1 == null)
                            {
                                // Directly assign order objects from the method's return value. This prevents us from overprotecting the position by making sure our code changes the orders, instead of submitting new orders
                                PrintWrite("  execUpdt4:  EXIT Sl1-TG1   q:" + quantity + " mp:" + marketPosition);
                                if (!abort) stopLossLong1 = ExitLongStopMarket(0, true, sumFilledLong1, safeStopPrice(averageEntryPrice - StopDistance * TickSize), "StopLossLong1", "Long limit entry 1");
                                if (!abort) targetLong1 = ExitLongLimit(0, true, sumFilledLong1, averageEntryPrice + ProfitDistance * TickSize, "TargetLong1", "Long limit entry 1");
                            }
                            else
                            {
                                PrintWrite("  execUpdt5:  CHANGE Sl1-TG1   q:" + quantity + " mp:" + marketPosition);
                                if (!abort) ChangeOrder(stopLossLong1, sumFilledLong1, 0, safeStopPrice(averageEntryPrice - StopDistance * TickSize));
                                if (!abort) ChangeOrder(targetLong1, sumFilledLong1, averageEntryPrice + ProfitDistance * TickSize, 0);
                            }
            
                            // Entry Order filled, and stops and targets submitted. Reset Price list and running Filled quantity
                            if (sumFilledLong1 == entryQuantity1)
                            {
                                PrintWrite("  execUpdt6:  FILLED   q:" + quantity + " mp:" + marketPosition);
            
                                sumFilledLong1 = 0;
                                LongEntry1Prices.Clear();
                            }
                        }
                    }
            
                    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                    {
                        // Note: This approach differs from SampleOnOrderUpdate in that Order objects are nulled in OnOrderUpdate when we see they are filled. This is because we do not need to hold onto the filled orders in OnExecutionUpdate
            
                        if (orderState == OrderState.Rejected) PrintWrite("\n ---- REJECTED ----");
                        PrintWrite("  ordUpdt1: " + order.ToString() + "  oState:" + orderState + "\n");
                        PrintWrite("  ordUpdt2: pq:" + Position.Quantity + "   " + sumFilledLong1 + "   q:" + quantity + "  " + time.ToString() + "  err:" + error + " natErr:" + nativeError);
            
                        // Assign Order objects here
                        // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
                        if (order.Name == "Long limit entry 1")
                            longEntry1 = order;
                        else if (order.Name == "StopLossLong1")
                            stopLossLong1 = order;
                        else if (order.Name == "TargetLong1")
                            targetLong1 = order;
            
                        // Null Entry order if filled or cancelled. We do not use the Order objects after the order is filled, so we can null it here
                        if (longEntry1 != null && longEntry1 == order)
                        {
                            if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                                longEntry1 = null;
                            if (order.OrderState == OrderState.Filled)
                                longEntry1 = null;
                        }
            
                        // Null Target and Stop orders if filled or cancelled. We do not use the Order objects after the order is filled, so we can null them here
                        if ((targetLong1 != null && targetLong1 == order)
                            || (stopLossLong1 != null && stopLossLong1 == order))
                        {
                            if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                                targetLong1 = stopLossLong1 = null;
                            if (order.OrderState == OrderState.Filled)
                                targetLong1 = stopLossLong1 = null;
                        }
                        
            
                        #region additional code to the original
                        if (error != NinjaTrader.Cbi.ErrorCode.NoError || orderState == OrderState.Rejected)
                        {
                            PrintWrite("  ordUpdt3: Order Updt Rejected - State:" + orderState + "\n");
            
                            abort = true;
            ;
                            bool TEST2 = true;
                            if (TEST2)
                            {
                                if (Position.Quantity == 0) // || sumFilledLong1 != 0
                                {
                                    PrintWrite("  ordUpdt4: Positions Flat after Order Rejection - cancelling stop/target - State:" + orderState + "\n"); ;
            
                                    MyCancelOrder(ref longEntry1);
                                    MyCancelOrder(ref stopLossLong1);
                                    MyCancelOrder(ref targetLong1);
                                }
            
                                if (Position.Quantity != 0) // || sumFilledLong1 != 0)
                                {
                                    PrintWrite("  ordUpdt5: Positions Long after Order Rejection - Closing all positions - pq:" + Position.Quantity + "  s:" + sumFilledLong1 + "\n");
            
                                    ExitLong();
            
                                    MyCancelOrder(ref longEntry1); // cancel remaining positions
                                    MyCancelOrder(ref stopLossLong1);
                                    MyCancelOrder(ref targetLong1);
                                }
                            }
            
                            return;
                        }
                        #endregion additional code to the original one.
            
                    } // OnOrderUpdate()
            
                    private double safeStopPrice(double _stopPrice)
                    {
                        int safeMarginTicks = 2;
                        double stop = _stopPrice;
                        bool SAFE = true;
                        if (SAFE)
                        {
                            stop = Math.Min(_stopPrice, GetCurrentBid() - safeMarginTicks * TickSize);
                        }
            
                        PrintWrite(CurrentBar + " ---- Bid: " + GetCurrentBid() + "  Stop: " + stop + " ----\n");
                        return stop;
                    }
            
            
                    public void MyCancelOrder(ref Order _order)
                    {
                        if (_order != null && (_order.OrderState == OrderState.Working || _order.OrderState == OrderState.Accepted))
                        {
                            CancelOrder(_order);
                        }
                    }
            
                    protected void PrintWrite(string mystr)
                    {
                        string mystr2 = CurrentBar + "  mp:" + Position.MarketPosition + "  s:" + sumFilledLong1 + "(pq:" + Position.Quantity + ")  ";
                        Print(mystr2 + mystr);
                        File.AppendAllText(path, mystr2 + mystr + "\n");
                    }
            ​
            This is the result with TEST2=False;
            RealtimeErrorHandling: StopCancelCloseIgnoreRejects
            Enabling NinjaScript strategy 'ManagedRithmicIGioT1/342051826' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions, ignore order rejections 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
            48 mp:Flat s:0(pq:0)
            Start
            49 mp:Flat s:0(pq:0) 49 Bid: 20218.5
            49 mp:Flat s:0(pq:0) Entry @10/6/2024 9:00:00 PM
            10/6/2024 9:00:05 PM Strategy 'ManagedRithmicIGioT1/342051826': Entered internal SubmitOrderManaged() method at 10/6/2024 9:00:05 PM: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=1 LimitPrice=20218.75 StopPrice=0 SignalName='Long limit entry 1' FromEntrySignal=''
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='5c5b985edd21448d9d375e295d12c23b' account='Playback101' name='Long limit entry 1' orderState=Submitted instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3981 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Submitted
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='5c5b985edd21448d9d375e295d12c23b' account='Playback101' name='Long limit entry 1' orderState=Accepted instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3981 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Accepted
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='5c5b985edd21448d9d375e295d12c23b' account='Playback101' name='Long limit entry 1' orderState=Working instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3981 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Working
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='5c5b985edd21448d9d375e295d12c23b' account='Playback101' name='Long limit entry 1' orderState=Filled instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=20218.5 onBehalfOf='' id=3981 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Filled
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Long s:0(pq:1) execUpdt1: ex:execution='4fed448d1dd2415c8dcefdab5bd5e908' instrument='NQ 12-24' account='Playback101' exchange=Default price=20218.5 quantity=1 marketPosition=Long orderId='5c5b985edd21448d9d375e295d12c23b' time='2024-10-06 21:00:05' sod=False statementDate='2024-10-06'
            49 mp:Long s:0(pq:1) execUpdt2: q:1 mp:Long name:Long limit entry 1
            49 mp:Long s:1(pq:1) execUpdt3: q:1 mp:Long
            49 mp:Long s:1(pq:1) execUpdt4: EXIT Sl1-TG1 q:1 mp:Long
            49 mp:Long s:1(pq:1) 49 ---- Bid: 20218.5 Stop: 20218 ----
            10/6/2024 9:00:05 PM Strategy 'ManagedRithmicIGioT1/342051826': Entered internal SubmitOrderManaged() method at 10/6/2024 9:00:05 PM: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=20218.00 SignalName='StopLossLong1' FromEntrySignal='Long limit entry 1'
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='b55509d0c60a45179238046ce68ebded' account='Playback101' name='StopLossLong1' orderState=Submitted instrument='NQ 12-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=20218 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3982 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Submitted
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Long s:1(pq:1)
            ---- REJECTED ----
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='b55509d0c60a45179238046ce68ebded' account='Playback101' name='StopLossLong1' orderState=Rejected instrument='NQ 12-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=20218 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3982 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Rejected
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:OrderRejected natErr:Sell stop or sell stop limit orders can't be placed above the market.
            49 mp:Long s:1(pq:1) ordUpdt3: Order Updt Rejected - State:Rejected
            49 mp:Long s:1(pq:1) execUpdt6: FILLED q:1 mp:Long
            Disabling NinjaScript strategy 'ManagedRithmicIGioT1/342051826'
            This is the result with TEST2=False;
            'barsAgo' needed to be between 0 and 49 but was 0
            Parameter name: barsAgo
            RealtimeErrorHandling: StopCancelCloseIgnoreRejects
            Enabling NinjaScript strategy 'ManagedRithmicIGioT1/342051826' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions, ignore order rejections 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
            48 mp:Flat s:0(pq:0)
            Start
            49 mp:Flat s:0(pq:0) 49 Bid: 20218.5
            49 mp:Flat s:0(pq:0) Entry @10/6/2024 9:00:00 PM
            10/6/2024 9:00:05 PM Strategy 'ManagedRithmicIGioT1/342051826': Entered internal SubmitOrderManaged() method at 10/6/2024 9:00:05 PM: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=1 LimitPrice=20218.75 StopPrice=0 SignalName='Long limit entry 1' FromEntrySignal=''
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='071797537f0541d88885811dc527bd4a' account='Playback101' name='Long limit entry 1' orderState=Submitted instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3984 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Submitted
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='071797537f0541d88885811dc527bd4a' account='Playback101' name='Long limit entry 1' orderState=Accepted instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3984 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Accepted
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='071797537f0541d88885811dc527bd4a' account='Playback101' name='Long limit entry 1' orderState=Working instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3984 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Working
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Flat s:0(pq:0) ordUpdt1: orderId='071797537f0541d88885811dc527bd4a' account='Playback101' name='Long limit entry 1' orderState=Filled instrument='NQ 12-24' orderAction=Buy orderType='Limit' limitPrice=20218.75 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=20218.5 onBehalfOf='' id=3984 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Filled
            49 mp:Flat s:0(pq:0) ordUpdt2: pq:0 0 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Long s:0(pq:1) execUpdt1: ex:execution='6a45f87c5bca45768504826a8bcbb381' instrument='NQ 12-24' account='Playback101' exchange=Default price=20218.5 quantity=1 marketPosition=Long orderId='071797537f0541d88885811dc527bd4a' time='2024-10-06 21:00:05' sod=False statementDate='2024-10-06'
            49 mp:Long s:0(pq:1) execUpdt2: q:1 mp:Long name:Long limit entry 1
            49 mp:Long s:1(pq:1) execUpdt3: q:1 mp:Long
            49 mp:Long s:1(pq:1) execUpdt4: EXIT Sl1-TG1 q:1 mp:Long
            49 mp:Long s:1(pq:1) 49 ---- Bid: 20218.5 Stop: 20218 ----
            10/6/2024 9:00:05 PM Strategy 'ManagedRithmicIGioT1/342051826': Entered internal SubmitOrderManaged() method at 10/6/2024 9:00:05 PM: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=20218.00 SignalName='StopLossLong1' FromEntrySignal='Long limit entry 1'
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='349c575a693f4755bc9d8631cafcd2da' account='Playback101' name='StopLossLong1' orderState=Submitted instrument='NQ 12-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=20218 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3985 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Submitted
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Long s:1(pq:1)
            ---- REJECTED ----
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='349c575a693f4755bc9d8631cafcd2da' account='Playback101' name='StopLossLong1' orderState=Rejected instrument='NQ 12-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=20218 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3985 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Rejected
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:OrderRejected natErr:Sell stop or sell stop limit orders can't be placed above the market.
            49 mp:Long s:1(pq:1) ordUpdt3: Order Updt Rejected - State:Rejected
            49 mp:Long s:1(pq:1) ordUpdt5: Positions Long after Order Rejection - Closing all positions - pq:1 s:1
            10/6/2024 9:00:05 PM Strategy 'ManagedRithmicIGioT1/342051826': Entered internal SubmitOrderManaged() method at 10/6/2024 9:00:05 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='d627cb8d6bf2439dbd76d83af0c08a10' account='Playback101' name='Sell' orderState=Submitted instrument='NQ 12-24' orderAction=Sell orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3986 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Submitted
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='d627cb8d6bf2439dbd76d83af0c08a10' account='Playback101' name='Sell' orderState=Accepted instrument='NQ 12-24' orderAction=Sell orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3986 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Accepted
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='d627cb8d6bf2439dbd76d83af0c08a10' account='Playback101' name='Sell' orderState=Working instrument='NQ 12-24' orderAction=Sell orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=3986 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Working
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Long s:1(pq:1) ordUpdt1: orderId='d627cb8d6bf2439dbd76d83af0c08a10' account='Playback101' name='Sell' orderState=Filled instrument='NQ 12-24' orderAction=Sell orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=20217.75 onBehalfOf='' id=3986 time='2024-10-06 21:00:05' gtd='2099-12-01' statementDate='2024-10-06' oState:Filled
            49 mp:Long s:1(pq:1) ordUpdt2: pq:1 1 q:1 10/6/2024 9:00:05 PM err:NoError natErr:
            49 mp:Flat s:1(pq:0) execUpdt1: ex:execution='2e91d8f8e4b64448b961672d9cdd292b' instrument='NQ 12-24' account='Playback101' exchange=Default price=20217.75 quantity=1 marketPosition=Short orderId='d627cb8d6bf2439dbd76d83af0c08a10' time='2024-10-06 21:00:05' sod=False statementDate='2024-10-06'
            49 mp:Flat s:1(pq:0) execUpdt2: q:1 mp:Short name:Sell
            49 mp:Flat s:1(pq:0) execUpdt6: FILLED q:1 mp:Long
            Disabling NinjaScript strategy 'ManagedRithmicIGioT1/342051826'
            Last edited by giogio1; 11-29-2024, 05:02 AM.

            Comment


              #7
              Hello,

              "For what I see it remains that nothing can stop a strategy from stopping in case of any rejection. I hope to be wrong in my observation."

              Below is a link to a video demonstrating a strategy is not disabled when a rejection occurs on an order submission (not an order change) when using StopCancelCloseIgnoreRejects or IgnoreAllErrors.



              The CloseStrategy() override will only run if the strategy is being disabled. (You would not see further print output past that point).
              If you are using StopCancelCloseIgnoreRejects or IgnoreAllErrors, the strategy will not be disabled and the CloseStrategy override will not be run when there is a rejection.
              You can call CloseStratey() from OnOrderUpdate() when an order is rejected if you want the strategy to be disabled when using these RealtimeErrorHandling options.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thank You Chelsea! It was necessary your video for me to resolve it. It got me in the right direction and found out that I had left an AddOns that was flattening everything and close strategies.
                I am sorry for the time it took. I spent days trying to figure it out. Thank you again.

                Do you think you can answer me on two things that are leaving me curious?
                What is "MaxRestarts=4 in 5 minutes" printout that I see at the beginning of the tracing printout? Can it be altered?
                And why the Overriding body of the Default CloseStrategy" (as indicated in the documentation) never get executed ?
                Best
                Gio

                Comment


                  #9
                  Hello Gio,

                  "What is "MaxRestarts=4 in 5 minutes" printout that I see at the beginning of the tracing printout? Can it be altered?"

                  This would be the maximum number of times a strategy will be restarted after a connection loss.

                  Below is a link to the help guide.



                  "And why the Overriding body of the Default CloseStrategy" (as indicated in the documentation) never get executed ?"

                  Possibly the strategy is not being disabled.
                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Yesterday, 05:17 AM
                  0 responses
                  62 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  134 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  75 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  45 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  50 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X