Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 = ((
    (
    TickSize) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / TickSize)) ?
    (
    Instrument.MasterInstrument.RoundToTickSize(((GetCurrentBid() - stopLossOrder.StopPrice)) - (TickSize))) :
    (
    TickSize)); 

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

    PHP Code:
    StopLossMove = (((
    Instrument.MasterInstrument.RoundToTickSize(TickSi ze)) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / Instrument.MasterInstrument.RoundToTickSize1TickSize))) ?
    (
    Instrument.MasterInstrument.RoundToTickSize(((GetCurrentBid() - stopLossOrder.StopPrice)) - (Instrument.MasterInstrument.RoundToTickSize(TickSize)))) :
    (
    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:


    Chris L.NinjaTrader Customer Service

    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 = ((Instrument.MasterInstrument.RoundToTickSize(TickSize)) / Instrument.MasterInstrument.RoundToTickSize(1*TickSize));
      Print(
      xB " xB");

      double xC = ((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?
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Oh yes I didn't mention context, here's the action block
          PHP Code:
          {
            
          currentSLPrice stopLossOrder.StopPrice + (StopLossMove);

            for (
          int index 0index quantitySelector.Valueindex++)
            {
              
          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 = ((Instrument.MasterInstrument.RoundToTickSize(TickSize)) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                        Print(
          xB " xB");

                        
          double xC = ((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 = (((TickSize) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / TickSize)) ?
                          (
          Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice)) - (TickSize)) : (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 0index quantitySelector.Valueindex++)
                          {
                            
          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.
            Chris L.NinjaTrader Customer Service

            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 = ((Instrument.MasterInstrument.RoundToTickSize(TickSi ze)) / Instrument.MasterInstrument.RoundToTickSize(1*Tick Size));
                            Print(
              xB " xB");

                            
              double xC = ((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 = (((TickSize) > Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice) / TickSize)) ?
                              (
              Instrument.MasterInstrument.RoundToTickSize((GetCurrentBid() - stopLossOrder.StopPrice)) - (TickSize)) : (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 0index quantitySelector.Valueindex++)
                              {
                                
              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 = ((Instrument.MasterInstrument.RoundToTickSize(TickSize)) /*/ Instrument.MasterInstrument.RoundToTickSize(1*Tick Size)*/);
                            Print(
              xB " xB");

                            
              double xC = ((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 0index quantitySelector.Valueindex++)
                              {
                                
              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 = (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 0index quantitySelector.Valueindex++)
                                {
                                  
                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 0index quantitySelector.Valueindex++)
                                {
                                  
                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 0index quantitySelector.Valueindex++)
                                {
                                  
                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 = (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 0index quantitySelector.Valueindex++)
                                {
                                  
                submitStopLossesOrdersList[index].StopPriceChanged currentSLPrice;
                                }

                                
                myAccount.Change(submitStopLossesOrdersList);
                              }
                            }

                          }, 
                null);
                          
                e.Handled true;

                        
                #endregion 

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by geddyisodin, Today, 05:20 AM
                3 responses
                20 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by lorem, Today, 09:18 AM
                1 response
                4 views
                0 likes
                Last Post lorem
                by lorem
                 
                Started by bmartz, Today, 09:30 AM
                0 responses
                3 views
                0 likes
                Last Post bmartz
                by bmartz
                 
                Started by GussJ, 03-04-2020, 03:11 PM
                14 responses
                3,245 views
                0 likes
                Last Post GussJ
                by GussJ
                 
                Started by ArkansasClint, Today, 09:28 AM
                0 responses
                0 views
                0 likes
                Last Post ArkansasClint  
                Working...
                X