Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Prevent StopOrder Order rejections

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

    Prevent StopOrder Order rejections

    I'm trying to account for potential order rejection when StopLoss orders get moved above entry price.
    I've tested this so far

    PHP Code:
    StopLossMove = ((
    (9 * TickSize) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / TickSize)) ?
    (Instrument.MasterInstrument.RoundToTickSize(((GetCurrentBid() - stopLossOrder.StopPrice)) - (9 * TickSize))) :
    (9 * TickSize)); 
    

    PHP Code:
    StopLossMove = (((
    9 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze)) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / TickSize)) ?
    (Instrument.MasterInstrument.RoundToTickSize(((GetCurrentBid() - stopLossOrder.StopPrice))- (1 * Instrument.MasterInstrument.RoundToTickSize(TickSize)))) :
    (9 * Instrument.MasterInstrument.RoundToTickSize(TickSize))); 
    

    PHP Code:
    StopLossMove = (((
    9 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze)) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / Instrument.MasterInstrument.RoundToTickSize( 1* TickSize))) ?
    (Instrument.MasterInstrument.RoundToTickSize(((GetCurrentBid() - stopLossOrder.StopPrice)) - (1 * Instrument.MasterInstrument.RoundToTickSize(1 * TickSize)))) :
    (9 * Instrument.MasterInstrument.RoundToTickSize(TickSize))); 
    

    It is supposed to set the StopLossMove value to 1 tick less than the gap from Current Bid to StopPrice on Long orders,
    if that gap is less than 9 ticks,
    else use 9 ticks for StopLossMove value.

    The problem is after testing the move still throws the error.

    What would be wrong in this case? Thanks?
    Last edited by PaulMohn; 06-21-2022, 01:38 PM.

    #2
    Hi Paul, thanks for posting. You need to use Print() to print out the values you are using and compare them to the price at the time of order submission. Specifically the values you are using to submit the order:

    Print((Instrument.MasterInstrument.RoundToTickSize (((GetCurrentBid() - stopLossOrder.StopPrice)));
    Print((9 * TickSize)));
    Print((Instrument.MasterInstrument.RoundToTickSize (((GetCurrentBid() - stopLossOrder.StopPrice))- (1 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze)))));
    Print((9 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze))));
    Print((Instrument.MasterInstrument.RoundToTickSize (((GetCurrentBid() - stopLossOrder.StopPrice)) - (1 * Instrument.MasterInstrument.RoundToTickSize(1 * TickSize)))));
    Print((9 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze))));

    //before you submit the order:
    Print(StopLossMove); //assuming this is the price you are using in the stop loss order.
    Print(GetCurrentBid());

    We have a full example on how to set stops and targets in OnOrderUpdate/OnExecutionUpdate here:


    Comment


      #3
      Hello Chris and thanks for the reply and useful directions.

      I've simplified the ternary operator statement by using variables and used prints as

      PHP Code:
      double xA = (Instrument.MasterInstrument.RoundToTickSize(GetCurrentBid() - stopLossOrder.StopPrice) / Instrument.MasterInstrument.RoundToTickSize(1*TickSize));
      Print(xA + " xA");
      
      double xB = ((9 * Instrument.MasterInstrument.RoundToTickSize(TickSize)) / Instrument.MasterInstrument.RoundToTickSize(1*TickSize));
      Print(xB + " xB");
      
      double xC = ((1 * Instrument.MasterInstrument.RoundToTickSize(TickSize)) / Instrument.MasterInstrument.RoundToTickSize(1*TickSize));
      Print(xC + " xC");
      
      Print((xA - xC) + " (xA - xC)");
      Print("StopLossMoveBefore : " + StopLossMove);
      
      
      StopLossMove = ((xB > xA) ? (xA - xC) : xB);
      
      
      Print("StopLossMoveAfter : " + StopLossMove); 
      
      Prints
      34 xA
      9 xB
      1 xC
      33 (xA - xC)
      StopLossMoveBefore : 0
      StopLossMoveAfter : 9
      Now it just throws the pop up error twice. Please see attached photo.

      Why doesn't it prevent the error and just place the order 1 tick below the Bid so no error occurs? Thanks!
      Last edited by PaulMohn; 06-21-2022, 02:47 PM.

      Comment


        #4
        Hi Paul, I do not have enough context here about how you are using StopLossMoveAfter. What is the price or variable that is being used in the actual stop loss order? Are you using an Exit method (ExitLongStopMarket) or using SetStopLoss?

        Comment


          #5
          Oh yes I didn't mention context, here's the action block
          PHP Code:
          {
            currentSLPrice = stopLossOrder.StopPrice + (StopLossMove);
          
            for (int index = 0; index < quantitySelector.Value; index++)
            {
              submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
            }
          
            myAccount.Change(submitStopLossesOrdersList);
          } 
          

          I use it in an indicator, not a strategy.

          The full code
          https://ninjatraderecosystem.com/use...otkeys-script/

          Thanks!


          Here's the current snippet on my test script

          PHP Code:
          #region MOVE UP StopLoss OCO Orders for BuyMarket OR SellMarket Orders
          
                    TriggerCustomEvent(BuyMar****RSellMarket_OrderMove StopLossUpOCOVar =>
                    {
                      double StopLossMove = 0;
          
          
                      if (entryBuyMar****rder != null)
                      {
                        double xA = (Instrument.MasterInstrument.RoundToTickSize(GetCurrentBid() - stopLossOrder.StopPrice) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                        Print(xA + " xA");
          
                        double xB = ((9 * Instrument.MasterInstrument.RoundToTickSize(TickSize)) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                        Print(xB + " xB");
          
                        double xC = ((1 * Instrument.MasterInstrument.RoundToTickSize(TickSize)) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                        Print(xC + " xC");
          
                        Print((xA - xC) + " (xA - xC)");
                        Print("StopLossMoveBefore : " + StopLossMove);
          
                        if (Keyboard.IsKeyDown(Key.NumPad1))
                          StopLossMove = (((1 * TickSize) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / TickSize)) ?
                          (Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice)) - (1 * TickSize)) : (1 * TickSize));
                        else
                        if (Keyboard.IsKeyDown(Key.NumPad9))
                          StopLossMove = ((xB > xA) ? (xA - xC) : xB);
          
          
                        Print("StopLossMoveAfter : " + StopLossMove);
          
                        if ( ( Keyboard.IsKeyDown(Key.NumPad1)
                          || Keyboard.IsKeyDown(Key.NumPad9) )
          
                          && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
                          && !(Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin)))
                        {
                          currentSLPrice = stopLossOrder.StopPrice + (StopLossMove);
          
                          for (int index = 0; index < quantitySelector.Value; index++)
                          {
                            submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
                          }
          
                          myAccount.Change(submitStopLossesOrdersList);
                        }
                      } 
          

          Thanks!
          Last edited by PaulMohn; 06-21-2022, 03:23 PM.

          Comment


            #6
            Hi Paul, the currentSLPrice is above the bid. Print out the final value of currentSLPrice and compare it to the Bid print, you will see the price is above the bid as the order gets submitted.

            Comment


              #7
              Thanks for the direction. I tested as

              PHP Code:
              #region MOVE UP StopLoss OCO Orders for BuyMarket OR SellMarket Orders
              
                        TriggerCustomEvent(BuyMar****RSellMarket_OrderMove StopLossUpOCOVar =>
                        {
                          double StopLossMove = 0;
              
              
                          if (entryBuyMar****rder != null)
                          {
                            double xA = (Instrument.MasterInstrument.RoundToTickSize(GetCurrentBid() - stopLossOrder.StopPrice) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                            Print(xA + " xA");
              
                            double xB = ((9 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze)) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                            Print(xB + " xB");
              
                            double xC = ((1 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze)) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                            Print(xC + " xC");
              
                            Print((xA - xC) + " (xA - xC)");
                            Print("StopLossMoveBefore : " + StopLossMove);
              
                            if (Keyboard.IsKeyDown(Key.NumPad1))
                              StopLossMove = (((1 * TickSize) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / TickSize)) ?
                              (Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice)) - (1 * TickSize)) : (1 * TickSize));
                            else
                            if (Keyboard.IsKeyDown(Key.NumPad9))
                              StopLossMove = ((xB > xA) ? (xA - xC) : xB);
              
              
                            Print("StopLossMoveAfter : " + StopLossMove);
              
                            Print("GetCurrentBid() : " + GetCurrentBid());
              
                            if ( ( Keyboard.IsKeyDown(Key.NumPad1)
                              || Keyboard.IsKeyDown(Key.NumPad9) )
              
                              && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
                              && !(Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin)))
                            {
                              currentSLPrice = stopLossOrder.StopPrice + (StopLossMove);
              
                              Print("currentSLPrice : " + currentSLPrice);
              
                              for (int index = 0; index < quantitySelector.Value; index++)
                              {
                                submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
                              }
              
                              myAccount.Change(submitStopLossesOrdersList);
                            }
                          } 
              
              Prints
              Buy
              -11057 xA
              9 xB
              1 xC
              -11058 (xA - xC)
              StopLossMoveBefore : 0
              StopLossMoveAfter : 0
              GetCurrentBid() : -1
              -11057 xA
              9 xB
              1 xC
              -11058 (xA - xC)
              StopLossMoveBefore : 0
              StopLossMoveAfter : -110.58
              GetCurrentBid() : -1
              currentSLPrice : -1.01000000000001
              I get nonsense negative values. Please see attached photo. How did you get currentSLPrice > GetCurrentBid() ? Thanks!

              Ah wait I think I get it working without nonsense negative values now. I'll post a demo shortly. Thanks!

              Ah, it prints

              13 xA
              9 xB
              1 xC
              12 (xA - xC)
              StopLossMoveBefore : 0
              StopLossMoveAfter : 9
              GetCurrentBid() : 109.69
              stopLossOrder.StopPrice : 109.56
              currentSLPrice : 118.56
              Demo

              So it doesn't take the TickSize (0.01) but rather the point Size (1.00) ? 9 points above instead of 9 ticks in the above prints.

              Ah, got it! Thanks a lot Chris!

              It was due to the variables xA, xB, and xC division by the TickSize that was not necessary.

              Corrected snippet
              PHP Code:
              TriggerCustomEvent(BuyMar****RSellMarket_OrderMove StopLossUpOCOVar =>
                        {
                          double StopLossMove = 0;
              
              
                          if (entryBuyMar****rder != null)
                          {
                            double xA = (Instrument.MasterInstrument.RoundToTickSize(GetCurrentBid() - stopLossOrder.StopPrice) /*/ Instrument.MasterInstrument.RoundToTickSize(1*Tick Size)*/);
                            Print(xA + " xA");
              
                            double xB = ((9 * Instrument.MasterInstrument.RoundToTickSize(TickSize)) /*/ Instrument.MasterInstrument.RoundToTickSize(1*Tick Size)*/);
                            Print(xB + " xB");
              
                            double xC = ((1 * Instrument.MasterInstrument.RoundToTickSize(TickSize)) /*/ Instrument.MasterInstrument.RoundToTickSize(1*Tick Size)*/);
                            Print(xC + " xC");
              
                            Print((xA - xC) + " (xA - xC)");
                            Print("StopLossMoveBefore : " + StopLossMove);
                            if (Keyboard.IsKeyDown(Key.NumPad9))
                              StopLossMove = ((xB > xA) ? (xA - xC) : xB);
              
              
                            Print("StopLossMoveAfter : " + StopLossMove);
              
                            Print("GetCurrentBid() : " + GetCurrentBid());
              
                            if ( ( Keyboard.IsKeyDown(Key.NumPad1)
                              || Keyboard.IsKeyDown(Key.NumPad9) )
              
                              && (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
                              && !(Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin)))
                            {
                              currentSLPrice = stopLossOrder.StopPrice + (StopLossMove);
              
                              Print("stopLossOrder.StopPrice : " + stopLossOrder.StopPrice);
              
                              Print("currentSLPrice : " + currentSLPrice);
              
                              for (int index = 0; index < quantitySelector.Value; index++)
                              {
                                submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
                              }
              
                              myAccount.Change(submitStopLossesOrdersList);
                            }
                          } 
              

              Now it seems to work as intended and the error is prevented. I'll do some more testing and be back asa. Thanks again!
              Last edited by PaulMohn; 06-21-2022, 04:16 PM.

              Comment


                #8
                The Updated Snippets with working debugging prints for later reference

                PHP Code:
                        #region MOVE UP StopLoss OCO Orders for BuyMarket OR SellMarket Orders
                
                          TriggerCustomEvent(BuyMar****RSellMarket_OrderMove StopLossUpOCOVar =>
                          {
                            double stopLossMoveUpRejectionPrevented = 0;
                
                
                            if (entryBuyMar****rder != null)
                            {
                              double rejectionGapSize = (Instrument.MasterInstrument.RoundToTickSize(GetCu rrentBid() - stopLossOrder.StopPrice) );
                              Print(rejectionGapSize + " rejectionGapSize");
                
                              double stopLossMoveSize = (StopLossMove * Instrument.MasterInstrument.RoundToTickSize(TickSi ze));
                              Print(stopLossMoveSize + " StopLossMoveSize");
                
                              double oneTickSize = (1 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze));
                              Print(oneTickSize + " OneTickSize");
                
                              Print((rejectionGapSize - oneTickSize) + " (rejectionGapSize - OneTickSize)");
                              Print("StopLossMoveBefore : " + StopLossMove);
                
                              if (Keyboard.IsKeyDown(Key.NumPad4))
                                stopLossMoveUpRejectionPrevented = ((stopLossMoveSize > rejectionGapSize) ? (rejectionGapSize - oneTickSize) : stopLossMoveSize);
                
                
                              Print("StopLossMoveAfter : " + StopLossMove);
                
                              Print("GetCurrentBid() : " + GetCurrentBid());
                
                
                              if (Keyboard.IsKeyDown(Key.NumPad4) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                              {
                                currentSLPrice = stopLossOrder.StopPrice + (stopLossMoveUpRejectionPrevented);
                
                                Print("stopLossOrder.StopPrice : " + stopLossOrder.StopPrice);
                
                                Print("currentSLPrice : " + currentSLPrice);
                
                                for (int index = 0; index < quantitySelector.Value; index++)
                                {
                                  submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
                                }
                
                                myAccount.Change(submitStopLossesOrdersList);
                              }
                            }
                            else if (entrySellMar****rder != null)
                            {
                              if (Keyboard.IsKeyDown(Key.NumPad4) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                              {
                                currentSLPrice = stopLossOrder.StopPrice + (StopLossMove * TickSize);
                
                                for (int index = 0; index < quantitySelector.Value; index++)
                                {
                                  submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
                                }
                
                                myAccount.Change(submitStopLossesOrdersList);
                              }
                            }
                          }, null);
                          e.Handled = true;
                
                        #endregion
                
                
                        #region MOVE DOWN StopLoss OCO Orders for BuyMarket OR SellMarket Orders
                
                          TriggerCustomEvent(BuyMar****RSellMarket_OrderMove StopLossDownOCOVar =>
                          {
                            double stopLossMoveDownRejectionPrevented = 0;
                
                
                            if (entryBuyMar****rder != null)
                            {
                              if (Keyboard.IsKeyDown(Key.NumPad4) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                              {
                                currentSLPrice = stopLossOrder.StopPrice - (StopLossMove * TickSize);
                
                                for (int index = 0; index < quantitySelector.Value; index++)
                                {
                                  submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
                                }
                
                                myAccount.Change(submitStopLossesOrdersList);
                              }
                            }
                            else if (entrySellMar****rder != null)
                            {
                              Print(" ");
                              double rejectionGapSize = (Instrument.MasterInstrument.RoundToTickSize(stopL ossOrder.StopPrice - GetCurrentAsk()) );
                              Print(rejectionGapSize + " rejectionGapSize");
                
                              double stopLossMoveSize = (StopLossMove * Instrument.MasterInstrument.RoundToTickSize(TickSi ze));
                              Print(stopLossMoveSize + " StopLossMoveSize");
                
                              double oneTickSize = (1 * Instrument.MasterInstrument.RoundToTickSize(TickSi ze));
                              Print(oneTickSize + " OneTickSize");
                
                              if (Keyboard.IsKeyDown(Key.NumPad4))
                                stopLossMoveDownRejectionPrevented = ((stopLossMoveSize > rejectionGapSize) ? (rejectionGapSize - oneTickSize) : stopLossMoveSize);
                
                
                              Print("stopLossMoveDownRejectionPrevented : " + stopLossMoveDownRejectionPrevented);
                
                              Print("GetCurrentAsk() : " + GetCurrentAsk());
                
                              if (Keyboard.IsKeyDown(Key.NumPad4) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                              {
                                currentSLPrice = stopLossOrder.StopPrice - (stopLossMoveDownRejectionPrevented);
                
                                Print("stopLossOrder.StopPrice : " + stopLossOrder.StopPrice);
                
                                Print("currentSLPrice : " + currentSLPrice);
                
                                for (int index = 0; index < quantitySelector.Value; index++)
                                {
                                  submitStopLossesOrdersList[index].StopPriceChanged = currentSLPrice;
                                }
                
                                myAccount.Change(submitStopLossesOrdersList);
                              }
                            }
                
                          }, null);
                          e.Handled = true;
                
                        #endregion 
                

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                571 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                330 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                549 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                549 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X