Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Troubleshooting Trailing-Stop Plot Issues in Custom Indicator

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

    Troubleshooting Trailing-Stop Plot Issues in Custom Indicator

    Hello NinjaTrader Forum,

    This is my first post here, and I'm open to guidance if there's a better way to engage with the community.

    I'm working on a custom indicator that has two states: one for when a WMA is bullish and another for when it's bearish. For a bullish WMA, it should plot the minimum price of the candle minus the ATR value. Conversely, for a bearish WMA, it should plot the maximum price of the candle plus the ATR value. This is intended as a visual guide for a trailing-stop.

    However, I'm encountering an issue where the plot always appears below the prices, even when the WMA is bearish, which doesn't make sense. The logic seems correct. I declare the plot value variables at the beginning of the class:

    Code:
    private double tsAtrEnLargos1;
    private double tsAtrEnCortos1;
    And I handle the assignments and value changes within OnBarUpdate(). What am I missing?

    Here the code for reference, though I should mention there are actually three plots involved-I've simplified the explanation to focus on the main issue.

    Code:
    protected override void OnBarUpdate()
    {
    double valorWMA = WMA(PeriodoWMA)[0];
    Values[0][0] = valorWMA;
    double valorATR;
    
    if (CurrentBar > 0)
    {
    valorATR = ATR(PeriodoATR)[1];
    double maximo = High[1];
    double minimo = Low[1];
    
    if (tsAtrEnLargos1 < minimo-valorATR)
    {
    tsAtrEnLargos1 = minimo-valorATR;
    tsAtrEnLargos2 = minimo-(valorATR * 2);
    tsAtrEnLargos3 = minimo-(valorATR * 3);
    }
    if (tsAtrEnCortos1 > maximo+valorATR)
    {
    tsAtrEnCortos1 = maximo+valorATR;
    tsAtrEnCortos2 = maximo+(valorATR * 2);
    tsAtrEnCortos3 = maximo+(valorATR * 3);
    }
    
    if (valorWMA > WMA(PeriodoWMA)[1])
    {
    PlotBrushes[0][0] = Brushes.Green;
    Values[1][0] = tsAtrEnLargos1;
    Values[2][0] = tsAtrEnLargos2;
    Values[3][0] = tsAtrEnLargos3;
    PlotBrushes[1][0] = new SolidColorBrush(Color.FromArgb(100, 0, 0, 255));
    PlotBrushes[2][0] = new SolidColorBrush(Color.FromArgb(150, 0, 0, 255));
    PlotBrushes[3][0] = new SolidColorBrush(Color.FromArgb(200, 0, 0, 255));
    }
    else if (valorWMA < WMA(PeriodoWMA)[1])
    {
    PlotBrushes[0][0] = Brushes.Red;
    Values[1][0] = tsAtrEnCortos1;
    Values[2][0] = tsAtrEnCortos2;
    Values[3][0] = tsAtrEnCortos3;
    PlotBrushes[1][0] = new SolidColorBrush(Color.FromArgb(100, 255, 0, 0));
    PlotBrushes[2][0] = new SolidColorBrush(Color.FromArgb(150, 255, 0, 0));
    PlotBrushes[3][0] = new SolidColorBrush(Color.FromArgb(200, 255, 0, 0));
    }
    }
    else
    {
    valorATR = ATR(PeriodoATR)[0];
    tsAtrEnCortos1 = Double.PositiveInfinity;
    }
    }
    Thanks in advance for your help!

    Best
    Attached Files
    Last edited by DanielSanMartin; 04-17-2024, 02:49 PM.

    #2
    Hello DanielSanMartin,

    Welcome to the NinjaTrader forums!

    Use prints to debug and understand the behavior.

    There appear to be two conditions that set the plot value

    if (tsAtrEnCortos1 > maximo+valorATR)

    else if (valorWMA < WMA(PeriodoWMA)[1])

    Print the time of the bar and all of the values in these conditions (i.e. tsAtrEnCortos1, maximo, valorATR, maximo+valorATR, valorWMA, and WMA(PeriodoWMA)[1] to the output window.
    Include labels for each value and comparison operator so we know what is being compared and how in the print output.

    Below is a link to a support article that demonstrates using prints to understand behavior and includes links to videos.


    After adding the prints, save the output to a text file (right-click the output window > select Save as).
    Attach the output text file to your next post and I will be happy to assist with analyzing the output.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I found the problem. There is something wrong with the logic. I could say "incomplete", since it is not anticipating a sharp change in trend. Now it's my turn to think about the solution. Thanks for the good predisposition.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      599 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      344 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
      558 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      557 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X