
//+------------------------------------------------------------------+
//|                                                     ATR_HiLo_Channel.mq4 |
//|                        Copyright 2025, MetaQuotes Software Corp. |
//|                                       https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrRed
#property indicator_color2 clrBlue
#property indicator_color3 clrGreen
#property strict

//--- input parameters
input int    AtrPeriod   = 14;     // ATR周期
input double AtrMultiplier = 2.0;  // ATR乘数
input int    HiLoPeriod  = 9;      // HiLo周期

//--- indicator buffers
double UpperBuffer[];
double LowerBuffer[];
double MiddleBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- indicator buffers mapping
   SetIndexBuffer(0, UpperBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, LowerBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, MiddleBuffer, INDICATOR_DATA);

   //--- set indicator styles
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);

   //--- set indicator labels
   IndicatorShortName("ATR HiLo Channel");
   SetIndexLabel(0, "Upper Band");
   SetIndexLabel(1, "Lower Band");
   SetIndexLabel(2, "Middle Line");

   //--- set accuracy
   IndicatorDigits(_Digits);

   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[])
  {
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1; 
   
   //
   //
   //
   
   for (; i>=0 && !_StopFlag; i--)
   {
      double atr     = iATR(NULL, 0, AtrPeriod, i);
      double highest = high[ArrayMaximum(high,HiLoPeriod,i)];
      double lowest  =  low[ArrayMinimum( low,HiLoPeriod,i)];

      MiddleBuffer[i] = (highest+lowest)/2;
      UpperBuffer[i]  = MiddleBuffer[i] + (AtrMultiplier * atr);
      LowerBuffer[i]  = MiddleBuffer[i] - (AtrMultiplier * atr);
   }
return(rates_total);
}
