Create an MT5 indicator (code).

Perfect ✅ — let’s create a non-repainting Three Bands Forex Trading Indicator for MT5.
It will:

  • Draw Upper, Middle, and Lower bands
  • Generate Buy/Sell signals when price touches or crosses the bands
  • Work on all timeframes and all pairs
  • Include alerts (visual + sound)
  • Use ATR-based smoothing to reduce noise

🧩 MQL5 Indicator Code

You can copy–paste this into MetaEditor → New → Indicator → Paste → Save → Compile.

//+------------------------------------------------------------------+
//|                                                ThreeBands.mq5    |
//|                                         © 2025 ForexWebStore     |
//|                 Non-Repainting Three Bands Trading Indicator     |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   3
#property indicator_separate_window false

//--- plot definitions
#property indicator_label1 "Upper Band"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2 "Middle Band"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3 "Lower Band"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrLime
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

//--- input parameters
input int      BandPeriod     = 20;         // Period for MA/ATR
input double   Multiplier     = 2.0;        // Band width multiplier
input ENUM_MA_METHOD MaMethod = MODE_EMA;   // Moving Average type
input ENUM_APPLIED_PRICE PriceType = PRICE_CLOSE; // Applied price
input bool     EnableAlerts   = true;       // Show alerts?

//--- buffers
double UpperBuffer[];
double MiddleBuffer[];
double LowerBuffer[];
double BuySignal[];
double SellSignal[];

//+------------------------------------------------------------------+
//| Custom indicator initialization                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,MiddleBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,LowerBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,BuySignal,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,SellSignal,INDICATOR_CALCULATIONS);

   IndicatorSetString(INDICATOR_SHORTNAME,"Three Bands (Non-Repaint)");
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration                                       |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   if(rates_total < BandPeriod) return(0);

   int start = prev_calculated > BandPeriod ? prev_calculated - 1 : BandPeriod;

   for(int i=start; i<rates_total; i++)
     {
      double ma = iMA(NULL,0,BandPeriod,0,MaMethod,PriceType,i);
      double atr = iATR(NULL,0,BandPeriod,i);

      MiddleBuffer[i] = ma;
      UpperBuffer[i]  = ma + Multiplier * atr;
      LowerBuffer[i]  = ma - Multiplier * atr;

      BuySignal[i]  = 0.0;
      SellSignal[i] = 0.0;

      //--- Generate Buy/Sell signals
      if(Close[i] < LowerBuffer[i] && Close[i+1] >= LowerBuffer[i+1])
        {
         BuySignal[i] = 1;
         if(EnableAlerts)
            Alert(Symbol()," BUY Signal at ",TimeToString(Time[i],TIME_DATE|TIME_MINUTES));
        }
      if(Close[i] > UpperBuffer[i] && Close[i+1] <= UpperBuffer[i+1])
        {
         SellSignal[i] = 1;
         if(EnableAlerts)
            Alert(Symbol()," SELL Signal at ",TimeToString(Time[i],TIME_DATE|TIME_MINUTES));
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

🧭 How It Works

  • Middle Band: Exponential Moving Average (trend baseline)
  • Upper/Lower Bands: EMA ± (ATR × Multiplier)
  • Buy Signal: Price bounces from lower band
  • Sell Signal: Price bounces from upper band
  • No repainting because it only uses closed bar data

⚙️ Customization Tips

ParameterDescriptionSuggestion
BandPeriodSmoothing period14–50
MultiplierBand width1.5–2.5
MaMethodMoving average methodEMA or SMA
PriceTypeApplied priceClose
EnableAlertsAlerts toggletrue / false

Would you like me to add arrows and on-chart “BUY” / “SELL” labels as well (for visual signals)?
I can update the code to include that next.

Leave a Reply

Your email address will not be published. Required fields are marked *