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

How to set a stop that moves up after hitting a certain unrealized tick gain

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

    How to set a stop that moves up after hitting a certain unrealized tick gain

    Hello how would I set up a stop that moves when lets say the unrealized tick gain on the trade is 12 it moves or places a new stop at breakeven or slightly above, I would have a hard stop setup when the trade is entered. Thanks

    #2
    Hello Prestonf,

    Thanks for your post.

    You can use exit order methods and add custom logic in your strategy to accomplish this goal.

    To get the strategy's UnrealizedPnL value in ticks, you could use the Position.GetUnrealizedProfitLoss() with the PerformanceUnit parameter set to PerformanceUnit.Ticks.

    See this help guide page about the Position.GetUnrealizedProfitLoss() method and sample code: https://ninjatrader.com/support/help...profitloss.htm

    My colleague Chelsea has created educational examples demonstrating breakeven and trailing stop in a NinjaScript strategy here:
    https://ninjatrader.com/support/foru...der#post806596

    You could view the example scripts and click the 'View code' button in the Strategy Builder to see the generated code for accomplishing this.

    Please let us know if we may assist further.​
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Having trouble understanding what is going on with the breakeven one. That would be the closest way to set up what im looking for. What is the trigger state and why are there so many sets? Still learning all of this so this is new to me. Thanks for the help

      Comment


        #4
        Hello Prestonf,

        Set 1 resets the TriggerState to 0 when flat to reset for a new entry.

        Set 2 is the entry conditions. Any conditions for entry would go here, would submit the entry, and would set the TriggerState to 1 to let Set 3 know the entry has been submitted and is waiting to fill.

        Set 3 when the entry is filled and the position has changed, the stop price is set to where the exit stop will be initially submitted, as well as the price where the breakeven will occur. The TriggerState is set to 2 so that we know the stop has initially be set and the trigger for breakeven has been set.

        Set 4 is the breakeven condition. If the TriggerState is 2, the stop price has already been set and the trigger price has been set. Once the market price reaches the trigger, this set will update the stop price to the breakeven price.

        Set 5 submits the stop to the set stop price after the trigger state is 2, meaning the entry has filled.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Perfect that makes sense.

          It looks like it’s for long only at least in the example.

          How would I go about making it work with short trades?

          Comment


            #6
            Hello Prestonf,

            The concepts would stay the same, the StopPrice and TriggerPrice would be different.

            The StopPrice would be n ticks above the Strategy -> Average position price, so the InitialStopDistance would be a positive number instead of a negative number.
            The TriggerPrice would be n ticks below the position average price, so the BreakEvenTrigger would be a negative number instead of a positive number.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks will give it a try

              Comment


                #8
                How would you set it up if the strategy takes longs and shorts?

                Just have more trigger states?

                Comment


                  #9
                  Can you have anything in the stops Stops and Targets?

                  Comment


                    #10
                    Hello Prestonf,

                    Yes, you would need conditions for the long position placing exit stop orders below the market, and separate conditions for the short position placing exit stop orders above the market.

                    SetStopLoss() and SetTrailStop() cannot use dynamic values in the Strategy Builder and should not be used at the same time as exit orders. You would need to choose between exit orders or set methods.

                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      You can still use targets though?

                      Comment


                        #12
                        Strategy is not entering trades at all. I don't see anything that I'm missing from the strategy I did earlier but I'm also not sure what I'm looking for.
                        Attached Files

                        Comment


                          #13
                          Hello Prestonf,

                          Thanks for your notes.

                          As my colleague Chelsea stated, you would need to use Exit methods or Set methods in your script since you cannot use not both. This means that if you choose to use Exit methods, you would need to use Exit methods, such as ExitLongLimit(), to place profit target orders.

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

                          That said, I do not see anything specific standing out in the code you shared that would prevent orders from being submitted.

                          If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

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

                          In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

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

                          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.​
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            I watched Chelsea's video about prints and it was very helpful regarding prints.

                            I'm still having trouble as where to put the prints and what I can actually print now.

                            I can only print the code under else if (State == State.DataLoaded)?

                            It must be something in the Sets but I don't know where to look, it should be making trades.

                            I just copied over the conditions that were working for me before and just added the Sets from Chelsea's breakeven example.

                            Really struggling to figure out what's wrong and how to fix it.​

                            I checked other strategies to see if it was just a setting i had on the chart but they seem to work.

                            Appreciate your help and patience with me thank you.




                            Attached Files

                            Comment


                              #15
                              Hell Prestonf,

                              Thanks for your post.

                              Prints could be added within OnStateChange(), OnBarUpdate(), and other methods to find out how a strategy is behaving and placing orders.

                              If you are not seeing trades appear then it is likely that the conditions to place the order is not becoming true.

                              You may create a new Set and move the new Set between Set 1 and 2. Then, you could add prints to the new Set that prints out Stochastics1.D[0], BarCounter1[0], BarCounterStart, BarCounterStop, BarsSinceExecution, and BarAfterExit.

                              This will print those values to a New > NinjaScript Output window outside of the condition to place the short order so that you may see how those values are evaluating.

                              For example, if you see that Stochastics1.D[0] is not printing a value greater than or equal to 90, the order would not be placed. If BarCounter1[0] is not greater than or equal to BarCounterStart and less than or equal to BarCounterStop, the condition to place the order is not true so the order would not be placed.

                              Please let me know if you need further assistance creating a certain prints to debug your script.
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by geddyisodin, Today, 05:20 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post geddyisodin  
                              Started by JonesJoker, 04-22-2024, 12:23 PM
                              6 responses
                              32 views
                              0 likes
                              Last Post JonesJoker  
                              Started by GussJ, 03-04-2020, 03:11 PM
                              12 responses
                              3,239 views
                              0 likes
                              Last Post Leafcutter  
                              Started by AveryFlynn, Today, 04:57 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post AveryFlynn  
                              Started by RubenCazorla, 08-30-2022, 06:36 AM
                              3 responses
                              79 views
                              0 likes
                              Last Post PaulMohn  
                              Working...
                              X