Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw Trailing Stops

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

    Draw Trailing Stops

    Hello, I would like to plot a a line on my strategy chart that shows the movement of the trailing stop. According the NT documentation it seem not possible to use the Add CustomStrategy function if I backtest my strategy. Therefore I used DrawDot with the expectation that it draws a dot for each trailing stop value. My code looks as follows:

    DrawDot(CurrentBar.ToString("0.00") + "Trail",true, 0,trailStopPrice,Color.Red);

    "trailStopPrice" is my variable that I define via SetStopLoss based on an ATR based trailing stop calculation.

    Unfortunately I get just a straight line per trade (see enclosed sample). Could anyone me a hint please.

    Thanks
    Attached Files

    #2
    Hello phadreus,

    Thank you for your post.

    Do you wish to only draw a line for each instance of the Stop Loss? Where is the DrawDot() located in your code (i.e. what condition triggers it's drawing)?

    Comment


      #3
      Thanks Patrick, indeed there was a mistake in my code. Nevertheless another question: I used the DrawDot method an get (which wonder) dots as a result (see enclosed image). Is there a method I can use to draw my trailing stop as a line? The DrawLine method does not work if I understand it right.

      Here is my code:

      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(CalculationMode.Percent, trailStopPercent);
      }

      else if (Position.MarketPosition == MarketPosition.Long)
      {
      if (Close[0] > Position.AvgPrice)
      {

      trailStopPrice = Close [0] - ATR(volatiliyPeriod)[0] * volatilityMultiplier;

      SetStopLoss(CalculationMode.Price,trailStopPrice);

      DrawDot(CurrentBar.ToString("0.00") + "Trail",true, 0,trailStopPrice,Color.Red);

      PrintWithTimeStamp ("ATR " + ATR(21)[0].ToString("0.00") + ", " + "Close " +
      Close[0].ToString("0.00") + ", " + "Trailstop "+ trailStopPrice.ToString("0.00"));
      }

      Thanks for your help
      Attached Files

      Comment


        #4
        Hello phadreus,

        Thank you for your response.

        Below is an example of tracking the trailing stop with a line:
        Code:
                #region Variables
        		private int barAtEnt = 0;
                #endregion
        
                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()
                {
        			SetTrailStop(CalculationMode.Ticks, 20);
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
        			if(Historical)
        				return;
        			
        			if(Close[0] > Open[0])
        			{
        				EnterLong("enter");
        			}
        			if(Position.MarketPosition != MarketPosition.Flat)
        			{
        				DrawLine("stop", CurrentBar+1 - barAtEnt, Position.AvgPrice - (20 *TickSize), -1, Position.AvgPrice - (20 *TickSize), Color.Red);
        			}
        		}
        		
        		protected override void OnExecution(IExecution execution)
        		{
        			if(execution.Order != null && execution.Order.Name == "enter" && execution.Order.OrderState == OrderState.Filled)
        			{
        				barAtEnt = CurrentBar;
        			}
        		}

        Comment


          #5
          Thanks Patrick,

          the DrawLine method works but it still provides a separate dash for each new trailing stop per bar. If the trailing stop is not changed no dash is drawn. What I am looking for is a possibility to plot the trailing stop as a curve. Is there a mistake in my coding or is there another method I can use?

          Thanks

          Here my current coding:

          else if (Position.MarketPosition == MarketPosition.Long)
          {
          if (Close[0] >= lastHigh)
          {

          lastHigh = Close[0];

          trailStopPrice = Close [0] - ATR(volatiliyPeriod)[0] * volatilityMultiplier;

          SetStopLoss(CalculationMode.Price,trailStopPrice);

          DrawLine(CurrentBar.ToString("0.00") + "Trail", true, 0, trailStopPrice, -1,
          trailStopPrice, Color.Red, DashStyle.Dot, 2);

          }
          }
          Attached Files

          Comment


            #6
            Hello phadreus,

            Thank you for your response.

            You will want to incorporate your code into the code I provided: http://www.ninjatrader.com/support/f...13&postcount=4

            Comment


              #7
              Patrick, many thanks. Your code works but I still get a kind of dotted line (actually a dash per bar). I suppose that´s what the DrawLine method should provide. But what I´am looking for is trailing stop line as shown in the enclosed image. Could you pleas give me another hint. Thanks
              Attached Files

              Comment


                #8
                Hello phadreus,

                Thank you for your response.

                Then you would want to set this as a plot. Please take a look at the following link for a reference sample on using plots within a strategy: http://www.ninjatrader.com/support/f...ead.php?t=6651

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                648 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                369 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                573 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                575 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X