Download MetaTrader 5

Error only in monthly MTF indicator

To add comments, please log in or register
hotpot
16
hotpot  

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;         
      }
   }
Keith Watford
Moderator
10644
Keith Watford  

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 

hotpot
16
hotpot  
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.

Keith Watford
Moderator
10644
Keith Watford  

Sorry for my bad advice.

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

hotpot
16
hotpot  
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

honest_knave
Moderator
2333
honest_knave  

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

Keith Watford
Moderator
10644
Keith Watford  

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 

hotpot
16
hotpot  
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).

To add comments, please log in or register