Please help regarding global and static variables

 

Dear All

Could anyone advise me regarding my questions regarding the above. I have just started learning MQL4 with 0 background in programming. I have gone through the book multiple times. I would like to draw 2 examples from the book.


Example 1


Problem 22. Create a program that counts ticks.

Solution algorithm of Problem 22 using a global variable (countticks.mq4):

//--------------------------------------------------------------------
// countticks.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
int Tick;                              // Global variable
//--------------------------------------------------------------------
int start()                            // Special function start()
  {
   Tick++;                             // Tick counter
   Comment("Received: tick No ",Tick); // Alert that contains number
   return;                             // start() exit operator
  }
//--------------------------------------------------------------------

The global variable "Tick" should increase by 1 and the value stored after every execution of int start(), however the value stays the same at 1 when i use a script to test. This leads me to wonder am i right to say that the value is stored only when using an EA and the value is erased only denit(), i cant test this theory using an EA now as the market is closed.

Below is the solution of Problem 22 using a static variable (Expert Advisor staticvar.mq4 ):

//--------------------------------------------------------------------
// staticvar.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
int start()                            // Special function start()
  {
   static int Tick;                    // Static local variable
   Tick++;                             // Tick counter
   Comment("Received: tick No ",Tick); // Alert that contains number
   return;                             // start() exit operator
  }
//--------------------------------------------------------------------

In the above case, "Tick" should also increase by 1 after every execution of int start(), however again it stays at 1 so is the problem the same one as above?

And also i was wondering how the tick retains its increasing value as everytime int start() is executed, since "static int tick" equals zero by default, won't the value be reverted to zero before executing the line "Tick++"





 

Scripts are designed to be used once therefore they do-not save their values. You can test EA by using the Strategy Tester even when the market is closed. 

 
existence:
[...]

And also i was wondering how the tick retains its increasing value as everytime int start() is executed, since "static int tick" equals zero by default, won't the value be reverted to zero before executing the line "Tick++"


That's the point of Static variables, they hold their value inside a function whereas normal declerations are re initialised every time the funtion runs. In this case the function is start() and runs every tick. The first example declares the variable outside of the function on the global scope and therefore it is not re initialised every time the function start() runs. In the second example, to have the same effect as global scope inside a function, it needs to be declared as static.

hth

V

 
ubzen:

Scripts are designed to be used once therefore they do-not save their values. You can test EA by using the Strategy Tester even when the market is closed.

Thank you, will try it out.
 

V

> the function is start() and runs every tick...

That is providing it has completed its code before the next tick comes in

If start() is still running when the next tick comes in, that data is missed by the EA

-BB-

 
Viffer:

That's the point of Static variables, they hold their value inside a function whereas normal declerations are re initialised every time the funtion runs. In this case the function is start() and runs every tick. The first example declares the variable outside of the function on the global scope and therefore it is not re initialised every time the function start() runs. In the second example, to have the same effect as global scope inside a function, it needs to be declared as static.

hth

V

Thank you, I do understand now.
 
The global variable "Tick" should increase by 1 and the value stored after every execution of int start(), however the value stays the same at 1 when i use a script to test. This leads me to wonder am i right to say that the value is stored only when using an EA and the value is erased only denit(), i cant test this theory using an EA now as the market is closed.

The values are not erased or reset in deinit. The values are set only when the EA/indicator is first loaded. Just change the chart timeframe or pair. The EA goes through a deinit/init cycle but the global/statics remain unchanged.

That's why code that modifies globals or externals like

double  pips2points=1;
int init(){
    if (Digits == 5 || Digits == 3){    pips2dbl *= 10; 
    ...
Will not work. Must use code like to reset variables on init:
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
 
WHRoeder:

The values are not erased or reset in deinit. The values are set only when the EA/indicator is first loaded. Just change the chart timeframe or pair. The EA goes through a deinit/init cycle but the global/statics remain unchanged.

That's why code that modifies globals or externals like

Will not work. Must use code like to reset variables on init:

Hi

Not sure i understand correctly. The code seems to be truncated. I can't see any difference in the 1st and 2nd example. The global variable pips2dbl will take on either value of 10 or 1 after the launch of the EA.

I don't see the difference in defining it as 1 or not defining it before init()

 
existence:

Hi

Not sure i understand correctly. The code seems to be truncated. I can't see any difference in the 1st and 2nd example. The global variable pips2dbl will take on either value of 10 or 1 after the launch of the EA.

I don't see the difference in defining it as 1 or not defining it before init()

In the first example, pips2point is declared =1 on the global scope (same as being a static) and then on init() is multiplied by 10. If you change the timescale, it runs init() again but doesn't run the global decleration that pips2point=1.... it still thinks it is 10 so would change it in init() to 100.


In the second example, the calculation of pips2point is contained wholly within init(). it resets pip to point to be 10 (or whatever) every time it is run without being reliant on any value outside of the function.

V

Reason: