Make function wait until condition

 

Hi everyone, I'm quite new to mql4 but I hope my thinking still makes sense. 

In the start function I create a trendline that I then want to check the price for for every coming bar. So far i've created a function outside of the start function that checks the price of it, but I am not sure how to make it run once per bar? Just creating a while(true) loop feels horribly ineffective. Hence my question: is there any way to make mt4 wait until a certain condition is true, such as a new bar being created? Once again: I am not inside of the start-function and so my function won't update once per tick.


Take care

 

In OnTick, check for a new bar. If not return

It's a new bar call your function to check your price. If not your condition return.

It's your condition at the start of a new bar, do it.

 

Thank you for your reply! 

The problem is that I want to call the function each time I make a new trendline, and then run it for everyone of these trendlines each time a new bar is created. So basically I will have the function running multiple instances simultaneously. With your idea I will have to create a vector with all of my trendlines, and add/delete trendlines from it, that seems more difficult. 

 
Krossaren96:

Thank you for your reply! 

The problem is that I want to call the function each time I make a new trendline, and then run it for everyone of these trendlines each time a new bar is created. So basically I will have the function running multiple instances simultaneously. With your idea I will have to create a vector with all of my trendlines, and add/delete trendlines from it, that seems more difficult. 

Check for a new trendline in OnChartEvent()

 
Krossaren96:

Hi everyone, I'm quite new to mql4 but I hope my thinking still makes sense. 

In the start function I create a trendline that I then want to check the price for for every coming bar. So far i've created a function outside of the start function that checks the price of it, but I am not sure how to make it run once per bar? Just creating a while(true) loop feels horribly ineffective. Hence my question: is there any way to make mt4 wait until a certain condition is true, such as a new bar being created? Once again: I am not inside of the start-function and so my function won't update once per tick.

Take care

MQL4
datetime newBar = 0;
void OnTick(void)   
{
     if( newBar != Time[0] )
     {
          newBar = Time[0];
          // work
     }
}
MQL4/5
datetime newBar = 0;
void OnTick(void)   
{
     datetime time_bar[];
     if( CopyTime(_Symbol, 0, 0, 1, time_bar) == 1 )
          if( newBar != time_bar[0] )
          {
               newBar = time_bar[0];
               // work
          }
}