거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Telegram에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
Experts

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

조회수:
1254
평가:
(3)
게시됨:
2024.04.04 19:19
이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

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