Can't find the logic error in this simple code

 
datetime last_bg_routine = 0;

int bg[10][6];

int init()
{

        ArrayInitialize(bg, 2);

        return(0);

}

void BgRoutine()
{
   int i, j;
   if (iTime(NULL, PERIOD_M1, 1) != last_bg_routine)
   {
      last_bg_routine = iTime(NULL, PERIOD_M1, 1);
      for (i = 9; i >= 1; i--)
      {
         for (j = 0; j <= 5; j++)
            bg[i][j] = bg[i-1][j];
      }
      bg[0][0] = Bg(PERIOD_M1);
      bg[0][1] = Bg(PERIOD_M5);
      bg[0][2] = Bg(PERIOD_M15);
      bg[0][3] = Bg(PERIOD_M30);
      bg[0][4] = Bg(PERIOD_H1);
      bg[0][5] = Bg(PERIOD_H4);
   }
   return;
}

The function BgRoutine() is called from start(), and it's supposed to run every 1 minute bar (and it does).

for (i = 9; i >= 1; i--)
{
   for (j = 0; j <= 5; j++)
      bg[i][j] = bg[i-1][j];
}

This part above is supposed to copy the rows from 0 to 9 (row 0 goes to row 1, row 1 goes to row 2, etc, but in the inverse way, so that the 0th row doesn't override all the others), and so it always overrides the last row every time it's run.

bg[0][0] = Bg(PERIOD_M1);
bg[0][1] = Bg(PERIOD_M5);
bg[0][2] = Bg(PERIOD_M15);
bg[0][3] = Bg(PERIOD_M30);
bg[0][4] = Bg(PERIOD_H1);
bg[0][5] = Bg(PERIOD_H4);

This part above puts the values of the function Bg() for the given period in the first row of the 2-dimensional array bg.

So my goal with this was to gradually (1 time per minute bar) fill the 2-dimensional array bg with the values of the function Bg(). In the first time the function ran, only the first row would have values different from 2. On the 2nd time, both the 1st and 2nd row would have values different than 2, etc... But what happens is that the 0th row overrides all the others every time this code runs, so I get a matrix with equal rows... I seriously can't find the logic error here..

 
I see nothing.
 
WHRoeder:
I see nothing.

I know right, this is so strange. Every time things like these happened to me in the past, I could figure them out. But I just can't see any problem here :|
 

I can't see anything wrong either - the only ridiculus thing I can think of is that the values returned by Bg() are alaways the same! Which will be true for all values above PEROID_M5 if its updated every minute.

 
Ickyrus:

I can't see anything wrong either - the only ridiculus thing I can think of is that the values returned by Bg() are alaways the same! Which will be true for all values above PEROID_M5 if its updated every minute.


Yea that could be it, but even if I wait only 1 minute bar it fills the whole array (when in this case it should only change the first row's values)..
 
if (iTime(NULL, PERIOD_M1, 1) != last_bg_routine)

Grrr How could I not spot the shift 1 !!!! should be zero

   if (iTime(NULL, PERIOD_M1, 0) != last_bg_routine)
   {
      last_bg_routine = iTime(NULL, PERIOD_M1, 0);
 
Ickyrus:

Grrr How could I not spot the shift 1 !!!! should be zero


What's the difference?
 
Jasus5457:

What's the difference?

Good point!!! Just used to it being 0 - For a moment there thought I had it (sheepish embarased look!)
 
as long as they match
   if (iTime(NULL, PERIOD_M1, 1) != last_bg_routine)
   {
      last_bg_routine = iTime(NULL, PERIOD_M1, 1);
 
//+------------------------------------------------------------------+
//|                                                   BG_Routine.mq4 |
//|                               Copyright © 2012,  Tjipke de Vries |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012,  Tjipke de Vries"
#property link      ""

datetime last_bg_routine = 0;

int bg[10][6];
int k=0;


#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators


   //ArrayInitialize(bg, 2);  //set empty array on 2


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   static datetime last_bg_routine;
//----
      bg[0][0] = 0+k;           //Bg(PERIOD_M1);/
      bg[0][1] = 1+k;           //Bg(PERIOD_M5);/ 
      bg[0][2] = 2+k;
      bg[0][3] = 3+k;
      bg[0][4] = 4+k;
      bg[0][5] = 5+k;

   if (iTime(NULL, PERIOD_M1, 1) != last_bg_routine)
     {
      BgRoutine();
      k++;
      last_bg_routine=iTime(NULL, PERIOD_M1, 1);
     } 
//----
   return(0);
  }
//+------------------------------------------------------------------+
void BgRoutine()
  {
   int i, j;    
   for (i = 9; i >= 1; i--)
     {
      j = 0;
      for (j = 5; j >= 0; j--)
        {
         bg[i][j] = bg[i-1][j];
         Print("bg[",i,"][",j,"] = ",bg[i][j]);
        } 
     }   
   return;
  }

Don't know what your Bg( ) function is so so i filled it with 0,1,2,3,4,5 to check what happens

and every minute it has to count up 1..

BgRoutine is called from Start() and it has to run once every 1 minute

So you only need to call it once in Start() that Period() i guess

Think if you now change it back to your function it is doing what you want

Reason: