Error only in monthly MTF indicator

 

Hey, I am making a MTF indicator to show static moving avarage from D1,W1, and MN1 timeframes in H1. Somehow I got it works for D1 and W1 periods, but I always get "array out of range" for MN1 period. Can somebody help me to find the solution for this problem?

Thanks

int counted_bars = IndicatorCounted();
   int limit = Bars - counted_bars - 1;
   int mn1 = 0, mn2 = 0;
   double  ma_mn1;
   datetime time_mn1[];
    
   if (Period() == PERIOD_H1)
   {
      ArrayCopySeries (time_mn1, MODE_TIME, NULL, PERIOD_MN1);
    
      for (mn1 = 0, mn2 = 0; mn1 < limit; mn1++)
      {
         if(Time[mn1] < time_mn1[mn2]) mn2++;
         ma_mn1 = iMA(NULL,PERIOD_MN1,1,mn2,MODE_SMA,PRICE_WEIGHTED,0);
         ma_1month[mn1] = ma_mn1;         
      }
   }
 

limit is given a value according to the number of bars on the H1 chart, but used in the loop as an index for the MN1 chart.

If there are more bars on the H1 chart than on the MN1 chart, then you will get the array out of range error 

 
GumRai:

limit is given a value according to the number of bars on the H1 chart, but used in the loop as an index for the MN1 chart.

If there are more bars on the H1 chart than on the MN1 chart, then you will get the array out of range error 

Thanks sir for the reply, yeah that's true in the logic, but why for period D1 and W1 works.
As we say before limit = bars on H1 that used for counting in MN1, so that bars H1> bars MN1. That's why it gives "array out of range"  error. In this case bars H1 are also more than bars D1, but why does it work?

Note: I have tried to change 
limit = 30 - counted_indicator - 1
But it always gives me "array out of range" error.

 

Sorry for my bad advice.

I just noticed that the iMA call on the monthly chart is always bar 0 

 
GumRai:

Sorry for my bad advice.

I just noticed that the iMA call on the monthly chart is always bar 0 

Ah,no problem sir. No advice is bad though.

Till now still no solution, if you come up with something please tell me  sir.

Thanks sir

 

You don't need to guess how many bars you have available on MN1 - see iBars()

 

Unlikely, but if the earliest bar on the H1 chart is earlier than the earliest bar on the MN1 chart

if(Time[mn1] < time_mn1[mn2]) mn2++;

 will likely give the error

check that mn2 is at least 1 less than iBars on MN1 

 
GumRai:

Unlikely, but if the earliest bar on the H1 chart is earlier than the earliest bar on the MN1 chart

 will likely give the error

check that mn2 is at least 1 less than iBars on MN1 

 

Thanks sir, I have found the problem. It is because of the IndicatorBuffer name, haha

Now it works, but it only show the line for  1 month in H1 timeframes (yeah, another problem).