Keltner Channels F
Download scriptThis tool is designed to provide a visual representation of market volatility and price trends on a trading chart. It functions by creating a dynamic "channel" around the current price action, helping traders identify the normal range of price movement and potential overextended areas.
Core Components of the Logic
The script calculates three distinct lines that form the channel:
1. The Baseline (Middle Line) At its heart, the tool calculates a Moving Average. This serves as the "equilibrium" or the center path of the market. It smooths out short-term price fluctuations to show you the underlying trend direction—whether the market is generally leaning upward, downward, or moving sideways.
2. The Volatility Measurement To determine how wide the channel should be, the script measures market "noise" or volatility. It looks at the distance between the highest and lowest prices over a specific period of time. This ensures that when the market is wild and swinging aggressively, the channel expands; when the market is calm and steady, the channel narrows.
3. The Boundaries (Upper and Lower Lines) The script then takes the Middle Line and adds or subtracts a calculated margin based on the volatility measurement.
- The Upper Line represents the top boundary of the "normal" trading range.
- The Lower Line represents the bottom boundary.
How It Works in Practice
By combining these elements, the tool creates a corridor for the price to move within. The logic is designed to adapt automatically: as market conditions change, the boundaries shift and resize to accommodate current price behavior. This allows users to visually distinguish between standard market fluctuations and significant price breakouts that may occur outside of the established channel boundaries.
//+------------------------------------------------------------------+
//| 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 DarkGray
#property indicator_color2 DarkGray
#property indicator_color3 DarkGray
double upper[], middle[], lower[];
extern int MA_PERIOD = 34;
extern int MA_MODE = 3;
extern int PRICE_MODE = 4;
extern int ATR_PERIOD = 20;
extern double K = 0.1;
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