Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stop Indicator

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

    Trailing Stop Indicator

    This trailing stop indicator is based on the Close crossing the trailing stop. I would like to change it to work off of the High/Low. Any assistance, advice or tips would be greatly appreciated. Thanks!

    #region Variables
    private double ticks = 15;

    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.Red, PlotStyle.Line, "TrailingStop"));
    CalculateOnBarClose = true;
    Overlay = true;
    BarsRequired = 1;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;

    // Trailing stop
    double trail;
    double loss = Ticks*TickSize ;

    if (Close[0] >= Value[1] && Close[1] >= Value[1])
    trail = Math.Max(Value[1], Close[0] - loss);

    else if (Close[0] < Value[1] && Close[1] < Value[1])
    trail = Math.Min(Value[1], Close[0] + loss);

    else if (Close[0] >= Value[1])
    {
    trail = Close[0] - loss;
    }

    else
    {
    trail = Close[0] + loss;
    }

    Value.Set(trail);
    }

    #2
    just change Close[x] to High[x] or Low[x]
    as required

    Comment


      #3
      Originally posted by mike82 View Post
      just change Close[x] to High[x] or Low[x]
      as required
      Thanks for the reply, but what I am trying to do is utilize High if it crosses above and utilize Low if it crosses below the trail, in one indicator, reasonably like the trailing stop in ATM actually works.

      Getting it to work with both seems to be tricky.

      Thanks again for your input.

      Comment


        #4
        Originally posted by ThatManFromTexas View Post
        .....what I am trying to do is utilize High if it crosses above and utilize Low if it crosses below the trail, in one indicator...
        by it you mean the high or low respectively?

        simply use if(High[0] >= Value[1]....if(Low[0] < Value[1] ...etc.

        Comment


          #5
          Originally posted by mike82 View Post
          by it you mean the high or low respectively?

          simply use if(High[0] >= Value[1]....if(Low[0] < Value[1] ...etc.
          Actually the solution is a lot more complicated than your suggestion. By utilizing High/Low you have two entities to compensate for instead of one like with Close.

          Thank you for your efforts.

          Comment


            #6
            Ok, I got the indicator working (code below). Now I am trying to write an indicator bar that would plot a green dot if the price is above the trail line , otherwise plot a red dot. I didn't have any trouble doing that with the original code that was based on the close as opposed to the high and low, but can not get it to work with this code. Any suggestions would be appreciated.



            Trailing Stop Indicator:

            protected override void OnBarUpdate()
            {
            if (CurrentBar < 1)
            return;

            // Trailing stop
            double trail;
            double loss = Ticks*TickSize ;

            if (Low[0] > Value[1] && Low[1] > Value[1])
            {
            trail = Math.Max(Value[1], High[0] - loss);

            }
            else if (High[0] < Value[1] && High[1] < Value[1])
            {
            trail = Math.Min(Value[1], Low[0] + loss);

            }
            else if (High[0] >= Value[1] &&High[1] < Value[1] )
            {
            trail = High[0] - loss;

            DrawArrowUp(CurrentBar.ToString(), false, 1, Value[1]-((Ticks+2)*TickSize), Color.Lime);
            }



            else
            {
            trail = Low[0] + loss;

            DrawArrowDown(CurrentBar.ToString(), false, 1, Value[1]+((Ticks+2)*TickSize), Color.Red);
            }

            Value.Set(trail);
            }

            Attempted "Colored Dot indicator";

            if (CurrentBar < 1)
            return;

            // Trailing stop
            double trail=0;
            zero.Set(Linevalue);
            //ts.Set(trail);
            double loss = Ticks*TickSize ;

            if (Low[0] > Value[1] && Low[1] > Value[1])
            {
            trail = Math.Max(Value[1], High[0] - loss);
            condition= true;
            }
            else if (High[0] < Value[1] && High[1] < Value[1])
            {
            trail = Math.Min(Value[1], Low[0] + loss);
            condition= false;
            }
            else if (High[0] >= Value[1] &&High[1] < Value[1] )
            {
            trail = High[0] - loss;
            condition= true;

            }



            else
            {
            trail = Low[0] + loss;
            condition= false;

            }
            ts.Set(trail);

            if(condition==true)
            Plot0.Set(0);
            else
            Plot1.Set(0);

            Comment


              #7
              If your plot's name is Value, this should do the trick:
              Code:
              protected override void OnBarUpdate()
              {
                  if (CurrentBar < 1)
                  return;
                  
                  // Trailing stop
                  double trail;
                  double loss = Ticks*TickSize ;
                  
                  if (Low[0] > Value[1] && Low[1] > Value[1])
                  {
                      trail = Math.Max(Value[1], High[0] - loss);
                  }
                  else if (High[0] < Value[1] && High[1] < Value[1])
                  {
                      trail = Math.Min(Value[1], Low[0] + loss);    
                  }
                  else if (High[0] >= Value[1] &&High[1] < Value[1] )
                  {
                      trail = High[0] - loss;    
                      DrawArrowUp(CurrentBar.ToString(), false, 1, Value[1]-((Ticks+2)*TickSize), Color.Lime);
                  }
                  else
                  {
                      trail = Low[0] + loss;    
                      DrawArrowDown(CurrentBar.ToString(), false, 1, Value[1]+((Ticks+2)*TickSize), Color.Red);
                  }
                  
                  Plot0.Set(trail);
              
                  if (Close[0] > Value[0])
                      DrawDot("green dot" + CurrentBar, false, 0, High[0] + 2 * TickSize, Color.Green);
                  else if (Close[0] < Value[0])
                      DrawDot("red dot" + CurrentBar , false, 0, Low[0] - 2 * TickSize, Color.Red);
                  
              }
              AustinNinjaTrader Customer Service

              Comment


                #8
                Thank you for the quick reply. I realize I didn't explain my objective very well. I wanted to have it position the dots in a horizontal line. Then I can assign a value to where the line will be drawn (private int LineValue=100)

                This is the one I wrote for the Trailing Stop that used Close instead of the High/Low. It worked fine. I just can't seem to get the High/Low Trailing Stop to plot.

                // Wizard generated variables
                private int ticks = 20; // Default setting for Period
                private double linevalue = 0;
                private DataSeries zero;
                private DataSeries ts;
                private double trail=0;
                // User defined variables (add any user defined variables below)
                #endregion

                /// <summary>
                /// This method is used to configure the indicator and is called once before any bar data is loaded.
                /// </summary>
                protected override void Initialize()
                { ts = new DataSeries(this);
                zero = new DataSeries(this);
                Add(new Plot(new Pen(Color.Lime, 1), PlotStyle.Dot, "Plot0"));
                Add(new Plot(new Pen(Color.Red, 1), PlotStyle.Dot, "Plot1"));
                Add(new Plot(new Pen(Color.Yellow, 1), PlotStyle.Dot, "Plot2"));
                CalculateOnBarClose = false;
                Overlay = false;
                PriceTypeSupported = true;
                PaintPriceMarkers = false;
                DisplayInDataBox = false;
                DrawOnPricePanel = false;
                }

                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                //Plot0.Set(Close[0]);

                // Trailing stop
                zero.Set(Linevalue);
                ts.Set(trail);
                double loss = Ticks*TickSize ;

                if (CurrentBar < 1) return;



                if (Close[0] >= ts[1] && Close[1] >= ts[1])

                trail = Math.Max(ts[1], Close[0] - loss);
                else

                if (Close[0] < ts[1] && Close[1] < ts[1])

                trail = Math.Min(ts[1], Close[0] + loss);


                else if (Close[0] >= ts[1])
                {
                trail = Close[0] - loss;
                //Plot2.Set(1,zero[0]);
                //DrawArrowUp(CurrentBar.ToString(), true, 1, zero[1]-(1*TickSize), Color.Lime);
                }

                else if (Close[0] < ts[1])
                {
                trail = Close[0] + loss;
                //Plot2.Set(1,zero[0]);
                //DrawArrowDown(CurrentBar.ToString(), false, 1, zero[1]+(1*TickSize), Color.Red);
                }

                ts.Set(trail);

                if (Close[0] >= ts[0])

                Plot0.Set(zero[0]);
                else
                Plot1.Set(zero[0]);

                Comment


                  #9
                  Thanks Austin

                  Ok, I was able to manipulate the code you wrote and got it to do what I wanted. Thanks!

                  Comment


                    #10
                    Hi Austin,
                    On this, I am using Automatic Trailing Stops in my strategy. How would you set up an indicator, say a DrawDot given NT6.5 already calculates my TrailingStops automatically? Thanks Alot, MKT

                    Comment


                      #11
                      Please check for example in the sharing section for the ATR trailing stop code posted, you could either set your SetStopLoss order to this value, or simply exit at market when the price level is breached.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      581 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      338 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      103 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      554 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      552 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X