Executing function only once per current bar

 

Hello everyone,

I am a beginner in coding and with the MT5 language.

I'm trying to develop a slightly evolved EA. I already managed to perform basic functions but now I am stuck.

My problems are:

In the OnTick function ()

I check the position opening condition four times, at defined times, during the construction of a new bar, it works.

The problem is that when a condition is valid and an order is passed, the tick following a new identical order is passed until there is no more money available.

I have tried many solutions but none works.

Here is my code :

If (condition1==true) OpenBuyPosition();

If (condition2==true) OpenBuyPosition();

If (condition3==true) OpenBuyPosition();

If (condition4==true) OpenBuyPosition();

If (condition5==true) OpenSellPosition();

If (condition6==true) OpenSellPosition();

If (condition7==true) OpenSellPosition();

If (condition8==true) OpenSellPosition();

All the condition are stored in global variables and reinitialzed to false jut before the closing of current bar. And this works.

When any condition is true and the function OpenBuyPosition() is executed, it's executed again and again and i don't want to open several identical positions in the same bar in construction.The best if it is possible to skip other indentical conditions as soon as one is true and the function is executed.

For example if a Buy position is taken, i want to skip all other conditions to open a Buy position until closing of current bar.

Thank you for the help.

 
chpeller:

Hello everyone,

I am a beginner in coding and with the MT5 language.

I'm trying to develop a slightly evolved EA. I already managed to perform basic functions but now I am stuck.

My problems are:

In the OnTick function ()

I check the position opening condition four times, at defined times, during the construction of a new bar, it works.

The problem is that when a condition is valid and an order is passed, the tick following a new identical order is passed until there is no more money available.

I have tried many solutions but none works.

Here is my code :

If (condition1==true) OpenBuyPosition();

If (condition2==true) OpenBuyPosition();

If (condition3==true) OpenBuyPosition();

If (condition4==true) OpenBuyPosition();

If (condition5==true) OpenSellPosition();

If (condition6==true) OpenSellPosition();

If (condition7==true) OpenSellPosition();

If (condition8==true) OpenSellPosition();

All the condition are stored in global variables and reinitialzed to false jut before the closing of current bar. And this works.

When any condition is true and the function OpenBuyPosition() is executed, it's executed again and again and i don't want to open several identical positions in the same bar in construction.The best if it is possible to skip other indentical conditions as soon as one is true and the function is executed.

For example if a Buy position is taken, i want to skip all other conditions to open a Buy position until closing of current bar.

Thank you for the help.

The simplest solution is to set a variable to check the time.  Example:

datetime lastBar;

int OnInit(void)
{
  lastBar = iTime(NULL,PERIOD_CURRENT,1);
}

void OnTick()
{
 if(lastBar<iTime(NULL,PERIOD_CURRENT,0)
 {
  if (condition1==true) 
  {
   OpenBuyPosition();
   lastBar = iTime(NULL,PERIOD_CURRENT,0);
  }
  etc.
  etc.
  etc.
 }
}
 

Thank You for the answer.


Finally I found another solution based on bool variables and it works.

Reason: