Having indicator being calculated at the end of a bar

 

Hello,

I'm a newbie in MQL4... I managed to code some up and down arrows on my graphs with buy & sell signals... Unfortunately, when it runs live, the signal are wrong because they are calculated "live" while i need them to be calculated on the bar 1 as soon as bar 0 starts. So on the last bar as soon as a new bar appears (= on close of the bar)

It cannot be a function of time because i use constan bar range chart, so a new bar isn't a function of time.

Thanks alot for your advices.

Best,
Gregory

 
Code it like EVERY other indicator
for(int shift = Bars - 1 - IndicatorCounted(); shift >= 0; shift--){
   buffer[shift] = whatever
When a new bar starts buffer[1] will be the last calculated value.
 

Thanks WHRoeder. I'm sorry but i'm not sure to understand... Could you elaborate a little bit more ? In fact i guess there should be a If statement somewhere to make the calculations when the new bar is there?

Thanks
Gregory

 

By the time a new bar appears the close price of the previous bar is not a live price so any sell or buy signals would be invalid.

You could do it on the Open price, which is the first tick of the new bar. it will be 1 pip higher or 1 pip lower then the close price of the previous bar.

if(Bid==Open[0]] ... 

If its not critical to have a signal on every bar you could do ..

if(Bid==Close[1])

As you will notice most of the time, at some point after a new bar starts, usually very soon after it starts, the price will be equal to the Close price of the previous bar, although there are occasions when that doesnt happen.

Reason: