A Simple Question About Variables

 

Hello everyone :) 

All I want to do is giving value to "x". ( x is a variable)

Then, when value of x is changed in OnTick function. That new value(given in Ontick) should remain untill its changed in Ontick again. 

double x= 10

void OnTick()

{
x= High[1]
}

That "10" value should called once at the start. But if I write the code like above (x is glabal) the first value which is "10" comes back.

 
apleasewouldbenice boom:

I should give a value to "x" for once.

https://docs.mql4.com/basis/variables/static

Static Variables - Variables - Language Basics - MQL4 Reference
Static Variables - Variables - Language Basics - MQL4 Reference
  • docs.mql4.com
A static variable can be initialized by a constant or constant expression corresponding to its type, unlike a simple local variable, which can be initialized by any expression. Static variables exist from the moment of program execution and are initialized only once after the program is loaded. If the initial values are not specified, variables...
 
Many thanks to you. 
 
apleasewouldbenice boom: I want to give a value to a variable for once. Then I should change its value in Ontick function. But the problem is if I give a value to this variable as 'global'. It's global value comes back. I don't want this and I cant give  it a value in OnStart or OnInit fucntions either.
  1. Globally declared and static variables keep their value between calls. If you don't want that set the value at the top of OnTick.

  2. There is only one OnTick (for EAs,) OnStart (for scripts,) or OnCalculate (for indicators.) Do not use more than one.

  3. Of course you can give it a value in OnStart (if and only if it is a script.) Just do it:
    int OnTick(){
       X x=0.0;
       :

  4. If you want to reset variables on chart change (symbol/TF) remember the change and do it.
    bool isFirstTick;
    int OnInit(){ isFirstTick=true; ... }
    int OnTick(){
       static X x;
       if(isFirstTick){ isFirstTick=false; x=0.0; ... }
       :
       x=30;

  5. Your post is almost unintelligible. Show your code and state your problem.

 
whroeder1:
  1. Globally declared and static variables keep their value between calls. If you don't want that set the value at the top of OnTick.

  2. There is only one OnTick (for EAs,) OnStart (for scripts,) or OnCalculate (for indicators.) Do not use more than one.

  3. Of course you can give it a value in OnStart (if and only if it is a script.) Just do it:

  4. If you want to reset variables on chart change (symbol/TF) remember the change and do it.

  5. Your post is almost unintelligible. Show your code and state your problem.

Sorry for bad problem description. All I want to do is giving value to "x". ( x is a variable)

Then, when value of x is changed in OnTick function. That new value(given in Ontick) should remain untill its changed in Ontick again. 

double x= 10

void OnTick()

{
x= High[1]
}

That "10" value should called once at the start. 

 
apleasewouldbenice boom: Sorry for bad problem description.

All I want to do is giving value to "x". ( x is a variable)

Then, when value of x is changed in OnTick function. That new value(given in Ontick) should remain untill its changed in Ontick again. 

That "10" value should called once at the start. 

That is exactly what your code does (assuming you fix your typos.)

Reason: