//+------------------------------------------------------------------+
//|                                       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 2
#property indicator_plots   2

//--- 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

//--- 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 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

//--- 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);

   max_period = MathMax(InpEnv1Period, InpEnv2Period);

   handle_env1 = iEnvelopes(_Symbol, _Period, InpEnv1Period, 0, InpEnv1Method, InpEnv1Price, InpEnv1Dev);
   handle_env2 = iEnvelopes(_Symbol, _Period, InpEnv2Period, 0, InpEnv2Method, InpEnv2Price, InpEnv2Dev);

   if(handle_env1 == INVALID_HANDLE || handle_env2 == INVALID_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. 