Quick question

 

question: how do you write an "if" function that triggers code at the END of each bar on the chart (which works independent of chart timeframe)?

 

Welcome!

murky waters:
question: how do you write an "if" function that triggers code at the END of each bar on the chart (which works independent of chart timeframe)?

murky waters,

Welcome to our forum!

Test this code:

bool NewBar() //this function will return true if there's a new bar.

{

static bool first_call = true;

static int start_bar = 0;

if(first_call)

{

start_bar=Bars;

first_call=false;

}

if(Bars == (start_bar+1))

{

first_call=true;

return (true);

}

else

{

return (false);

}

}

int start() //<-- in your start function you may use code like this

{

if (NewBar)

{

//do something

}

return (0);

}
 

Really great job

Hi coderguru,

Your are really doing a gr8 job here . Keep up good works

 
 
 

Great Code

Thanks Codersguru

You help me greatly

 

Another Way

I prefer to use this solution for finding new Bars

bool NewBar()

{

static datetime LastTime;

if (LastTime != Time[0])

{

LastTime = Time[0];

return(true);

}

return(false);

}

it's seems to be shorter

 
intelligent_14:
I prefer to use this solution for finding new Bars
bool NewBar()

{

static datetime LastTime;

if (LastTime != Time[0])

{

LastTime = Time[0];

return(true);

}

return(false);

}
it's seems to be shorter

Yes this is good one!

 
codersguru:
Yes this is good one!

thank u codersguru, i had read all of your MQL4 tutorial, its very good, i finished it 2 weeks ago

thanks a lot

Reason: