Keltner Channels1

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
Keltner Channels1

This script is a custom technical analysis tool designed to map out price volatility and trend direction on a trading chart. It visualizes this data by creating a "channel"—a set of three distinct lines that help traders identify the normal range of price movement and spot potential extremes.

Core Components

The tool works by calculating and plotting three specific lines:

  • The Middle Line: This represents the equilibrium or the "average" price over a specific period. It serves as the baseline for the entire channel, showing you where the price is currently centering itself.
  • The Upper and Lower Boundaries: These lines sit above and below the middle line. They represent the outer limits of normal market activity based on recent volatility.

How the Logic Works

The script determines the width of the channel by analyzing how much the price fluctuates. It does this through a multi-step calculation:

  1. Trend Tracking: First, it calculates a moving average to establish the center of the trend. This ensures the channel moves with the general direction of the market rather than staying static.
  2. Volatility Measurement: The script then looks at the "range" of price movement (the distance between highs and lows) over a set period. It can calculate this in two different ways depending on your settings:
    • It can use a standard volatility formula (ATR).
    • Alternatively, it can perform a manual calculation by measuring the average height of the price candles over a specific number of bars.
  3. Channel Construction: Finally, it takes that volatility measurement and multiplies it by a user-defined factor. This "multiplier" determines how wide or narrow the channel appears. It adds this value to the middle line to create the upper boundary and subtracts it to create the lower boundary.

Practical Application

By visualizing these three lines, the tool provides a clear picture of market "noise." When price stays within the white boundaries, it is considered to be moving within its normal expected range. If the price breaks significantly outside of the upper or lower lines, it often signals that a new trend is forming or that the market has reached an overextended state, potentially providing a signal for traders to watch for a reversal or a breakout.

Indicators Used
Moving average indicatorIndicator of the average true range
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
Keltner Channels1
//+------------------------------------------------------------------+
//|                                               KeltnerChannel.mq4 |
//|                                                  Coded by Gilani |
//|                      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 White
#property indicator_color2 White
#property indicator_color3 White


double upper[], middle[], lower[];
extern int     MA_PERIOD = 20;
extern int     MA_MODE = 0;
extern int     PRICE_MODE = 5;
extern int     ATR_PERIOD = 20;
extern double  K = 1.5;
extern bool    ATR_MODE = false;

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

   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexShift(1,0);
   SetIndexDrawBegin(1,0);
   SetIndexBuffer(1,middle);

   SetIndexStyle(2,DRAW_LINE);
   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++) {
      
      middle[x] = iMA(NULL, 0, MA_PERIOD, 0, MA_MODE, PRICE_MODE, x);
      
      if (ATR_MODE)
      avg  = iATR(NULL,0,ATR_PERIOD, x);
      else 
      avg  = findAvg(ATR_PERIOD, x);
      
      upper[x] = middle[x] + K*avg;
      lower[x] = middle[x] - K*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