Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Math seems correct, but Plot is off

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

    Math seems correct, but Plot is off

    I have a couple rate of change indicators that I am working on and it seems like it would be simple, but I am obviously missing something. The 'slope' indicator is in purple on a 100 SMA, that seems to be working fine. I have the same 100 SMA settings being used in the 'SlopeAccel' indicator that I am working on below. The math seems super simple, I just want the difference in the slope from the previous bar to this bar. But the plot shows that I am clearly in the wrong somewhere.

    Click image for larger version

Name:	2020-03-25_08h51_15.png
Views:	334
Size:	18.2 KB
ID:	1091650

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class SlopeAccel : Indicator
        {
    
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Rate of change.";
                    Name                                        = "SlopeAccel";
                    Calculate                                    = Calculate.OnPriceChange;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    Period                    = 100;
                    AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.Bar, "Accel");
                    AddLine(Brushes.DeepSkyBlue, 1, "Strength");
                }
                else if (State == State.DataLoaded)
                {
    
                }
            }
    
            protected override void OnBarUpdate()
            {
            if(CurrentBar < 2) return;
    
                slp[0] = 1000 * Slope(SMA(Period),10,0);
    
                Accel[0] = slp[0] - slp[1];
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Period", Order=1, GroupName="Parameters")]
            public int Period
            { get; set; }
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> Accel
            {
                get { return Values[0]; }
            }
            [Browsable(false)]
            [XmlIgnore()]
            public Series<double> slp
            {
                get { return Values[0]; }
            }
    
            #endregion
    
        }
    Attached Files

    #2
    Hello Mabba,

    Thank you for the post.

    The math you are doing is taking the current plot value minus the previous:

    Code:
     Accel[0] = slp[0] - slp[1];
    One potential problem I see is that you used the same Plot for both properties:
    Code:
     
        [Browsable(false)]
    [XmlIgnore()]
    public Series<double> [B]Accel[/B]
    {
        get { return [B]Values[0];[/B] }
    }
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> [B]slp[/B]
    {
        get { return [B]Values[0];[/B] }
    }
    These both return the same plot so they contain the same values. I believe in this case you would need to add a second plot for what you are asking.

    The slp plot contains the calculated values from the indicator:

    Code:
     slp[0] = 1000 * Slope(SMA(Period),10,0);
    And the second plot would contain your difference:

    Code:
    Accel[0] = slp[0] - slp[1];
    But because these currently represent the same plot that just re writes its value which is then used as the next slp[1] instead of the actual value.



    Try making the following change:


    Code:
    [B]AddPlot(new Stroke(Brushes.Transparent, 2), PlotStyle.Bar, "slp");[/B]
    AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.Bar, "Accel"); // this is now the second plot
    
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Accel
    {
       get { return [B]Values[1];[/B] } // this now references the second plot
    }
    You would need to remove the indicator from where you applied it and re apply it after this change.



    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    639 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    366 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    107 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    569 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    572 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X