Download MetaTrader 5
To add comments, please log in or register
No PC at hand? Communicate with friends using your mobile phone!
Ian Venner
2583
Ian Venner 2014.03.19 02:41 

I don't remember reading anything about the properties of arrays being changed, are they supposed to still be static by default ? In strategy tester 616 they are not unless I explicitly define them as static. I'll post a test code in a few minutes in case anyone wants to check this out.

Ian Venner
2583
Ian Venner 2014.03.19 02:55  

Test code for strategy tester: Recommend run it for one day on 4hr chart to minimise journal output.

Code tests the array once at every bar, if the array indexes are empty (zero) it puts the time in them. Obviously this should only happen once if the array is static. Result printed to journal.

//+------------------------------------------------------------------+
//|                                                   Test Array.mq4 |
//+------------------------------------------------------------------+
#property strict
void OnTick()
  {
//---
   static datetime time=0;
   datetime testarray[3]={0};
//---
   if(time<Time[0])
   for(int i=0; i<3; i++)
   {if(testarray[i]<1)
    {testarray[i]=Time[0];
     Print("debug1: ",i," ",testarray[i]);
   }}
   time=Time[0];
  }
//+------------------------------------------------------------------+

Then change the array to: static datetime testarray[3]={0}; and run the test again.

Keith Watford
Moderator
9543
Keith Watford 2014.03.19 03:32  
SDC:

Test code for strategy tester: Recommend run it for one day on 4hr chart to minimise journal output.

Code tests the array once at every bar, if the array indexes are empty (zero) it puts the time in them. Obviously this should only happen once if the array is static. Result printed to journal.

Then change the array to: static datetime testarray[3]={0}; and run the test again.


I'm a bit confused about your point

datetime testarray[3]={0};

will be actioned everytime whereas

static datetime testarray[3]={0};

will only be actioned at first program run.

Ian Venner
2583
Ian Venner 2014.03.19 03:39  

Why ? Arrays used to be always be static without declaring them as static...

Ian Venner
2583
Ian Venner 2014.03.19 03:42  

I just looked it in the book it still says they are...

...An array can be also initialized by the standard function ArrayInitialize(). All arrays are static, i.e. are of static type even if at the initialization this is not explicitly indicated. It means all arrays preserve their values between calls of the function, in which the array is declared (see Types of Variables).

Ian Venner
2583
Ian Venner 2014.03.19 04:32  

Having brought this up, I can't remember the last time I used local arrays that didn't recieve their values and serve their purpose within the tick, I have been looking through my old code to find something that required them to be static like the example I posted and I cant find anything, maybe they have been "not static" for a long time and I never noticed ...

Keith Watford
Moderator
9543
Keith Watford 2014.03.19 04:36  
SDC:

I just looked it in the book it still says they are...

...An array can be also initialized by the standard function ArrayInitialize(). All arrays are static, i.e. are of static type even if at the initialization this is not explicitly indicated. It means all arrays preserve their values between calls of the function, in which the array is declared (see Types of Variables).


Oh, I see.

I never realised that, but I've always declared them globally when necessary

Ian Venner
2583
Ian Venner 2014.03.19 06:01  

Me too but recently I've been trying to get away from doing it, I keep reading so much about that is bad coding practice.

Keith Watford
Moderator
9543
Keith Watford 2014.03.19 11:12  
SDC:

Me too but recently I've been trying to get away from doing it, I keep reading so much about that is bad coding practice.


I never realised that it was considered bad coding practice, learning a lot today :D

It's my preference to declare globally and often initialise in init, then I don't have to worry about the values when switching timeframes

GreenMoney
379
GreenMoney 2014.03.19 16:15  
SDC:

. . . are [array] supposed to still be static by default ? . . .

SDC:

. . . Arrays used to be always be static without declaring them as static...
SDC:

Having brought this up, I can't remember the last time I used local arrays that didn't recieve their values and serve their purpose within the tick, I have been looking through my old code to find something that required them to be static like the example I posted and I cant find anything, maybe they have been "not static" for a long time and I never noticed ...

Despite your correct quotation from the book, I don't believe arrays were static by default in build 509 and earlier. Consider the following code (compiled on ME build 509 and tested on MT4 509):

int start() {
   array_test();

   return(0);
}

void array_test() {
   int array1[1] = {0};
   static int array2[1] = {0};
   
   // test for array1
   if (array1[0] > 0)
      Print ("array1 is greater than zero.");
   else
      array1[0]++;
   
   // test for array2
   if (array2[0] > 0)
      Print ("array2 is greater than zero.");
   else
      array2[0]++;
}

That code produces the following output in the journal/log:

12:04:38 TestingEA EURUSD,H1: loaded successfully

12:04:39 TestingEA EURUSD,H1: initialized

12:04:41 TestingEA EURUSD,H1: array2 is greater than zero.

12:04:41 TestingEA EURUSD,H1: array2 is greater than zero.

12:04:42 TestingEA EURUSD,H1: array2 is greater than zero.

12:04:45 TestingEA EURUSD,H1: array2 is greater than zero.

12:04:49 TestingEA EURUSD,H1: array2 is greater than zero.

12:04:49 TestingEA EURUSD,H1: array2 is greater than zero.

12:04:51 TestingEA EURUSD,H1: array2 is greater than zero.

12:04:51 TestingEA EURUSD,H1: deinitialized

12:04:51 TestingEA EURUSD,H1: uninit reason 1

12:04:51 TestingEA EURUSD,H1: removed

As you can see, array1 doesn't retain its value between function calls. However, array2 does retain its value because it was declared as static.

This is true even when the array is a local array in start():

int start() {
   int array1[1] = {0};
   static int array2[1] = {0};
   
   // test for array1
   if (array1[0] > 0)
      Print ("array1 is greater than zero.");
   else
      array1[0]++;
   
   // test for array2
   if (array2[0] > 0)
      Print ("array2 is greater than zero.");
   else
      array2[0]++;

   return(0);
}

12:23:51 TestingEA EURUSD,H1: loaded successfully

12:23:53 TestingEA EURUSD,H1: initialized

12:23:53 TestingEA EURUSD,H1: array2 is greater than zero.

12:23:54 TestingEA EURUSD,H1: array2 is greater than zero.

12:23:56 TestingEA EURUSD,H1: array2 is greater than zero.

12:23:59 TestingEA EURUSD,H1: array2 is greater than zero.

12:23:59 TestingEA EURUSD,H1: array2 is greater than zero.

12:23:59 TestingEA EURUSD,H1: array2 is greater than zero.

12:23:59 TestingEA EURUSD,H1: array2 is greater than zero.

12:24:00 TestingEA EURUSD,H1: array2 is greater than zero.

12:24:02 TestingEA EURUSD,H1: deinitialized

12:24:02 TestingEA EURUSD,H1: uninit reason 1

12:24:02 TestingEA EURUSD,H1: removed

.

Ian Venner
2583
Ian Venner 2014.03.19 17:42  

You are probably right, I dont think I wrote any code that actually required local arrays to be static until now, I had just assumed they were because it says they are in the book.

Someone at MQ should amend that section about arrays.

/ /12
To add comments, please log in or register