how to get signal when lattest bar is just opened?

 

Hi, All

I need an indicator that calculate the signal when the latest bar is just opened. For example for a macd crossing in a M5 period, if we don't calculate after the bar is closed it will change to up/down very often within a M5 bar. If my EA is based on the crossing signal, it will open buy and close it immediatly and then open sell. This will be very bad.

Is there any good way to solve this issue?

I have tried to do something like following:

datatime pretime;

init()

{

pretime=Time[0];

}

start()

{

if(pretime!=Time[0])

{

......macd crossing checking....

pretime=Time[0];

}

but if i invoke this indicator in my EA, it doesn't work. It seems that it still return the M1 result rather than M5. For each ticket, it still return signal when bar is not closed. Is there anything need to be synchronized between indicator and EA? Thanks a lot.

}

BR,

Linyi

 
Why nobody answer my question? Anybody encountered the same problem before? Thanks!
 

Take your crossing signal from bar 1 and 2, not bar 0 and 1

 
phy wrote >>

Take your crossing signal from bar 1 and 2, not bar 0 and 1

Hi, phy

Thank you very much for your reply.

Is this solution the best one? Will the result be accurate? Can this solution be applied to others, such as MA, Volume, etc.?

Do you think that the checking if(pretime!=Time[0]) is needed?

BR,

Linyi

 

The code you show looks fine.

Reason: