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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      47 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      23 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      33 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      51 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X