Keltner_ATR_Band
Download scriptThis tool is designed to provide a visual representation of price volatility and trend boundaries on your trading chart. Unlike standard moving averages that only track the direction of the price, this script calculates a dynamic "channel" or "envelope" that helps traders identify the typical range of price movement based on historical data.
How the Logic Works
The script operates by calculating two primary components to determine the upper and lower boundaries of the market:
1. The Baseline (Moving Average) First, the tool calculates a Simple Moving Average (SMA) based on a specific timeframe (defined by the "Maperiod"). This acts as the central anchor or "mean" price. It smooths out the daily price fluctuations to show you the underlying trend of the market.
2. The Volatility Measure (ATR) Next, the script looks at the Average True Range (ATR). This is a technical metric that measures how much the price typically moves—up or down—over a set period. In simple terms, it tells the script whether the market is currently calm or highly volatile.
Creating the Bands
Once the tool has the baseline and the volatility measurement, it performs a specific calculation to create two distinct lines on your chart:
- The Upper Band: It takes the central Moving Average and adds the Volatility (ATR) multiplied by a specific factor. This creates an upper ceiling.
- The Lower Band: It takes the same central Moving Average and subtracts the Volatility (ATR) multiplied by that same factor. This creates a lower floor.
Practical Application
By plotting these two lines simultaneously, the script creates a visual corridor. Because the width of this corridor expands during times of high market activity and shrinks during periods of low activity, it provides a customized look at price "extremes." Traders often use these boundaries to identify when the price is moving significantly outside of its normal behavior, which can signal potential exhaustion in a trend or an upcoming change in market direction.
//+------------------------------------------------------------------+
//| 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 White
#property indicator_color2 White
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Common External variables |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| External variables |
//+------------------------------------------------------------------+
extern double MAPeriod = 50;
extern double ATRMult = 3.75;
//+------------------------------------------------------------------+
//| Special Convertion Functions |
//+------------------------------------------------------------------+
double ExtHistoBuffer[];
double ExtHistoBuffer2[];
//+------------------------------------------------------------------+
//| Initialization |
//+------------------------------------------------------------------+
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 |
//+------------------------------------------------------------------+
double ma = 0;
double atr = 0;
double KU = 0;
double KL = 0;
int limit, shift;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
/*[[
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
]]*/
// loop from first bar to current bar (with shift=0)
for(shift=limit;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;
ExtHistoBuffer[shift] = KU;
ExtHistoBuffer2[shift] = KL;
}
return(0);
}
Comments