Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SUM Function Overflow?

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

    SUM Function Overflow?

    I am designing an adaptive lookback period for Kaufman's Efficiency Ratio. The Efficiency Ratio logic is copied from the built-in KAMA indicator. Here is the logic.
    Basically I want a the lookback period to be able to change on every bar defined by this integer p, p is between _MinERPeriod and _BaseERPeriod. However, when too many bars are loaded. The variable
    Code:
    noise
    is getting weird. It can sometimes become negative or very close to 0. This issue goes away if I load less than 1000 candle bars. I am wondering if there is a way to get around this issue.


    Code:
    protected override void OnBarUpdate()
            {
                er[0] = 0;
                diffSeries[0] = 0;
                diffSeries[0] = CurrentBar > 0 ? Math.Abs(Input[0] - Input[1]): 0;
                double DCWidthBase = DonchianChannel(_BaseERPeriod).Upper[0] - DonchianChannel(_BaseERPeriod).Lower[0];
                int p = _MinERPeriod;
                for (int i = _MinERPeriod; i<= _BaseERPeriod; i++)
                {
                    p = i;
                    double DCWidth = DonchianChannel(i).Upper[0] - DonchianChannel(i).Lower[0];
                    if (DCWidth >= _PctThreshold * DCWidthBase)
                    {
                        break;
                    }
                }
                if (CurrentBar < DCWidthBase)
                {
                    return;
                }
                
                double signal = Math.Abs(Input[0] - Input[p]);
                double noise = SUM(diffSeries, p)[0];
                
                if (noise == 0)
                {
                    er[0] = er[1];
                    return;
                }
                if (noise < 0)
                {
                    Print(p);
                    return;
                }
                er[0] = signal / noise;
                ER[0] = er[0];​
    }

    #2
    protected override void Initialize()
    {
    diffSeries = new DataSeries(this);
    noiseSums = new Sum[_BaseERPeriod - _MinERPeriod + 1];
    for(int i = _MinERPeriod; i <= _BaseERPeriod; i++)
    noiseSums[i - _MinERPeriod] = SUM(diffSeries, i);
    }

    protected override void OnBarUpdate()
    {
    // need at least one prior bar for diff
    if (CurrentBar == 0) return;
    diffSeries[0] = Math.Abs(Input[0] - Input[1]);

    // pick p based on Donchian widths
    double baseWidth = DonchianChannel(_BaseERPeriod).Upper[0]
    - DonchianChannel(_BaseERPeriod).Lower[0];
    int p = _BaseERPeriod;
    for(int i = _MinERPeriod; i <= _BaseERPeriod; i++)
    {
    double w = DonchianChannel(i).Upper[0] - DonchianChannel(i).Lower[0];
    if (w >= _PctThreshold * baseWidth)
    {
    p = i;
    break;
    }
    }

    // make sure we have at least p bars
    if (CurrentBar < p)
    {
    er[0] = er[1];
    return;
    }

    // compute ER safely
    double signal = Math.Abs(Input[0] - Input[p]);
    double noise = noiseSums[p - _MinERPeriod][0];
    noise = Math.Max(noise, 1e-8);

    er[0] = signal / noise;
    ER[0] = er[0];
    }


    With that in place:
    • You never call SUM with a period you don’t yet have bars for.
    • You never let noise go to zero or negative.
    • NinjaTrader’s internal SUM buffers stay in lockstep with your DataSeries.

    That will eliminate the weird negative/near-zero spikes when you load thousands of bars.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    647 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
    572 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X