Keltner_ATR_Bands

Download script
Author: Copyright © 2005, MetaQuotes Software Corp.
Keltner_ATR_Bands

This tool is designed to provide traders with a visual representation of market volatility and price boundaries. Unlike standard moving averages that only show the central trend, this script calculates "bands" that expand and contract based on how much the price actually moves over time.

Core Logic and Functionality

The indicator works by analyzing two primary components of market data to create an upper and lower boundary:

1. Trend Baseline (Moving Average) The script first establishes a central path for the price. It looks back at a specific number of historical candles (defined by the user) to calculate a Simple Moving Average. This represents the "equilibrium" or the average price trajectory over that period.

2. Volatility Measurement (ATR) To determine how wide the bands should be, the script uses an Average True Range (ATR) calculation. This measures the current intensity of market fluctuations—essentially measuring how much "room" the price has been moving in recently.

3. Band Calculation The final visual output is created by applying a multiplier to that volatility measurement and adding or subtracting it from the central trend baseline:

  • Upper Band: The script takes the average price and adds a multiple of the current volatility. This creates a ceiling that adjusts automatically; when the market is calm, the ceiling drops; when the market becomes wild, the ceiling rises.
  • Lower Band: The script takes the average price and subtracts that same multiple of volatility, creating a floor.

How to Interpret the Results

By plotting these two lines on your chart, you get a visual "tunnel" of expected price action:

  • Dynamic Adjustments: Because the bands are tied to volatility (ATR), they are not static. They will widen during high-activity periods (like news events or market opens) and tighten during quiet sessions.
  • Price Context: When the price stays within these bands, it suggests the market is behaving normally relative to its recent history. If the price pushes toward or outside of these boundaries, it indicates that the current price movement is significantly stronger than the recent average volatility, potentially signaling an overextended move or a breakout.
Indicators Used
Moving average indicatorIndicator of the average true range
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
Keltner_ATR_Bands
//+------------------------------------------------------------------+
//|                                           Keltner ATR Bands .mq4 |
//|                                     This is not Keltner Channels |
//|                                                                  |
//|                                        Converted by : Dr. Gaines |
//|                                      dr_richard_gaines@yahoo.com |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright " Copyright © 2005, MetaQuotes Software Corp."
#property link      " http://www.metaquotes.net/"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Red
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Common External variables                                        |
//+------------------------------------------------------------------+
extern double MAPeriod=50;
extern double ATRMult=3.75;
//+------------------------------------------------------------------+
//| Special Convertion Functions                                     |
//+------------------------------------------------------------------+
int LastTradeTime;
double ExtHistoBuffer[];
double ExtHistoBuffer2[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetLoopCount(int loops)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetIndexValue(int shift, double value)
  {
   ExtHistoBuffer[shift]=value;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetIndexValue2(int shift, double value)
  {
   ExtHistoBuffer2[shift]=value;
  }
//+------------------------------------------------------------------+
//| End                                                              |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(0, ExtHistoBuffer);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(1, ExtHistoBuffer2);
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
//+------------------------------------------------------------------+
//| Local variables                                                  |
//+------------------------------------------------------------------+
   int shift=0;
   double ma=0;
   double atr=0;
   double KU=0;
   double KL=0;
/*[[
    Name := Keltner ATR Bands
    Author := Copyright © 2005, MetaQuotes Software Corp.
    Link := http://www.metaquotes.net/
    Separate Window := No
    First Color := White
    First Draw Type := Line
    First Symbol := 217
    Use Second Data := Yes
    Second Color := White
    Second Draw Type := Line
    Second Symbol := 218
]]*/
   SetLoopCount(0);
   // loop from first bar to current bar (with shift=0)
     for(shift=Bars-1;shift>=0 ;shift--){
      ma=iMA(NULL, 0, MAPeriod, 0,  MODE_SMA, PRICE_CLOSE,  shift);
      atr=iATR(NULL, 0, MAPeriod, shift);
      KU=ma + ATRMult*atr;
      KL=ma - ATRMult*atr;
      SetIndexValue(shift, KU);
      SetIndexValue2(shift, KL);
     }
   return(0);
  }
//+------------------------------------------------------------------+

Comments