How to use a static value in OnTick function

Hello everyone :slight_smile:

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.

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

Many thanks to you. …

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

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

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;
   : 

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;

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.

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