Keltner_ATR_Band mt4_v1
Download scriptThis technical indicator, known as the Keltner ATR Bands, is designed to provide traders with a visual representation of market volatility and price boundaries directly on their trading charts. Unlike standard moving averages that only show a single trend line, this tool creates a dynamic "channel" around the current price action to help identify overextended movements.
How the Logic Works
The indicator functions by calculating two specific mathematical components based on historical price data:
-
The Baseline (Moving Average): The script first calculates a Simple Moving Average (SMA) based on a user-defined period (set by default to 50). This represents the "average" price of the asset over that timeframe, acting as the center point of the channel.
-
The Volatility Measurement (ATR): Simultaneously, the script calculates the Average True Range (ATR). The ATR is a critical market indicator that measures how much an asset typically moves in a given period. Essentially, it tells the script how "choppy" or "calm" the market is.
Creating the Bands
The core logic of the indicator lies in combining these two elements to create two distinct boundary lines:
- Upper Band: The script takes the Baseline (SMA) and adds a multiple of the volatility (ATR) to it. This creates a ceiling that moves upward when volatility increases or the trend climbs.
- Lower Band: The script takes the Baseline (SMA) and subtracts that same volatility multiple from it. This creates a floor that moves downward as price drops or fluctuations widen.
Practical Application for Traders
By plotting these two lines—the Upper and Lower Bands—on your chart, you can see the "normal" range of price movement.
- Volatility Tracking: When the bands widen, the market is experiencing high volatility (large price swings). When the bands contract, the market is in a period of consolidation or low activity.
- Price Extremes: These lines serve as visual cues for potential overbought or oversold conditions. When the price pushes to the outer edges of these bands, it may indicate that the asset is stretching too far from its average and could be due for a reversal or a period of cooling off.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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 |
//+------------------------------------------------------------------+
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 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| 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 |
//+------------------------------------------------------------------+
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