//+------------------------------------------------------------------+
//| Indicator settings                                               |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Green
#property indicator_color2  Red
#property indicator_width1  3
#property indicator_width2  3
#property indicator_minimum 10
#property indicator_maximum 90

// External input parameters
extern int Lb = 3;                     // Lookback period for moving average
extern int sslMA_method = 0;           // Moving average method
extern int SSL_BarLevel = 50;          // Bar level (between 10-90)

// Alert options
extern bool alertsOn = false;          // Enable/disable alerts
extern bool alertsMessageBox = false;  // Enable/disable message box alerts
extern bool alertsSound = false;       // Enable/disable sound alerts
extern string alertsSoundFile = "TP1M.wav";  // Sound file for alerts
extern bool alertsEmail = false;       // Enable/disable email alerts
extern bool alertsAfterBarClose = false;  // Alerts after bar close

// Buffers for storing indicator values
double sslHup[];                       // Buffer for SSL up signals
double sslHdn[];                       // Buffer for SSL down signals
double hlv[];                          // Buffer for signal values
string IndicatorFileName;              // Stores the indicator file name

//+------------------------------------------------------------------+
//| Initialization function                                           |
//+------------------------------------------------------------------+
int init() {
   // Define indicator buffers and set styles
   IndicatorBuffers(3);  // Define 3 buffers
   SetIndexBuffer(0, sslHup); SetIndexStyle(0, DRAW_ARROW); SetIndexArrow(0, 167); SetIndexLabel(0, "SSLup " + Lb);
   SetIndexBuffer(1, sslHdn); SetIndexStyle(1, DRAW_ARROW); SetIndexArrow(1, 167); SetIndexLabel(1, "SSLdn " + Lb);
   SetIndexBuffer(2, hlv);  // Define signal buffer

   // Set indicator short name and file name
   IndicatorShortName("SSL Bar " + Lb);
   IndicatorFileName = WindowExpertName();

   return(0);
}

//+------------------------------------------------------------------+
//| Deinitialization function                                         |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}

//+------------------------------------------------------------------+
//| Main indicator calculation function                               |
//+------------------------------------------------------------------+
int start() {
   int counted_bars = IndicatorCounted();  // Count the number of bars already processed
   int limit;

   if (counted_bars < 0) return(-1);      // Error handling for counted bars
   if (counted_bars > 0) counted_bars--;  // Decrease counted bars for recalculation
   limit = Bars - counted_bars;           // Define the limit for processing new bars

   // Loop through each bar and calculate the indicator
   for (int i = limit; i >= 0; i--) {
      hlv[i] = hlv[i+1];  // Carry forward the previous signal value

      // Calculate signals based on moving average crossover
      if (Close[i] > iMA(Symbol(), 0, Lb, 0, MODE_SMA, PRICE_HIGH, i+1)) hlv[i] = 1;
      if (Close[i] < iMA(Symbol(), 0, Lb, 0, MODE_SMA, PRICE_LOW, i+1)) hlv[i] = -1;

      // Assign SSL up or down signal based on the hlv value
      if (hlv[i] == -1) {
         sslHdn[i] = SSL_BarLevel; 
         sslHup[i] = EMPTY_VALUE;
      } else {
         sslHdn[i] = EMPTY_VALUE;
         sslHup[i] = SSL_BarLevel;
      }
   }

   // Handle alerts if enabled
   if (alertsOn) {
      int alertBar = 0; 
      if (alertsAfterBarClose) alertBar = 1;  // Alert after bar closes if option is enabled

      // Trigger alert on signal change
      if (hlv[0+alertBar] != hlv[1+alertBar]) {
         if (hlv[0+alertBar] == 1)
            doAlert("Up");
         else
            doAlert("Down");
      }
   }

   return(0);
}

//+------------------------------------------------------------------+
//| Alert handling function                                           |
//+------------------------------------------------------------------+
void doAlert(string doWhat) {
   static string previousAlert = "nothing";  // Store the previous alert state
   static datetime previousTime;             // Store the time of the last alert
   string message;

   // Trigger alert if it's a new signal or time has changed
   if (previousAlert != doWhat || previousTime != Time[0]) {
      previousAlert = doWhat;
      previousTime = Time[0];

      // Construct alert message
      message = StringConcatenate(Symbol(), " at ", Close[0], " SSL Bar: trend changed to ", doWhat);

      // Send alert based on user settings
      if (alertsMessageBox) Alert(message);
      if (alertsEmail) SendMail(StringConcatenate(Symbol(), " SSL "), message);
      if (alertsSound) PlaySound(alertsSoundFile);
   }
}
