Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Simple code for detecting received "new bar or candle" - MetaTrader 5 Expert - MT4/MT5 Resource

author EAcpu | 6 reads | 0 comments |

This code block detects a new bar or a new candle when it is received.

The basic principle of the code is very simple. First, the code stores the time of the previous bar/candle . (Then add 60 seconds (equal to 1 minute you can add time as needed) to the time of the previous column, this gives the closing time value for the current bar/candle.

Current time = closing time value of the current bar/candle. This means that new ones have been received/current bars have been closed..

In this code, the flag (bool type variable ' new bar received') avoids multiple calls to this code block. This means that this code block is executed only once per bar/candle. This comment(); and playSound("ok.wav"); are used to check the accuracy of the code block. You can delete it if needed.

Once the current time is higher than the closing time of the current candle, the flag will be reset to false to check the arrival of the next bar. (Read the comments to see).

//+------------------------------------------------------------------+
//| New column detection.mq5 |
//| Author: H.A.T. Lakmal|
//| https://t.me/Lakmal846 |
//+------------------------------------------------------------------+

boolean new column received = erroneous ; // control flag.

//+------------------------------------------------------------------+
//|Expert initialization function |
//+------------------------------------------------------------------+
Integer initialization ()
  { //--- Create a timer event to set the timer ( 60 ); //--- Return ( initialization successful );
  } //+------------------------------------------------------------------+
//|Expert to initialize function |
//+------------------------------------------------------------------+
Blank solution initialization ( constant integer reason)
  { //--- Destroy timer event to terminate timer ();

  } //+------------------------------------------------------------------+
//|Expert check function |
//+------------------------------------------------------------------+
Blank check ()
  {
date time previous column time = love time ( _symbol , PERIOD_M1 , 1 );
Date and time current closing time = previous bar time + 60 ; // The closing time of the current bar. datetimetime_current = timecurrent ();

if (Time_Current == TimeCurrentClose && NewBarRecived == false )
    {
play sound ( "ok.wav" ); // For declarations that don't work.      new column received = true ; // Update flag to avoid multiple calls. // Your code goes here ----- (do something) }
Else if (current time > current closing time)
        {
        New bar received = wrong ; // Reserve flag for the next opening bar. // Your code goes here ----- (do something) }

Comment ( "\n" + "\n" + "Current Column Time -:" + String Time (Previous Column Time, TIME_DATE | TIME_MINUTES | TIME_SECONDS ) +
"\n" + "Current Close Time -:" + String time (time currently closed, TIME_DATE | TIME_MINUTES | TIME_SECONDS ) +
"\n" + "current time -:" + string time (time_current, TIME_DATE | TIME_MINUTES | TIME_SECONDS ) + "\n" + "\n" + "received a new bar -:" + new bar received); // Used for checking calculations } //+--------------------------------------------------------------------------------+
//|Timer function |
//+------------------------------------------------------------------+
blank timer ()
  { //--- }ent">//+------------------------------------------------------------------+ //|Trade Function | //+------------------------------------------------------------------+ Blank Online Trading () { //--- } //+------------------------------------------------------------------+ //| ChartEvent Function | //+------------------------------------------------------------------+ Blank Chart Event ( const integer ID, constant long &l parameter, constant double &d parameter, constant string &s parameter) { //--- } //+----------------------------------------------------------------+


Attachment download

📎 new_candel_-_bar_detector.mq5 (3.39 KB)

Source: MQL5 #49018

Simple code for detecting received "new bar or candle" - MetaTrader 5 Expert Pre-Using Tips

This page has been supplemented with structured instructions, focusing on the purpose of EA source code, MQL4/MQL5 platform compatibility, testing process and risk control. Before downloading or deploying, it is recommended to first determine whether it is consistent with your own variety, cycle and account environment.

Suitable for who to use

  • Users who want to quickly screen EA source code and are willing to do simulation disk verification first.
  • People who need to compare MT4/MT5 EAs, indicators, scripts or source code projects.
  • Traders who wish to record backtests, parameters and risk boundaries before placing a trade.

Testing and risk control suggestions

  • First verify the default parameters in the strategy tester or demo account, and then adjust the symbol, period, spread and trading period one by one.
  • Scalping, grid, martin, and high-frequency strategies should focus on observing maximum drawdowns, consecutive losses, slippage, and trading frequency.
  • It is not recommended to run any document directly on the real disk. At least observe the simulated disk performance for 2-4 weeks first.

FAQ

Can MT4 and MT5 be used interchangeably?
Usually cannot be used directly. EX4/MQ4 corresponds to MT4, EX5/MQ5 corresponds to MT5, and the source code needs to be compiled in the corresponding MetaEditor.

Is this resource guaranteed to be profitable?
cannot. EAs, indicators and scripts can only be used as trading tools, and the results depend on parameters, market environment, spreads, slippage and risk control.

What else can I continue to watch?
Forex EA download , foreign exchange indicator download , EA evaluation , MQL5 CodeBase .

English Search Notes

This page is also optimized for English search intent around Simple code for detecting received "new bar or candle" - MetaTrader 5 Expert . Related search terms include: MQL4 source code, MQL5 source code. Test any Forex EA, Expert Advisor, MT4/MT5 indicator, script or MQL source code with historical data and a demo account before live trading.

Verification code Refresh