First tick detection

 

Hi MQLers,

am I somehow able to detect a tick which starts a new candle / column / whatever? Background: within my indicator I have a piece of code which only cares about finished periods, so instead of having it run with every coming tick I plan to first check if an incoming tick 'falls' into the current period, in which case I'll branch off that code, and if the tick starts a new period, only then I'll run the code starting with the period that have just been closed ...

 

Sure:


At start-section you add:

double PreviousOpen=-99;


Before your code that you only want to calculate once per bar you add before it:


if (PreviousOpen!=Open[0])

{


your code here...


PreviousOpen=Open[0];

}

 
geektrader:

Sure:

At start-section you add:

Very bad idea to use price . . . for many reasons and this Can price != price ?

Also . . .

Please use this to post code . . . it makes it easier to read.



and . . . use Time[1] instead

 
You are right, Time would be better since open could equal the same price on the next bar. Anyhow, I am still using this in one of my EA´s since I really just want to re-calculate the indicator if the open price has changed and with Open[0] I get both of this, detection of a new bar and if open price has changed. Hence on bars that have the same open price as the previous bar there is no need to recalculate it at least for my EA and it works fine in live for months already:) Anyhow, for him just use Time then instead of Open.
 
The open may not have changed, but everything else has. All MAs, the high, low, etc.
Reason: