Trend Bands

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
Trend Bands

This "Trend Bands" tool is designed to provide visual guidance on market trends by plotting a multi-layered band system directly onto your trading chart. Its primary objective is to help traders identify the current price direction and potential areas of overextension or reversal by combining smoothed moving averages with historical volatility data.

Core Logic and Visual Components

The script generates three distinct lines on your chart, each serving a specific role in analyzing the market's momentum:

  • The Dynamic Center (EMA): The indicator calculates an Exponential Moving Average (EMA). Unlike a simple average, this emphasizes recent price action more heavily, providing a "heartbeat" of the current trend. This serves as your primary baseline for understanding where the price is currently heading.
  • The Trend Boundary (SMA): To establish the outer boundaries, the script utilizes a Simple Moving Average (SMA). While the center line focuses on recent speed, this component provides a more stable foundation to calculate the width of the trading bands.
  • The Upper and Lower Bands: These are the "envelopes" surrounding the trend. They are calculated by taking the stable moving average and adding or subtracting a specific multiplier against the average price range over a set period.

How the Calculation Works

To determine how wide these bands should be, the script performs a unique calculation of market "stretch":

  1. Volatility Measurement: It looks back over a specific number of bars (the "period") and measures the total distance between the highest price (High) and the lowest price (Low) for each bar.
  2. Average Range: It averages those distances to find the typical "swing" of the market.
  3. Band Expansion: The script takes this average swing and multiplies it by a factor of 3.5. This result is then added to the upper boundary and subtracted from the lower boundary.

Practical Application for Traders

By combining these elements, the indicator creates a visual "corridor." When price stays within these bands, it suggests the market is following a healthy trend relative to its typical volatility. If the price pushes significantly outside of these yellow and magenta bands, it may indicate that the market is becoming overextended, potentially signaling that the current move is exhausted and a reversal or correction might be approaching.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
Trend Bands
//+------------------------------------------------------------------+
//|                                                  Trend Bands.mq4 |
//|                                              Dwt5 and adoleh2000 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Magenta
#property indicator_color3 Yellow


double upper[], middle1[], middle2, lower[];
extern int period = 34;


int init()
  {
   SetIndexStyle(0,DRAW_LINE,EMPTY,2);
   SetIndexShift(0,0);
   SetIndexDrawBegin(0,0);
   SetIndexBuffer(0,upper);

   SetIndexStyle(1,DRAW_LINE,EMPTY,3);
   SetIndexShift(1,0);
   SetIndexDrawBegin(1,0);
   SetIndexBuffer(1,middle1);
   
   SetIndexStyle(2,DRAW_LINE,EMPTY,2);
   SetIndexShift(2,0);
   SetIndexDrawBegin(2,0);
   SetIndexBuffer(2,lower);
    

//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   double avg;
   
   for(int x=0; x<limit; x++) {
      middle1[x] = iMA(NULL, 0, period, 0, MODE_EMA, PRICE_TYPICAL, x);// drawn line
      middle2= iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x);// only used to calculate outer bands

      avg  = findAvg(period, x);
      upper[x] = middle2 + (3.5*avg);
      lower[x] = middle2 - (3.5*avg);
   }
   return(0);
  }
//+------------------------------------------------------------------+


   double findAvg(int period, int shift) {
      double sum=0;
      for (int x=shift;x<(shift+period);x++) {     
         sum += High[x]-Low[x];
      }
      sum = sum/period;
      return (sum);
   }

Comments