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

ATR Trailing Stop Help

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

    ATR Trailing Stop Help

    Hi,

    Please could you help me create an ART trailing stop using the strategy wizard?


    Thanks a lot

    #2
    Hello Tom,

    Hello,

    You would not be able to make this type of dynamic stop loss using the Strategy Wizard.

    You would need to unlock the code and then you can use the custom ATR value with SetStopLoss() which can then dynamically adjust to that price.

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Cal,

      A little with the syntax would be appreciated please. I started with the wizard and I have successfully transitioned to an unlocked algo, but I am still a novice at best and stabbing in the dark most of the time.

      Specifically, what I would like to do is; after the position is opened, set a trailing stop that is equal to the ATR x 2 and to have this update on each bar. Would that be something like:

      SetTrailStop("", CalculationMode.ATR*2, false);
      CalculateOnBarClose = true;
      for a short position and

      SetTrailStop("", CalculationMode.ATR*-2, false);
      CalculateOnBarClose = true;
      for a long position?

      Does all this belong under the "protected override void Initialize()" section or do I need something in the order entry sections as well?

      Thanks in advance!

      Best regards,

      Dolfan

      Comment


        #4
        Hello,
        Can you clarify are you wanting the intial stop to be at the value of the ATR[0] * 2 and then have the strategy trail by one or are you wanting the stop to be always equal to the current value of the ATR[0]?
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          I would want the stop to be trailing therefore updating on each bar. The stop should be equal to the price + ATR*2 depending on whether or not it is long or short. My current syntax is below. Thanks in advance for your assistance.

          Best regards,

          Dolfan

          protected override void Initialize()
          {
          Add(HMA(8));
          Add(SMA(30));
          Add(ATR(14));
          Add(HMA(8));
          Add(SMA(30));
          Add(ATR(14));
          Add(ATR(10));
          SetStopLoss("", CalculationMode.Price, Variable0, false);

          EntriesPerDirection = 5;
          EntryHandling = EntryHandling.AllEntries;

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1
          if ((CrossAbove(HMA(8), EMA(30), 1))
          && ATR(14)[0] > .112);
          {
          EnterLong(1, "");
          Variable0 = CurrentBar - 2* ATR(10)[0];
          }

          // Condition set 2
          if ((CrossBelow(HMA(8), EMA(30), 1))
          && ATR(14)[0] > .112);
          {
          EnterShort(1, "");
          Variable0 = CurrentBar + 2*ATR(10)[0];
          }

          // Condition set 3
          if (Position.MarketPosition == MarketPosition.Long
          && CrossAbove(Close, Position.AvgPrice + 25 * TickSize, 1))
          {
          EnterLong(1, "");
          Variable0 = CurrentBar - 2* ATR(10)[0];
          }

          // Condition set 4
          if (Position.MarketPosition == MarketPosition.Short
          && CrossBelow(Close, Position.AvgPrice - 25 * TickSize, 1))
          {
          EnterShort(1, "");
          Variable0 = CurrentBar + 2*ATR(10)[0];
          }

          Comment


            #6
            Hello,
            If you are wanting to get the price of the current bar you will use Close[0]. CurrentBar will return the number of bars the current bar is. For more information on CurrentBar please see the following link: http://ninjatrader.com/support/helpG...currentbar.htm
            For more information on Close please see the following link: http://ninjatrader.com/support/helpGuides/nt7/close.htm

            If you are wanting to have the StopLoss always adjusting to the value of the Close plus or minus the ATR value you will need to use SetStopLoss in OnBarUpdate() for your logic.
            I have provided an example below that would have the StopLoss updating to the value of Close minus twice the value of the ATR[0].
            Code:
            protected override void OnBarUpdate()
            {
            	if(Historical)
            		return;
            	if(Close[0] > Open[0])
            		EnterLong();
            	if(Position.MarketPosition == MarketPosition.Long)
            		SetStopLoss(CalculationMode.Price, Close[0] - 2 * ATR(10)[0]);
            		
            }
            Cody B.NinjaTrader Customer Service

            Comment


              #7
              Cody,

              The stop loss syntax makes sense, thanks! However, I cannot confirm that all is well because my strategy is only taking long trades. Can you look at what I previously posted and tell me why? It seems to look OK to me. Thanks in advance.

              Best regards,

              Dolfan

              PS, Also worth noting is that because I do not have a profit target and it never enters a short trade, the algo holds the positions until the end of the back test period. Totally unrealistic!



              Even though the trade chart shows short sells, you can see that the account summary shows none.



              And the lost of trades are all LONG

              Last edited by Dolfan; 05-10-2016, 09:37 AM.

              Comment


                #8
                // Condition set 1
                if ((CrossAbove(HMA(8), EMA(30), 1))
                && ATR(14)[0] > .112);
                {
                EnterLong(1, "");
                Variable0 = CurrentBar - 2* ATR(10)[0];
                }

                // Condition set 2
                if ((CrossBelow(HMA(8), EMA(30), 1))
                && ATR(14)[0] > .112);
                {
                EnterShort(1, "");
                Variable0 = CurrentBar + 2*ATR(10)[0];
                }
                In the if condition sets here you have a semi colon after your if statement. What this is doing is it runs the check for the if statement but then you are telling it to do nothing.

                Then you have EnterLong() and EnterShort() that are not part of the actions for the if conditions and as such the strategy is trying to submit and EnterLong() and EnterShort() order on every bar. Due to the Internal order handling rules it is ignoring the EnterShort() orders as the EnterLong() order submits first and methods that generate orders to enter a position will be ignored if he strategy position is flat and an order submitted by an enter method is active and the order is used to open a position in the opposite direction.
                Please see the following link on the Internal Order Handling Rules:http://ninjatrader.com/support/helpG...d_approach.htm
                Cody B.NinjaTrader Customer Service

                Comment


                  #9
                  OK, got it. Abusive use of semi-colons. Now it doesn't make any money.

                  Thanks!

                  Best regards,

                  Dolfan

                  Comment


                    #10
                    I have been playing around for a bit with different settings and I cannot figure how how this is exiting trades. I know what I expected it to do but it doesn't seem to make sense. Can you tell me what you think it is looking for in terms of exits? You will notice that I have put some of my statements into dormant mode with comments slashes. Still. this is my first time trying to use the ATR trailing stop and the numbers don't make sense. The strategy entered a trade in CL today when the ATR was .242 but the stop as calculated was 153 ticks away! Say what? How did it arrive at this stop number? The strategy also entered a trade in GC when the ATR was 2.11 and stop was 49 ticks away. This simply doesn't make any sense to me. How do I get it to print to my Output window and see what it is doing?, or can I even do that?

                    Thanks in advance once again. Here is the syntax.

                    Best regards,

                    Dolfan

                    PS, All other entries using this strategy with other instruments result in an immediate close or failure to open due to stop placement. I feel like if I can solve for CL and GC, I can solve the rest.

                    protected override void Initialize()
                    {
                    Add(HMA(8));
                    Add(EMA(21));
                    Add(ATR(14));
                    Add(ATR(10));
                    // Add(ATRTrailingStop(3, 10));
                    Add(HMA(8));
                    Add(EMA(21));
                    Add(ATR(14));
                    Add(ATR(10));
                    // Add(ATRTrailingStop(3, 10));
                    SetStopLoss("", CalculationMode.Price, Variable0, false);

                    EntriesPerDirection = 5;
                    EntryHandling = EntryHandling.AllEntries;

                    CalculateOnBarClose = true;
                    }

                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
                    // Condition set 1
                    if (CrossAbove(HMA(8), EMA(21), 1)
                    && ATR(14)[0] > 0.2)
                    {
                    EnterLong(1, "");
                    }

                    // Condition set 2
                    //if (Position.MarketPosition == MarketPosition.Long
                    // && CrossAbove(Close, ATRTrailingStop(2, 10), 1))
                    {
                    // ExitLong("", "");
                    }

                    // Condition set 3
                    if (CrossBelow(HMA(8), EMA(21), 1)
                    && ATR(14)[0] > 0.2)
                    {
                    EnterShort(1, "");
                    }

                    // Condition set 4
                    //if (Position.MarketPosition == MarketPosition.Short
                    // && Close[0] > ATRTrailingStop(2, 10)[0])
                    {
                    // ExitShort("", "");
                    }

                    // Condition set 5
                    if (Position.MarketPosition == MarketPosition.Long
                    && CrossAbove(Close, Position.AvgPrice + 25 * TickSize, 1))
                    {
                    EnterLong(2, "");
                    SetStopLoss(CalculationMode.Price, Close[0] - 0.2 *ATR(10)[0]);
                    }

                    // Condition set 6
                    if (Position.MarketPosition == MarketPosition.Short
                    && CrossBelow(Close, Position.AvgPrice - 25 * TickSize, 1))
                    {
                    EnterShort(2, "");
                    SetStopLoss(CalculationMode.Price, Close[0] + 0.2 *ATR(10)[0]);
                    }

                    Comment


                      #11
                      Hello Dolfan,

                      Thanks for your post.

                      When using the SetStopLoss() method you need to keep in mind that it is immediately used as soon as an entry order is made. If you have not preset the stop loss value before the order, the stoploss used will be the very last one that you set which could be incorrect.

                      To send information to the output window you would use the Print(); statement. Please see: http://ninjatrader.com/support/helpG...nt7/?print.htm

                      The output window is displayed by going to Tools>output window.

                      Using the print statements throughout your code will help you see what going on and is a good debug technique to use. Alternative would be to draw objects on the chart to show the position and timing of things.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        OK, so in the code detailed below I have

                        SetStopLoss("", CalculationMode.Price, Variable0, false);

                        but I have no variable specified? I need to look back through other communications to see where I got that. It was set at one time but through all the tweaks I have lost that variable.

                        Also, to clarify about printing, do I need a separate print statement for each action or will one suffice for the entire strategy?

                        My last error message in the Output window reads as thus;

                        **NT** Strategy 'abcTRADER/85a68bed4b50450cb9659d124d5fe913' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.

                        Thanks for your assistance.

                        Best regards,

                        Dolfan

                        PS, What I don't understand is why it does not illustrate this premature stop error in back testing. Can you explain?
                        Last edited by Dolfan; 05-16-2016, 08:56 AM.

                        Comment


                          #13
                          Hello Dolfan,

                          Thanks for your reply.

                          You would use a Print statement at each location in the strategy where you want to know either some action has occurred and/or the value of some variable/condition to facilitate understanding why or why not an order occurred.

                          Here is a link to further debugging info: http://ninjatrader.com/support/forum...ead.php?t=3418
                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            OK Patrick,

                            I have read your reply and the explanation in the forum and neither are specific enough to solve the problem. Where exactly does the "Print" statement go? I have tried putting it both in front of the order command and I have set it alone as below and both return errors when compiling. Can you please be exact when describing where a print statement works? Thanks!

                            Best regards,

                            Dolfan

                            // Condition set 3
                            if (CrossBelow(HMA(8), EMA(21), 1)
                            && ATR(14)[0] > 0.2)
                            {
                            EnterShort(1, "");
                            Variable0 = ATRTrailingStop(3.5, 5)[0];
                            Print; <<<---------RETURNS ERROR when compiling!

                            // Condition set 3
                            if (CrossBelow(HMA(8), EMA(21), 1)
                            && ATR(14)[0] > 0.2)
                            {
                            Print EnterShort(1, ""); <<<<<<<<----------RETURNS ERROR when compiling!
                            Variable0 = ATRTrailingStop(3.5, 5)[0];

                            Comment


                              #15
                              Hello Dolfan,

                              Thanks for your reply.

                              Print statements can contain any mix of text and values. Please see the helpguide link for examples of print statements: http://ninjatrader.com/support/helpG...nt7/?print.htm

                              You would place text and or variables inside of () to send to the output window. Text must be contain within "" Print ("this is a text example "); You can print values directly like Print (ATR(10)[0]); You can combine (as it makes it clearer) Print ("this is the ATR in CS5 after order: "+ATR(10)[0]);

                              Here is a specific examples for your code:

                              // Condition set 3
                              if (CrossBelow(HMA(8), EMA(21), 1)
                              && ATR(14)[0] > 0.2)
                              {
                              EnterShort(1, "");
                              Variable0 = ATRTrailingStop(3.5, 5)[0];
                              Print ("CS3 Variable0 = "+ ATRTrailingStop(3.5, 5)[0]);

                              }
                              Paul H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jclose, Today, 09:37 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,413 views
                              0 likes
                              Last Post Traderontheroad  
                              Started by firefoxforum12, Today, 08:53 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post firefoxforum12  
                              Started by stafe, Today, 08:34 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post stafe
                              by stafe
                               
                              Started by sastrades, 01-31-2024, 10:19 PM
                              11 responses
                              169 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X