//+------------------------------------------------------------------+
//|                                  SuperTrend_MultiTimeframe_MT4.mq4|
//|                    Copyright 2026, TraderSentiments Quant Team   |
//|                                    https://tradersentiments.com  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, TraderSentiments"
#property link      "https://tradersentiments.com/indicators/mt4/trend/supertrend-indicator"
#property version   "2.20"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  clrLimeGreen
#property indicator_color2  clrCrimson
#property indicator_width1  2
#property indicator_width2  2

input int      InpATRPeriod        = 10;          // ATR Period
input double   InpMultiplier       = 3.0;         // ATR Multiplier Factor
input bool     InpAlertOnTrendChange= true;       // Popup Alert on Trend Reversal
input bool     InpSendEmailNotification= false;   // Send Email on Trend Switch

double UpTrendBuffer[];
double DownTrendBuffer[];

int OnInit()
  {
   SetIndexBuffer(0, UpTrendBuffer);
   SetIndexStyle(0, DRAW_LINE);

   SetIndexBuffer(1, DownTrendBuffer);
   SetIndexStyle(1, DRAW_LINE);

   IndicatorShortName("SuperTrend MT4 (" + IntegerToString(InpATRPeriod) + "," + DoubleToStr(InpMultiplier, 1) + ")");
   return(INIT_SUCCEEDED);
  }

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[])
  {
   int limit = rates_total - prev_calculated;
   if(prev_calculated == 0) limit = rates_total - InpATRPeriod - 1;

   for(int i = 0; i < limit; i++)
     {
      double atr = iATR(NULL, 0, InpATRPeriod, i);
      double median = (high[i] + low[i]) / 2.0;
      double upperBand = median + (InpMultiplier * atr);
      double lowerBand = median - (InpMultiplier * atr);

      if(close[i] > median)
        {
         UpTrendBuffer[i] = lowerBand;
         DownTrendBuffer[i] = EMPTY_VALUE;
        }
      else
        {
         DownTrendBuffer[i] = upperBand;
         UpTrendBuffer[i] = EMPTY_VALUE;
        }
     }

   return(rates_total);
  }
