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 do I reset SetTrailStop

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

    How do I reset SetTrailStop

    Hello,

    First off, I love these forums... I'm learning so much, and NT support is truly fantastic! Thank you all for your assistance with my issues as I'm working to figure this out!

    --

    Ok, so the issue I'm having is that SetTrailStop doesn't seem to be resetting.

    Here's what I'm trying to do...
    • I wait for an entry point
    • I SetTrailStop to 99% of entry point, to essentially disable it -- SetTrailStop("Long", CalculationMode.Percent, .99, false);
    • I wait for an Entry point to go Long -- EnterLong(quantity, "Long");
    • When a predefined profit percentage is hit, I SetTrailStop to 1% to capture the majority of profits
    • When the Sell is executed the strategy waits for another entry point
    That's the goal... and I feel like I've tried so many different combinations of how to reset, I'm sure I'm missing the right one!

    What happens is that if I SetTrailStop to 99% before I EnterLong(), I can't seem to reset it to 1%.

    If I don't set it prior to the order, I can SetTrailStop to 1% when a profit target is reached, but then I can't get it back to 99% prior to the next order. It seems like I'm only able to set it once and that's it. This wouldn't be a problem, but I'm trusting that I have good stock picks that will eventually bounce back. So, I want the strategy to reenter when it determines a good entry point, but when it does, it has kept the 1% trail stop (even if I try to reset it).

    I've tried referencing the EntryName for the order on the reset or on the initial setting. I've tried without it. I've tried alternating. I've tried using "Trail Stop" as the help guide mentions... none of it seems to work.

    Here's the code I'm using:

    To Enter a Trade

    Code:
    // Set Trail Stop to 99% (trailStopPercent defaults to 99)
    SetTrailStop("Long", CalculationMode.Percent, trailStopPercent/100.00d, false);
    TimeInForce = TimeInForce.Day; // Must be set for Market Orders                                 
    EnterLong((int)orderQuantity,"Long"); // Buy Quantity Long
    and in a different section

    Code:
    // When a profit target is hit, set Trail Stop to 1% (revisedTrailStop defaults to 1)
    TimeInForce = TimeInForce.Gtc;
    SetTrailStop("Long", CalculationMode.Percent, revisedTrailStop/100d, false);
    Thank you for anything you can do to point me in the right direction.
    -Jeff

    #2
    Hello traderjho,

    Thank you for your post.

    To understand why the script is behaving as it is, such as placing orders or not placing orders 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).

    Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    Below is a link to a forum post that demonstrates using prints to understand behavior and including a link to a video recorded using the Strategy Builder.
    https://ninjatrader.com/support/foru...121#post791121

    Please let me know if I may further assist

    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      I have been using Prints. That's how I know the logic is being executed.

      I also have been using TraceOrders, which doesn't display much even when the logic is executed, which surprised me. I have seen TrailStops get cancelled because of TimeInForce (via the TraceOrders). I've been trying to diagnose this for way too long... lol.

      Ultimately I'd like to see if there are any examples of setting and resetting a Trail Stop in a strategy? I haven't found anything that shows how it's supposed to work. That would really be helpful and would probably highlight what isn't working in my code.

      Or if you have some sample code, I can try and I can post the feedback I get, or it may help me figure out where I've gone wrong.

      Comment


        #4
        Hello traderjho,

        Thank you for that information.

        Please see the link below which contains an example named ProfitChaseStopTrailSetMethodsExample_NT8 which dynamically changes the price of a stop. The stop is reset back to the original distance on lines 94 through 103.

        https://ninjatrader.com/support/foru...269#post802269

        Also, see this forum link for more information: https://ninjatrader.com/support/foru...t-settrailstop

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          So, in the example code... it's programatically emulating a Trail Stop... does that mean a dynamic Trail Stop doesn't work?

          Is there any example code showing similar functionality in a TrailStop?

          I tried using CalculationMode.Price (from the example) on the TrailStop just to see... but that didn't work, that calculationmode isn't an option with TrailStop.

          I left out the entry signal from the trail stops and that didn't work.

          I used prints to ensure the logic is working and it is. If I change from SetTrailStop to ExitLong() it triggers everytime, so the logic is working, it's just not allowing a trail style stop which is the whole point of what I'm trying to do.

          Comment


            #6
            Hello traderjho,

            Thank you for your note.

            Please clarify what is specifically happening with the SetTrailStop() method. Are orders not being changed?

            Something you could do is call SetTrailStop() to specify the target price before calling your entry, such as EnterLong(), within each of your order entry conditions. The code would look something like this:

            Code:
            protected override void OnStateChange()
            {
                ...
                if (State == State.Configure)
                {
                    // Sets a trail stop of 1 percent
                    SetTrailStop("entryOrder", CalculationMode.Percent, 1, false);
                }
            }
            
            protected override void OnBarUpdate()
            {
                // Resets the stop loss to the original value before calling our entry order if current Close is greater than current Open
                if (Close[0] > Open[0])
                {
                    SetTrailStop("entryOrder", CalculationMode.Percent, 1, false);
                    EnterLong("entryOrder");
                }
            }
            Or, you could check when you are in a flat position and call SetTrailStop(). The code would look something like this:

            Code:
            protected override void OnStateChange()
            {
                ...
                if (State == State.Configure)
                {
                    // Sets a trail stop of 1 percent
                    SetTrailStop("entryOrder", CalculationMode.Percent, 1, false);
                }
            }
            
            protected override void OnBarUpdate()
            {
                // Resets the stop loss to the original value when all positions are closed
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    SetTrailStop("entryOrder", CalculationMode.Percent, 1, false);
                }
            }
            Let us know if we may assist further.
            Last edited by NinjaTrader_BrandonH; 02-22-2021, 04:37 PM.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Do you have an example where you change the trail stop? All of the examples do the same thing (12 Ticks).

              That's the issue I started with. It won't change no matter what I do.

              When I set it before the trade, after the trade... whatever the first setting is, it stays at. It won't change.

              Again, I want to only have a TrailStop after a profit percent is hit. If the stock goes up 3%, then I want a 1% trailstop. When the 3% hasn't been hit, I want no trail stop or a 99% trailstop that won't trigger.

              Thank you for your help.

              Comment


                #8
                Hello traderjho,

                Thank you for your note.

                I have edited the examples in my previous post to use percent instead of ticks. The examples demonstrate "resetting" SetTrailStop() to it's original value. If you would like to change the SetTrailStop(), you would call SetTrailStop() after your entry order using different parameters. As stated in the help guide, "You may call this method from within the strategy OnBarUpdate() method should you wish to dynamically change the trail stop price while in an open position."

                SetTrailStop() - https://ninjatrader.com/support/help...ttrailstop.htm

                In your script, you would need to add an entry signal to your entry order and specificaly target the entry with the FromEntrySignal string from SetTrailStop().

                Also, please ensure to test your script thoroughly using the Playback connection.
                Playback Connection - https://ninjatrader.com/support/help...connection.htm

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Mongo, Today, 11:05 AM
                2 responses
                7 views
                0 likes
                Last Post Mongo
                by Mongo
                 
                Started by guillembm, Today, 11:25 AM
                0 responses
                3 views
                0 likes
                Last Post guillembm  
                Started by Tim-c, Today, 10:58 AM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by traderqz, Yesterday, 09:06 AM
                4 responses
                27 views
                0 likes
                Last Post traderqz  
                Started by traderqz, Today, 12:06 AM
                4 responses
                8 views
                0 likes
                Last Post traderqz  
                Working...
                X