Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adjusting Traiing Stops and Profit Targets

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

    Adjusting Traiing Stops and Profit Targets

    SetTrailStop("", CalculationMode.Ticks, SETTrailingStopTicks, true);

    SetProfitTarget(CalculationMode.Ticks, SETProfitTargetTicks);

    I am using the 2 above in my Strategy code

    During an Active Trade, IF I move the SetTrailStop to a tighter Stop IT Remains where I move it to and Hold that new stop value and as Price advances above the Trail Stop Ticks of the code it will resume Trailing ( this is good when you fell your trade is at a stop resistance area and you wish to protect profits at that level area)

    My Question is regarding the SetProfitTarget order, During an Active Trade, IF I move the SetProfitTarget to a closer to the entry Price within a few Live Data ticks the order moves back to the original Profit Target.

    Is there a way to keep this from happening?

    SetProfitTarget(CalculationMode.Ticks, SETProfitTargetTicks, true); <<-------- WOULD ADDING true fix that issue


    #2
    Hello DTSSTS,

    Thanks for your post.

    When using Set methods like SetProfitTarget()/SetStopLoss(), orders will move back based on the strategy's Calculate mode. For example, if you use a Calculate mode of OnBarClose, the Set methods would move back to their original position on the close of a bar.

    Instead, you would need to use Exit methods such as ExitLongStopLimit() with the isLiveUntilCanceled property set to true and ensure that your logic is only calling the Exit methods once so that the order does not reset. When the isLiveUntilCanceled property is set to true the order will NOT expire at the end of a bar but instead remain live until the CancelOrder() method is called or its time in force is reached.

    Please refer to the help guide link below regarding Exit methods and the isLiveUntilCanceled property.
    https://ninjatrader.com/support/help...gstoplimit.htm

    Also, please refer to the attached example strategy demonstrating how this could be accomplished.

    Let us know if we may assist further.
    Attached Files
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      thanks I have an additional question regarding the order type i posted

      Trading MNQ 2 CONTRACT

      my TrailStop is 1 order for 2 contracts, BUT why is my Profit order 2 orders for 1 contract each

      Comment


        #4
        Hello DTSSTS,

        Thanks for your note.

        I am not able to determine why that behavior could be occurring based on the details provided.

        Could you please provide more detail about the situation?

        Are you able to reproduce the behavior when testing the strategy using the Playback connection?

        Playback: https://ninjatrader.com/support/help...8/playback.htm

        If so, please send us a reduced example script that demonstrates the behavior and the exact steps and settings used to reproduce the behavior.

        Note that a reduced copy refers to a copy of the script that contains the minimum amount of code needed to reproduce the issue. All other code is commented out or removed.

        To create a copy of your script to modify, open a New > NinjaScript Editor, select your script, right-click in the Editor, select 'Save as', name the script, and click OK.

        Exporting: https://ninjatrader.com/support/help...tAsSourceFiles

        I look forward to further assisting.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          BrandonH I am inplementing the Exit Methods as shown in the Zip file.

          if (Position.MarketPosition == MarketPosition.Long && myBool == true)
          {
          ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - 10*TickSize, "MyStop", "MyEntry");
          ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + 10 * TickSize, "MyTarget", "MyEntry");

          myBool = false;
          }


          There is NO Trailing Stop Sample Method however

          How would that be implemented?

          Also in the Code I do not see "with the isLiveUntilCanceled property set to true "

          What will cancel these

          Comment


            #6
            Hello DTSSTS,

            Thanks for your note.

            The SampleExitMethods script attached in my previous post demonstrates using Exit methods such as ExitLongStopLimit() with the isLiveUntilCanceled property set to true to ensure that logic is only calling the Exit methods once so that the order does not reset. The script does not demonstrate a trailing stop.

            For an example of using Exit methods to create a trailing stop, see the example script linked in this forum thread: https://ninjatrader.com/support/foru...der#post806596

            The isLiveUntilCanceled property is set to true for both of the Exit order methods.

            ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - 10*TickSize, "MyStop", "MyEntry");
            ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + 10 * TickSize, "MyTarget", "MyEntry");


            The syntax for using isLiveUntilCanceled could be found below as well as in the help guide pages linked below.

            ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)
            ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)


            ExitLongLimit: https://ninjatrader.com/support/help...tlonglimit.htm
            ExitLongStopMarket: https://ninjatrader.com/support/help...stopmarket.htm

            Let us know if we may assist further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              To be clear, I DO NOT NEED BOTH OF THESE, CORRECT?

              the isLiveUntilCanceled property is set to true for both of the Exit order methods.

              ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - 10*TickSize, "MyStop", "MyEntry");
              ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + 10 * TickSize, "MyTarget", "MyEntry");


              The syntax for using isLiveUntilCanceled could be found below as well as in the help guide pages linked below.

              ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)
              ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)


              i am currently only using the example form the zip file



              ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - 10*TickSize, "MyStop", "MyEntry");
              ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + 10 * TickSize, "MyTarget", "MyEntry");



              Comment


                #8
                Hello DTSSTS,

                Thanks for your note.

                If you would like both of the Exit methods to be live until canceled then yes, you would need to set the isLiveUntilCanceled property to true for both Exit methods.

                Otherwise, if you do not need both of the Exit methods to be live until canceled then you would only need to set the isLiveUntilCanceld property to true for the Exit method that you want to remain live until canceled.

                Let us know if we may assist further.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  OK I have the 2 orders with the true part and I have 2 others without the true part, So first I need to modify the lower 2 to include the True and also change the Quantity to Position.Quantity
                  not sure what the 0 means the examples in the zip to create Trail stop and Breakeven used the Convert.ToInt32 in the orders where the examples in the zip for Profit Target and Stop DID NOT

                  so examples were not consistent in the way they are presented as a learning tool, I NEED TO MODIFY THE LOWER 2 ORDERS

                  // Stop Order and Profit Target Order
                  ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - SETStopTicks*TickSize, "LStop", "");
                  ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + SETProfitTargetTicks * TickSize, "LPTarget", "");

                  //Trailing Stop Order
                  ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), CurrentStopPrice, @"LTStop", "");

                  //Break Even Order
                  ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), StopPrice, @"LBEExit", @"");

                  MY GUESS AS TO THE NEW FORMAT, not sure if can just remove the Covert.ToInt32 however

                  //Trailing Stop Order
                  ExitLongStopMarket(0, true, Position.Quantity, CurrentStopPrice, @"LTStop", "");

                  //Break Even Order
                  ExitLongStopMarket(0, true, Position.Quantity, StopPrice, @"LBEExit", @"");

                  ARE THESE CHANGES CORRECT?

                  Next to follow your reply I need to ADD the following for each order, these items were not included in the SampeExitMethods zip, Where in the Colde Should these lines be located or do they go directly below the orders as shown above

                  ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)
                  ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)


                  ALSO WHAT DIRECTS THE CODE TO KNOW THERE ARE NOW 3 ExitLongStopMarket orders seems there will need to be 2 more lines of code for those

                  but i do not know how to distinguish them from each other

                  Thanks


                  Comment


                    #10
                    Hello DTSSTS,

                    Thanks for your note.

                    The Strategy Builder automatically adds Convert.ToInt32 and this could be excluded from the script when calling your Entry/Exit methods.

                    If you need to keep an order alive until it is canceled, you would need to use the overload syntax for the Entry/Exit method that allows you to specify the isLiveUntilCanceled property.

                    See the help guide pages for the Entry/Exit methods you are using to see the syntax that allows you to specify the property isLiveUntilCanceled for that Entry/Exit method. All the different Entry/Exit methods could be found in the Managed Approach help guide below.

                    Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

                    ExitLongStopMarket: https://ninjatrader.com/support/help...stopmarket.htm

                    "Should these lines be located or do they go directly below the orders as shown above
                    ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)
                    ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)"


                    These lines would not be used in the script specifically. These lines of code show the Exit method C# syntax that would be used to allow you to specify the isLiveUntilCanceled property. This is also seen in the Entry/Exit method help guide documentation.

                    From the ExitLongStopMarket() help guide: "The following method variation is for experienced programmers who fully understand Advanced Order Handling concepts:

                    ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)"


                    If you are wanting to have 3 separate ExitLongStopMarket orders, you would need to call ExitLongStopMarket 3 times in your code. Once for each stop market order you would like placed. If you are wanting a single ExitLongStopMarket order with a quantity of 3, you would need to specify a quantity of 3 when calling ExitLongStopMarket. Exit order methods could be distinguished by specifying a signalName property when calling your Exit order method.

                    Note that the previously mentioned scripts are examples and not fully working strategies. You would need to code your own custom logic and Entry/Exit order methods based on your use case.

                    Let us know if we may assist further.
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Thanks

                      I assume that I did these correctlywhen I added the 0, true and the Position.Quantity

                      //Trailing Stop Order
                      ExitLongStopMarket(0, true, Position.Quantity, CurrentStopPrice, @"LTStop", "");

                      //Break Even Order
                      ExitLongStopMarket(0, true, Position.Quantity, StopPrice, @"LBEExit", @"");

                      ARE THESE CHANGES CORRECT?

                      So the syntax example is just the break down of what is included below

                      ExitLongStopMarket(0, true, Position.Quantity, CurrentStopPrice, @"LTStop", "");

                      Name
                      0 is int barsinProgressIndex
                      true is bool isLiveUntilCancelled,
                      Position.Quanitity is int quantity
                      StopPrice or othe descritption is double
                      then the 2 strings

                      Since I added these 4 new exits I get an error pop up on occassion, NOT everytime, but I do not have all using full syntax

                      Should I expect that to fix that new issue by adding full syntax to those new ordes currently NOT using true etc

                      ALSO I have other just ExitLong orders that are sent when indicator condition sets are reached

                      ONE I should had the full syntax to those
                      and

                      TWO will that stop some isoloated instances that 2 orders are sent exactly the same time which rarely will create a position in the opposite direction with the entry of the short position being the Long exit order

                      Thanks again

                      Comment


                        #12
                        Hello DTSSTS,

                        Thanks for your note.

                        "I assume that I did these correctly when I added the 0, true and the Position.Quantity"

                        That would be the correct syntax. Passing in 0 for the barsInProgressIndex property means that the order would be submitted to the primary data series. Passing in true for the isLiveUntilCanceled property would mean the order will NOT expire at the end of a bar but instead remain live until the CancelOrder() method is called or its time in force is reached. Position.Quantity would be used for the int quantity property in your Exit method.​​​​​​

                        "So the syntax example is just the break down of what is included"

                        Yes, the C# syntax shows the parameters that would need to be used when calling that specific method. For example, if you wanted to use the isLiveUntilCanceled property, you would have to use the C# syntax that allows that property to be specified.

                        Note that in the support department it is against our policy to provide C# education. If you are new to C#, to get a basic foundation for the concepts and syntax used in NinjaScript I would recommend this section of articles in our help guide first: https://ninjatrader.com/support/help...g_concepts.htm

                        The different syntax that could be used for ExitLongStopMarket could be found below and in the help guide linked in my previous posts. From the help guide:

                        Syntax
                        ExitLongStopMarket(double stopPrice)
                        ExitLongStopMarket(int quantity, double stopPrice)
                        ExitLongStopMarket(double stopPrice, string fromEn trySignal)
                        ExitLongStopMarket(double stopPrice, string signal Name, string fromEntrySignal)
                        ExitLongStopMarket(int quantity, double stopPrice, string signalName, string fromEntrySignal)

                        The following method variation is for experienced programmers who fully understand Advanced Order Handling concepts.
                        ExitLongStopMarket(int barsInProgressIndex, bool i sLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)


                        ExitLongStopMarket: https://ninjatrader.com/support/help...stopmarket.htm

                        "ONE I should had the full syntax to those"

                        This would entirely depend on your use case. You would need to compare the syntax you are using to the syntax mentioned in the help guides to ensure the Exit methods are using the correct parameters when being called.

                        "TWO will that stop some isoloated instances that 2 orders are sent exactly the same time which rarely will create a position in the opposite direction with the entry of the short position being the Long exit order"

                        Debugging steps would need to be taken to determine how your strategy is processing and placing orders.

                        Note from the Managed Approach help guide page: Entry() methods will reverse the position automatically. For example, if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position.

                        Also, note that Entry/Exit order methods should not be called in the same condition. If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. It takes time for an order to be submitted, processed, transmitted, accepted, working, filled, and then for NinjaTrader to process the fill and update the position. This will not happen in the same bar update, it will take time.

                        The result will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

                        Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

                        Ultimately, to understand why the script is behaving as it is, such as placing orders or not placing orders when expected, you would need to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                        https://ninjatrader.com/support/foru...121#post791121

                        Let us know if we may assist further.
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment


                          #13
                          Passing in 0 for the barsInProgressIndex property means that the order would be submitted to the primary data series.

                          I use multipe data series, so the Zero would be come 1 or 2 etc

                          I do NOT use EnterShort unless the the Position Quantity equals 0 as to not reverse

                          ALSO HAVING ISSUES WITH CHANGING MY EXIT ORDERS

                          CURRENTLY USING

                          ExitShort(Convert.ToInt32(Position.Quantity), @"ExitS3", "SPS4");//<<<< THIS IS ALSO WITH CONDITIONS FORM SECONDARY DATA SERIES

                          I CHANGED TO BELOW BUT HAVE COMPILE ERRORS

                          ExitShort(0, true, Position.Quantity, @"ExitS3", "SPS4");

                          HELP GUIDE SHOWS

                          ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)

                          UPDATE if i remove true the order then I do not have compile errors, Since this is Exit Market order, I assume does not need isLiveUntilCancelled

                          ExitShort(0, Position.Quantity, @"ExitS3", "SPS4");

                          but this does NOT address my issue of rare instance of 2 Exit orders being sent and 1 Exiting and the other create a reverse position

                          NOT sure how to fix that, but since I have the entry name on all my exits adding an exit without entry name would at least create an exit path for the position that was created by the exit order

                          ExitShort(Convert.ToInt32(Position.Quantity), @"ExitS3", @"");// should I add this for emergency exit when a position is created by an exit order
                          Last edited by DTSSTS; 02-14-2022, 04:53 PM. Reason: UPDATE

                          Comment


                            #14
                            Hello DTSSTS,

                            Thanks for your note.

                            "I use multipe data series, so the Zero would be come 1 or 2 etc"

                            If you have multiple data series in your script and want to submit an order to one of the secondary series, you could do so by passing in the barsInProgressIndex of that added series when calling your Entry/Exit methods. Or, you could check for the BarsInProgress value of the added series you want to and call your Entry/Exit methods.

                            Please thoroughly review this help guide information and examples about working with multi-timeframe/multi-instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm

                            "I CHANGED TO BELOW BUT HAVE COMPILE ERRORS

                            ExitShort(0, true, Position.Quantity, @"ExitS3", "SPS4");"


                            If you have compile errors occurring, you would need to take debugging steps to determine what exactly in your script is causing the error to be thrown. In this case, ExitShort() does not contain a syntax overload that would allow you to specify an isLiveUntilCanceled property since this order method submits a market order. You would need to use a Stop Limit or Stop Market order if you want to specify isLiveUntilCanceled.

                            This could be seen in the syntax section of the ExitShort() help guide page. See the Managed Approach help guide linked in my previous posts for a list of order methods and their available syntax.

                            As mentioned in my previous posts, if you are calling Entry and Exit methods in the same run of OnBarUpdate, this could cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

                            From my previous post: Note from the Managed Approach help guide page: Entry() methods will reverse the position automatically. For example, if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position.

                            Also, note that Entry/Exit order methods should not be called in the same condition. If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. It takes time for an order to be submitted, processed, transmitted, accepted, working, filled, and then for NinjaTrader to process the fill and update the position. This will not happen in the same bar update, it will take time.

                            The result will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.


                            You would have to take Debugging steps to determine how orders are being placed so that you could modify your strategy accordingly. See the forum thread linked in my previous post demonstrating using prints to determine how a strategy is behaving and placing orders.

                            "ExitShort(Convert.ToInt32(Position.Quantity), @"ExitS3", @"");// should I add this for emergency exit when a position is created by an exit order"

                            This would be entirely dependent on your use case.

                            Let us know if we may assist further.
                            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                            Comment


                              #15
                              Thanks for all the help. I would think all the information you shared here will be of benefit to many other traders going forward.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              46 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              126 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              66 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              42 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