Home System Trading Post

Effortlessly Detect New Bars in MetaTrader 5 with This Simple Code

Attachments
49018.zip (977 bytes, Download 0 times)

Hey traders! Today, I’m excited to share a neat little code snippet that helps you detect a New Bar or New Candle in MetaTrader 5. It’s pretty straightforward, so let’s dive in!

The core idea behind this code is simple yet effective. First, it stores the Time of the Previous Bar/Candle. Then, it adds 60 seconds (equivalent to 1 minute, but feel free to adjust the time as needed) to the time of the previous bar to get the Closing Time Value of the Current Bar/Candle.

Once the current time matches the closing time of the current bar, it means a new bar has arrived.

The flag (a boolean variable called NewBarReceived) prevents this code block from being triggered multiple times. Essentially, the code executes only once per bar/candle. The Comment() function and the PlaySound("ok.wav") are included to verify the accuracy of the code—you can remove them if you prefer.

After the current time surpasses the closing time of the current candle, the flag resets to false, preparing to check for the next bar's arrival (take a look at the comments for more details).

//+------------------------------------------------------------------+
//|                                                 New Bar Detect.mq5 |
//|                                                   by H A T Lakmal |
//|                                            https://t.me/Lakmal846 |
//+------------------------------------------------------------------+

bool NewBarReceived = false; // Flag 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 && NewBarReceived == false)
     {
      PlaySound("ok.wav");   // For the statement work of not.
      NewBarReceived = true; // Update the flag to avoid multiple  calls.

      // Your Code goes here ----- (Do Something)

     }
   else
      if(Time_Current > TimeCurrentClose)
        {
         NewBarReceived = false; // Reset 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 Received -: " + NewBarReceived); 
           // 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)
  {
//---

  }
//+------------------------------------------------------------------+

 

Related Posts

Comments (0)