Why on the flowing code do i get array out of range error only when on property strict?
I know the problem is
But im not sure how to get around what i need to do? Use i and i+1 as shown above. To compare current and previous values?
I don't know if uses
int trend[Bars-1];
The maximum value of 'i' is Bars-1.
So need the maximum array of trend variable is at least equal of i+1
Try this...
int trend[Bars];
for(int i=Bars-1; i>=0; i--) { // Timeframe lowprice_i=iLow(_Symbol,_Period,iLowest(_Symbol,_Period,MODE_LOW,Amplitude,i)); highprice_i=iHigh(_Symbol,_Period,iHighest(_Symbol,_Period,MODE_HIGH,Amplitude,i)); lowma=NormalizeDouble(iMA(_Symbol,_Period,Amplitude,0,MODE_SMA,PRICE_LOW,i),Digits()); highma=NormalizeDouble(iMA(_Symbol,_Period,Amplitude,0,MODE_SMA,PRICE_HIGH,i),Digits()); trend[i]=trend[i+1]; atr=iATR(_Symbol,_Period,100,i)/2;
(I am assuming you declared your trend[] as an Indicator Buffer earlier in your code)
Array Out of Range means you are looking for an Index (Candle in your case) that does not exist.
You loop starts at "Bars-1". (The Oldest Candle that exists on the chart)
When analysing the Oldest Candle (Bars-1), you are requesting info about "i+1" (Bars-0).
This Candle/Index "Bars-0 or i+1" does not Exist.
There are multiple ways to fix this but, in summary, create a "int Limit" instead of using Bars in you for loop header.
That Limit will need to be Adjusted for the First Loop of the Indicator (!prev_calculated), and then readjust for the subsequent loops.
I unfortunately don't have links to other Trends but I know for sure that there are some on here. Search for "How to do your Lookbacks Correctly".
for(int i=Bars-1; i>=0; i--)
See How to do your lookbacks correctly #9 — #14 & #19.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Why on the flowing code do i get array out of range error only when on property strict?
I know the problem is
trend[i]=trend[i+1]
But im not sure how to get around what i need to do? Use i and i+1 as shown above. To compare current and previous values?