Keltner_Channels
Download scriptThis script is designed to provide a visual tool for traders to understand price volatility and identify potential trends on a currency or asset chart. It creates three distinct lines on your screen: an upper boundary, a middle baseline, and a lower boundary, which together form what is known as a Keltner Channel.
How the Logic Works
The core of this tool relies on two mathematical components: a "Middle Line" (the average price) and a "Volatility Measurement" (how much the price swings).
1. The Middle Line (Baseline) The script first looks at a specific number of past candles—defined by a user-set timeframe—and calculates the simple average closing price. This creates a smooth middle line that follows the general direction of the market. It acts as the "equilibrium" or the heartbeat of the asset's current movement.
2. The Volatility Measurement (The Range) To determine how wide the channel should be, the script performs a secondary calculation. It looks at the "High" and "Low" prices of each candle over the same period. By subtracting the low price from the high price, it determines the size of the "swing" for every candle. It then averages these swings to find out how much the market is typically moving back and forth.
3. Creating the Channel Boundaries Finally, the script combines these two pieces of information to draw the outer boundaries:
- The Upper Boundary: This is calculated by taking the Middle Line and adding the average volatility measurement to it.
- The Lower Boundary: This is calculated by taking the Middle Line and subtracting the average volatility measurement from it.
What This Means for the User
By plotting these three lines, the script creates a visual "tunnel" for price action. When the market is stable, the price tends to stay within these boundaries. When the price breaks significantly above or below these lines, it often signals that the market is experiencing unusual activity or a potential shift in momentum, helping traders visualize whether the current price movement is normal or extreme based on recent history.
//+------------------------------------------------------------------+
//| Keltner Channels.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 period = 10;
int init()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexShift(0,0);
SetIndexDrawBegin(0,0);
SetIndexBuffer(0,upper);
SetIndexStyle(1,DRAW_LINE);
SetIndexShift(1,0);
SetIndexDrawBegin(1,0);
SetIndexBuffer(1,middle);
SetIndexStyle(2,DRAW_LINE);
SetIndexShift(2,0);
SetIndexDrawBegin(2,0);
SetIndexBuffer(2,lower);
return(0);
}
int deinit(){return(0);}
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, period, 0, MODE_SMA, PRICE_CLOSE, x);
avg = findAvg(period, x);
upper[x] = middle[x] + avg;
lower[x] = middle[x] - 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