//+------------------------------------------------------------------+
//|                                             NewBarAlert_BTEv.mq4 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "2.00"
#property strict
#property indicator_chart_window

input ENUM_TIMEFRAMES   Alert_Tf             = PERIOD_CURRENT;
input string AlertSounds = "alert.wav,alert2.wav,alert3.wav,alert4.wav,alert5.wav";

string Sounds[];
int    SoundsCount = 0;
bool   rnd_seeded  = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   int n = StringSplit(AlertSounds, ',', Sounds);

   SoundsCount = 0;
   for(int i = 0; i < n; i++)
   {
      string s = StringTrimLeft(StringTrimRight(Sounds[i]));
      if(s != "")
         Sounds[SoundsCount++] = s;
   }

   ArrayResize(Sounds, SoundsCount);

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
  {
//---
                if(IsNewBar(Alert_Tf) && SoundsCount > 0)
                 {
                 if(!rnd_seeded)
                  {
                  MathSrand(GetTickCount());
                  rnd_seeded = true;
                  }

                 int r = MathRand() % SoundsCount;
                 PlaySound(Sounds[r]);
                 }               
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
bool IsNewBar(ENUM_TIMEFRAMES per)
{ 
  static datetime Trend_Candle_prevTime1 = -1;
  
  if(Trend_Candle_prevTime1 != iTime(_Symbol,per,6))
  { 
   Trend_Candle_prevTime1 = iTime(_Symbol,per,6); 
       
   return(true);  
  } 

  return(false); 
}
//+------------------------------------------------------------------+

