Problem with 3SMA's

 

3 SMA's: 30, 50, 100

4 TimeFrames: 30, 60, 240, 1440

What I try to find is the direction of the SMA's for these Time Frames. The first and second TimeFrame is right but the third and fourth is wrong!

I can't find the bug. Anyone?

#property copyright "Copyright 2005, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"
 
#property indicator_chart_window
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorShortName("TEST");
Comment("");
//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
Comment("");
//----
//----
return(0);
}
 
int start()
{
 
int p=0;
int per[]    = {30, 60, 240, 1440};
int sma[]    = {30, 50, 100};
int checkA[12];
 
for(int k=0; k<5; k++)
   {
   for(int i=0;i<4;i++)
      {
      if ((iMA(NULL,per[k],sma[i],0,MODE_SMA,PRICE_CLOSE, 1) - (iMA(NULL,per[k],sma[i],0,MODE_SMA,PRICE_CLOSE, 10))) > 0) checkA[i+p]=3; 
      else checkA[i+p]=2;
      }
   p=p+4;
   }
 
Comment(checkA[0],"\n",
checkA[1],"\n",
checkA[2],"\n",
checkA[3],"\n",
checkA[4],"\n",
checkA[5],"\n",
checkA[6],"\n",
checkA[7],"\n",
checkA[8],"\n",
checkA[9],"\n",
checkA[10],"\n",
checkA[11],"\n");
 
 
return(0);
}
 
int per[] has only four entries, so k should range from 0 to 3, inclusive?
int sma[] has only three entries, so i should range from 0 to 2, inclusive?
 

I agree with richplank

for(int k=0; k<4; k++)
   {
   for(int i=0;i<3;i++)
      {
      if ((iMA(NULL,per[k],sma[i],0,MODE_SMA,PRICE_CLOSE, 1) - (iMA(NULL,per[k],sma[i],0,MODE_SMA,PRICE_CLOSE, 10))) > 0) checkA[i+p]=3; 
      else checkA[i+p]=2;
      }
   p=p+4;
   }