Static Variables

 

Dear All,

Please help and advice about the static variables behavior.

The problem, let x be such static counter as shown below.

Suppose that, x=5 at the previous ticks.

At the current tick; assume that, the "condition" is met and hence x= 6.

The question is, what about the "Calculate = f(x)" which value will be taken for calculating the f(x) expression?

It should recall the x=6 value as x is not local variable.

static int x = 0;

int start()
{
...
...
...
if (condition) x++;
...
...
...
int Calculate = f(x);
...
...
...
return;
}

Please clarify.

 

I did not try it out, but the static keyword should not compile in your example.

It has effect only inside the global-level function, i.e. it must be placed inside your start() function block. When variable is placed outside - at the global level, it is already permanent.

 
Ovo:

I did not try it out, but the static keyword should not compile in your example.

It has effect only inside the global-level function, i.e. it must be placed inside your start() function block. When variable is placed outside - at the global level, it is already permanent.

That compiles. But it's useless as global variables are already static.
 
Dears, Please help me answering my question, I'm not interested in compile circumstances, I'm interested in its functionality. In other words; within the same tick, there are assignment and calculation expressions. My question is, when the assignment done first, which value will be used in the calculation expression? The new value occurred within the tick or the old value before the tick takes place?
 
OmegaFX: when the assignment done first, which value will be used in the calculation expression? The new value occurred within the tick or the old value before the tick takes place?
Neither. The assignment to static's or globally declared variables is done ONCE, when the code is FIRST loaded. After that, within the tick, it will be incremented and used per your code: 0, 1, 2 ...
 
OmegaFX:
Dears, Please help me answering my question, I'm not interested in compile circumstances, I'm interested in its functionality. In other words; within the same tick, there are assignment and calculation expressions. My question is, when the assignment done first, which value will be used in the calculation expression? The new value occurred within the tick or the old value before the tick takes place?


In your example static int x is initialized to zero value before the tick. If condition is true a new value is stored in the programs stack for that variable and used from that point on. If condition is not true, value stored by x is still zero.

The code gets executed by line and any old values are replace instantly.The program allocates a memory location for your static variable from the start depending on the variable type.

You cannot have two different values stored in the same memory allocation for one variable. The value once changed, will be used until a new condition causes it to be changed again or the program is restarted.

Always the newest calculated and assigned value is the only value for that variable. If more than one condition is used within the tick that causes the value of x to be changed several times, the newest value is the only value for x.

Try this example, run at low speed in strategy tester, visual, and you will see that newest value is always the only value.

Starts from x=0 =>then x++ = 1 => then f(x) adds 1 => so if we set x=f(x) => result is 2

Next tick x= 2 => x++ = 3 => f(x) = 4 and then 6 and 8 and......

//+------------------------------------------------------------------+
//|                                                  static test.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

static int x=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(Time[0]==Time[0]) x++;// simple condition to allow x++
   int Calculate=f(x);      // f(x) will increment x by one
   x=Calculate;             // assign new value to x again
   Comment("Static variable X = "+x); //value of x goes up by 2
   Sleep(5000);
  }
//+------------------------------------------------------------------+
int f(int y)
  {
   int z= y;
   int w=z+1;
   return(w);
  }
//+------------------------------------------------------------------+
 
angevoyageur:
That compiles. But it's useless as global variables are already static.


There's a difference, global variables can be initialized different, do not need to be initialized by a constant or constant expression while global static variables are only initialized before the OnInit() and need to be initialized by a constant or a constant expression.

I guess they're more useful as local static variable since they get initialized within the function and keep their value until next call.

 
thrdel:


There's a difference, global variables can be initialized different, do not need to be initialized by a constant or constant expression while global static variables are only initialized before the OnInit() and need to be initialized by a constant or a constant expression.

...

You are wrong. Globals variables and static variables are initialized the same way, only local variables can be initialize with any expression. See documentation.

A global variable is declared outside any function, adding the keyword static doesn't change anything.

 
thrdel:


There's a difference, global variables can be initialized different, do not need to be initialized by a constant or constant expression while global static variables are only initialized before the OnInit() and need to be initialized by a constant or a constant expression.

I guess they're more useful as local static variable since they get initialized within the function and keep their value until next call.

These are Global Variables please use the correct terminology so you do not mess up searches for Users in the future. What you are talking about is scope and variables with global scope or globally declared.
Reason: