//+------------------------------------------------------------------+
//|                                       DualEnvelopeIndependent.mq5|
//+------------------------------------------------------------------+
#property copyright "Amarnath Kondiyan Mohan"
#property link      "https://www.linkedin.com/in/amarnath-kondiyan-mohan-546293a/"
#property version   "2.00"
#property description "Volatility Breakout Indicator with Custom GUI."
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   6

//--- Plot Buy Signal
#property indicator_label1  "Buy Signal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- Plot Sell Signal
#property indicator_label2  "Sell Signal"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed  
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

//--- the Upper plot1
#property indicator_label3  "Upper1"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- the Lower plot1
#property indicator_label4  "Lower1"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

//--- the Upper plot2
#property indicator_label5  "Upper2"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrYellow
#property indicator_style5  STYLE_DASH
#property indicator_width5  1
//--- the Lower plot2
#property indicator_label6  "Lower2"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrOrange
#property indicator_style6  STYLE_DASH
#property indicator_width6  1

//--- Input Groups
input group "=== Envelope 1 (Trigger) ==="
input int                InpEnv1Period = 14;         // Period
input ENUM_MA_METHOD     InpEnv1Method = MODE_SMA;   // Method
input ENUM_APPLIED_PRICE InpEnv1Price  = PRICE_CLOSE;// Applied Price
input double             InpEnv1Dev    = 0.1;        // Deviation
input bool               InpShowEnv1   = true;       // Show Envelope 1 trigger

input group "=== Envelope 2 (Target) ==="
input int                InpEnv2Period = 21;         // Period
input ENUM_MA_METHOD     InpEnv2Method = MODE_EMA;   // Method
input ENUM_APPLIED_PRICE InpEnv2Price  = PRICE_CLOSE;// Applied Price
input double             InpEnv2Dev    = 0.25;       // Deviation
input bool               InpShowEnv2   = true;       // Show Envelope 2 target

//--- Indicator buffers
double         BuyBuffer[];
double         SellBuffer[];

//--- Indicator handles
int            handle_env1;
int            handle_env2;
//--- Indicator arrays
double         env1_upper[], env1_lower[];
double         env2_upper[], env2_lower[];

//--- Global variables
int            max_period;

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, BuyBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_ARROW, 233); 
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,20);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   ArrayInitialize(BuyBuffer, EMPTY_VALUE);

   SetIndexBuffer(1, SellBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(1, PLOT_ARROW, 234); 
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-20);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   ArrayInitialize(SellBuffer, EMPTY_VALUE);

   SetIndexBuffer(2,env1_upper,INDICATOR_DATA);
   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,InpShowEnv1?DRAW_LINE:DRAW_NONE);
   SetIndexBuffer(3,env1_lower,INDICATOR_DATA);
   PlotIndexSetInteger(3,PLOT_DRAW_TYPE,InpShowEnv1?DRAW_LINE:DRAW_NONE);
   SetIndexBuffer(4,env2_upper,INDICATOR_DATA);
   PlotIndexSetInteger(4,PLOT_DRAW_TYPE,InpShowEnv2?DRAW_LINE:DRAW_NONE);
   SetIndexBuffer(5,env2_lower,INDICATOR_DATA);
   PlotIndexSetInteger(5,PLOT_DRAW_TYPE,InpShowEnv2?DRAW_LINE:DRAW_NONE);

   max_period = MathMax(InpEnv1Period, InpEnv2Period);

   if ( (handle_env1 = iEnvelopes(_Symbol, _Period, InpEnv1Period, 0, InpEnv1Method, InpEnv1Price, InpEnv1Dev)) == INVALID_HANDLE ) {
      Print("❌ Failed to open envelope 1 handle");
      return(INIT_FAILED);
   }
   if ( (handle_env2 = iEnvelopes(_Symbol, _Period, InpEnv2Period, 0, InpEnv2Method, InpEnv2Price, InpEnv2Dev)) == INVALID_HANDLE ) {
      Print("❌ Failed to open envelope 2 handle");
      return(INIT_FAILED);
   }

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Deinitialization                                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   IndicatorRelease(handle_env1); 
   IndicatorRelease(handle_env2); 
   ChartRedraw(0);
}

//+------------------------------------------------------------------+
//| Main Iteration Loop                                              |
//+------------------------------------------------------------------+
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(rates_total < max_period)
      return(0);

   int start = (prev_calculated > max_period) ? prev_calculated - 1 : max_period;

   if(CopyBuffer(handle_env1, 0, 0, rates_total, env1_upper) <= 0) return 0;
   if(CopyBuffer(handle_env1, 1, 0, rates_total, env1_lower) <= 0) return 0;
   if(CopyBuffer(handle_env2, 0, 0, rates_total, env2_upper) <= 0) return 0;
   if(CopyBuffer(handle_env2, 1, 0, rates_total, env2_lower) <= 0) return 0;

   for(int i = start; i < rates_total - 1; i++) {
      if(env1_upper[i] == EMPTY_VALUE || env1_upper[i-1] == EMPTY_VALUE)
         continue;

      BuyBuffer[i]   = close[i] > env1_upper[i] && close[i-1] <= env1_upper[i-1] ? low[i]-10*_Point  : EMPTY_VALUE;
      SellBuffer[i]  = close[i] < env1_lower[i] && close[i-1] >= env1_lower[i-1] ? high[i]+10*_Point : EMPTY_VALUE;
   }
   // [Render UI Logic]
   return(rates_total);
}

// Helper functions (DrawBand, DrawUILabel) should remain below the main loop. 