Array out of range in a custom average function

Array out of range in a custom average function

Manage alerts

Loading saved threads...

Ilya Mikheev · External communityPost link
External question — Stack Overflow Stack Exchange Author: Ilya Mikheev Original post: https://stackoverflow.com/questions/64011775 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. So im writing a custom atr indicator with multipliers (Display 2 lines, first line is raw atr value, and second one is atr*1.5), but for some reason it says "array out of range (66,28)", where I calculate my average of the true range. I did use SetIndexBuffer on my arrays, so the problem should be in OnCalculate(). This is what I have in my OnCalculate function: for(int i=0;i<rates_total-prev_calculated;i++){ TRBuffer[i] = MathMax(MathMax(high[i]-low[i], MathAbs(high[i]-close[i+1])),MathAbs(low[i]-close[i+1])); if(prev_calculated-1<=ATRLength){ double sumTR=0; for(int j=0;j<ATRLength;j++){ sumTR+=TRBuffer[i+j]; } double atr = sumTR/ATRLength; TRBuffer[i] = atr*TPMultiplier; SLBuffer[i] = atr*SLMultiplier; } }
Quote
Report
Ilya Mikheev · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Ilya Mikheev Original post: https://stackoverflow.com/a/64026088 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Found the answer, this is the final code. if(rates_total<=ATRLength) return 0; int limit = rates_total-prev_calculated; if(prev_calculated>0) limit++; double ATRVal; for(int i=limit-2; i>1; i--){ TRBuffer[i] = MathMax(MathMax(high[i]-low[i], MathAbs(high[i]-close[i+1])),MathAbs(low[i]-close[i+1])); ATRVal = iMAOnArray(TRBuffer, 0, ATRLength, 0, MODE_SMA, i); TPBuffer[i] = ATRVal*TPMultiplier; SLBuffer[i] = ATRVal*SLMultiplier; }
Quote
Report

Post Reply

Checking account access…
Array out of range in a custom average function | Forex.com.bd