array out of range on strict?

 

Why on the flowing code do i get array out of range error only when on property strict?

int start()
{
   double atr,lowprice_i,highprice_i,lowma,highma;
   bool nexttrend=0;
   double minhighprice= High[Bars-1];
   double maxlowprice = Low[Bars-1];
   
   //+------------------------------------------------------------------+
   // Trend Multi Timeframe 
   //+------------------------------------------------------------------+  
   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;

      if(nexttrend==1)                              
      {
         maxlowprice=MathMax(lowprice_i,maxlowprice); 
                                                       
         if(highma<maxlowprice && iClose(_Symbol,_Period,i)<iLow(_Symbol,_Period,i+1))          
         {     
            trend[i]=1.0;                                                            
            nexttrend=0;                                        
            minhighprice=highprice_i;              
         }
      }
      if(nexttrend==0)                      
      {
         minhighprice=MathMin(highprice_i,minhighprice);

         if(lowma>minhighprice && iClose(_Symbol,_Period,i)>iHigh(_Symbol,_Period,i+1))
         {
            trend[i]=0.0;         
            nexttrend=1;
            maxlowprice=lowprice_i;
         }
      }
   }
   
   return (0);
}

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?

 
Stephen Reynolds:

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];
 
Stephen Reynolds:
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.

Reason: