//+------------------------------------------------------------------+
//|                                                   S4T_Alerts.mq4 |
//|                                                 soft4traders.com |
//|                                             www.soft4traders.com |
//+------------------------------------------------------------------+
#property copyright "soft4traders.com"
#property link      "www.soft4traders.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#property description "This indicator informs the user that the closing time of the current bar is approaching."
#property description " "
#property description "Данный индикатор сообщает пользователю о приближении времени закрытия текущего бара."
// VARIABLES
input int AlertNSecondsBeforeNewBar=120; //Alert in N Seconds Before New Bar
input int NAlertsPerSignal=5; //N Alerts On Each Signal
input bool   DisplayComment=true; //Display Comment
int nTimesAlerted;

MqlDateTime sTimeCur,sTimeLocal;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   EventSetTimer(1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(DisplayComment)
     {
      Comment("");
     }
   EventKillTimer();  
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   if(AlertNSecondsBeforeNewBar>0)
     {
      TimeCurrent(sTimeCur);
      TimeLocal(sTimeLocal);
      sTimeCur.min=sTimeLocal.min;
      sTimeCur.sec=sTimeLocal.sec;
      datetime curTime=StructToTime(sTimeCur);

      datetime curTimeStartAlert=Time[0]+PeriodSeconds()-AlertNSecondsBeforeNewBar;

      if(curTime>=curTimeStartAlert && 
         NAlertsPerSignal>nTimesAlerted)
        {
         Alert(_Symbol," Period: ",_Period," m. THE NEW BAR IS APPROACHING!!!");
         nTimesAlerted++;
        }
      if(curTime<curTimeStartAlert){nTimesAlerted=0;}

      if(DisplayComment)
        {
         Comment("Current Time ",curTime
                 ,"\nCurrent Time Start Alert: ",curTimeStartAlert);
        }
     }
  }
//+------------------------------------------------------------------+
