Keltner Channels B
Download scriptThis custom indicator is designed to provide traders with a visual representation of market volatility and price trends by plotting a "channel" on the trading chart. It functions by establishing a central baseline and then projecting boundaries above and below that line based on recent price movements.
How the Logic Works
The tool creates three distinct components on your chart:
1. The Central Baseline (Middle Line) The core of the indicator is a moving average. This line tracks the "average" price over a specific number of previous candles. It helps traders identify the general direction of the market—whether the price is currently trending upward, downward, or moving sideways.
2. Volatility Calculation To determine how wide the channel should be, the script calculates the market's current volatility (how much the price swings). It does this in one of two ways depending on your settings:
- Standard Mode: It looks at the difference between the highest and lowest prices reached over a specific period.
- ATR Mode: It utilizes the Average True Range, which is a common technical tool used to measure market "noise" and movement intensity.
3. The Upper and Lower Boundaries Once the script knows where the price is (the middle line) and how much it is swinging (the volatility), it calculates the outer boundaries.
- The Upper Boundary is created by adding a multiplier of the volatility to the middle line.
- The Lower Boundary is created by subtracting that same multiplier from the middle line.
Summary for Traders
Think of this indicator as a "tunnel" for price action. When the market is calm, the tunnel narrows; when the market becomes volatile, the tunnel widens. This allows you to see at a glance whether the current price movement is normal for the current market conditions or if the price is stretching unusually far outside its expected range.
//+------------------------------------------------------------------+
//| 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 White
#property indicator_color2 White
#property indicator_color3 White
double upper[], middle[], lower[];
extern int MA_PERIOD = 20;
extern int MA_MODE = 0;
extern int PRICE_MODE = 5;
extern int ATR_PERIOD = 20;
extern double K = 2.2;
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