Keltner_ATR_Band mt4

Download script
Author: Copyright © 2005, MetaQuotes Software Corp.
Keltner_ATR_Band mt4

This script is designed to work as a visual indicator for your trading charts, specifically providing a method to measure market volatility and price extremes. It generates two distinct boundary lines that surround the current price action to help traders identify the "normal" range of market movement.

How the Logic Works

The tool operates by calculating three primary mathematical components at every point on your chart:

1. The Baseline (Moving Average) First, the script looks back at a specific number of previous candles (defined by the "MAPeriod" setting) to calculate the Simple Moving Average. This represents the "fair value" or the average price of the asset over that period. Think of this as the center gravity of the market.

2. The Volatility Measurement (ATR) Next, the script calculates the Average True Range (ATR). Unlike a moving average which tracks price, the ATR measures how much the price is "swinging" or fluctuating. It tells the script whether the market is currently calm and stable or wild and erratic.

3. The Upper and Lower Bands The final step combines these two pieces of information. The script takes the Baseline (the average) and adds or subtracts a multiple of the Volatility Measurement (the ATR).

  • Upper Band: Created by adding the volatility to the average.
  • Lower Band: Created by subtracting the volatility from the average.

Visual Representation

The result is two white lines displayed on your chart. Because these bands are tied to the Average True Range, they are "dynamic."

  • When the market becomes highly volatile, the bands will widen significantly to accommodate the larger price swings.
  • When the market becomes quiet or ranging, the bands will contract toward each other.

Essentially, this tool helps you visualize the boundaries of current market behavior, providing a visual cue for when price might be overextended relative to its recent average movement.

Indicators Used
Moving average indicatorIndicator of the average true range
Miscellaneous
Implements a curve of type %1
16 Views
0 Downloads
0 Favorites
Keltner_ATR_Band mt4
//+------------------------------------------------------------------+
//|                                           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