This code block detects a New Bar or a New Candle when it has received.Β
the basic principle of the codes is very simple. First the code stores the Time of the previous bar / Candle. (Then add 60 seconds (equals to 1 min. you can add time as you want) to the Time of the previous bar which give the closing time value of the Current Bar / Candle.Β
Once,
Current Time = closingΒ time value of the Current Bar / Candle.Β That means a new has received / the current bar has closed..
in this code the flag (the bool type variable ‘NewBarRecived’)Β avoids the multiple calling of this code block. which means this code block execute only once per bar / candle. The Comment(); and the playsound(“ok.wav”); isΒ used to check the accuracy of the code block. You can remove it if you want.Β
The flag is reset to false once the current Time is above the closing time of the current candle to check next bar arrival. (Watch comments to see).
//+------------------------------------------------------------------+ //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β New Bar Detect.mq5 | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β by H A T Lakmal | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ bool NewBarRecived = false; // Falg for control. //+------------------------------------------------------------------+ //| Expert initialization functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ int OnInit() Β Β { //--- create timer Β Β EventSetTimer(60); //--- Β Β return(INIT_SUCCEEDED); Β Β } //+------------------------------------------------------------------+ //| Expert deinitialization functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ void OnDeinit(const int reason) Β Β { //--- destroy timer Β Β EventKillTimer(); Β Β } //+------------------------------------------------------------------+ //| Expert tick functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ void OnTick() Β Β { Β Β datetime TimePreviousBar = iTime(_Symbol,PERIOD_M1,1); Β Β datetime TimeCurrentClose = TimePreviousBar + 60; // Closing Time of the current bar. Β Β datetime Time_Current = TimeCurrent(); Β Β if(Time_Current == TimeCurrentClose && NewBarRecived == false) Β Β Β Β { Β Β Β Β Β Β PlaySound("ok.wav");Β Β // For the statement work of not. Β Β Β Β Β Β NewBarRecived = true; // Update the flag to avoide multipleΒ Β calls. Β Β Β Β Β Β // Your Code goes here ----- (Do Something) Β Β Β Β } Β Β else Β Β Β Β Β Β if(Time_Current > TimeCurrentClose) Β Β Β Β Β Β Β Β { Β Β Β Β Β Β Β Β NewBarRecived = false; // Rest the flag for next bar open. Β Β Β Β Β Β Β Β // Your Code goes here ----- (Do Something) Β Β Β Β Β Β Β Β } Β Β Comment("\n" +Β Β "\n" +Β Β "Time Current Bar -: " + TimeToString(TimePreviousBar,TIME_DATE|TIME_MINUTES|TIME_SECONDS) + Β Β Β Β Β Β Β Β Β Β "\n" + "Time Current Close -: " +TimeToString(TimeCurrentClose,TIME_DATE|TIME_MINUTES|TIME_SECONDS) + Β Β Β Β Β Β Β Β Β Β "\n" + "Time Current -: " + TimeToString(Time_Current,TIME_DATE|TIME_MINUTES|TIME_SECONDS) + "\n" +"\n" + "A New Bar Recived -: " + NewBarRecived); // For check calculations Β Β } //+------------------------------------------------------------------+ //| Timer functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ void OnTimer() Β Β { //--- Β Β } //+------------------------------------------------------------------+ //| Trade functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ void OnTrade() Β Β { //--- Β Β } //+------------------------------------------------------------------+ //| ChartEvent functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ void OnChartEvent(const int id, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β const long &lparam, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β const double &dparam, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β const string &sparam) Β Β { //--- Β Β } //+------------------------------------------------------------------+
Β