The issue is that you are mixing series and non-series indexing.
In OnCalculate, the price arrays (high[], low[], etc.) are already in series mode (shift 0 = current bar), but your code changes their orientation and then calculates ATR with different indexing, which desynchronizes the values.
Always use the same indexing (either series or non-series, but consistent) and return rates_total instead of rates_total - 1.
The mixed indexing is right. Rather than working out the correct reverse-offset for every CopyBuffer call inside the loop, it's simpler to pull the whole ATR range once per OnCalculate and index it the same direction as the price arrays:
double atrBuffer[]; ArraySetAsSeries(atrBuffer, false); CopyBuffer(atrHandle, 0, 0, rates_total, atrBuffer); // then inside the loop: atr = atrBuffer[i];
Copying the full range once and reading atrBuffer[i] directly keeps every array in the function using the same oldest-to-newest order as time[], high[], and low[], so there's no separate offset formula to get wrong per bar.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, why in my script ,the high[] not return a good high ?