Watch how to download trading robots for free
Find us on Facebook!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

Simple Code for Detect A "New Bar or New Candle " Received - expert for MetaTrader 5

Views:
1133
Rating:
(3)
Published:
2024.04.04 19:19
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

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 |
//|                                           https://t.me/Lakmal846 |
//+------------------------------------------------------------------+

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)
  {
//---

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

 

Logarithmic Moving Average Logarithmic Moving Average

Logarithmic Moving Average continuously calculates the logarithmic mean of highest price and lowest price within a period.

Harmonic Moving Average Harmonic Moving Average

MQL5 version of harmonic moving average

Code Block for "Trailing Stop" based on current market price. (Ask / Bid) Code Block for "Trailing Stop" based on current market price. (Ask / Bid)

This code block loops through all opened position and do trailing based on Ask and Bid prices.

Dominant Candle Dominant Candle

Dominant Candle is a a two candlestick set where the wicks intersect each other but body of the candles are either gapped up, gapped down or equal