DigBollingerBands

Download script
Author: Copyright � 2006, Nikolay Kositsin

The following description outlines the logic and functionality of the DigBollingerBands indicator. This summary is designed for a general audience to understand how the tool processes market data without delving into the underlying code structure.

Overview

The DigBollingerBands indicator is a modified version of the classic Bollinger Bands technical analysis tool. While standard Bollinger Bands provide a visual representation of price volatility and trend direction, this specific version incorporates "digitization" or "discretization" logic. This means it smooths out minor price fluctuations to focus on more significant movements, helping traders filter out market "noise."

Key Functional Components

1. Customizable Price Inputs The indicator does not strictly rely on the closing price of a candle. It allows the user to select various types of price data for its calculations, such as:

  • Open, High, or Low prices.
  • Median, Typical, or Weighted averages.
  • Advanced price models like Heiken Ashi values.

2. Advanced Moving Average Calculation At the heart of the indicator is a moving average (the "midline"). The script provides multiple different methods for calculating this average. Once the moving average is calculated, the indicator applies a specific step—referred to as a "digitization" value—to smooth the midline. This prevents the indicator from reacting too aggressively to every tiny tick in price movement.

3. Volatility and Deviation Logic The indicator calculates two outer bands (Upper and Lower) based on the distance from the smoothed midline.

  • It measures the variance (how much prices swing away from the average).
  • It applies a "Deviation" multiplier to determine how wide the bands should be.
  • Like the midline, these outer bands are also subjected to digitization logic. This ensures that the bands move in more distinct, meaningful increments rather than fluid, constant motion.

4. Dynamic Visualization The indicator renders three primary visual elements on the chart:

  • The Upper Band: Represents a high-volatility ceiling.
  • The Moving Average (Midline): Represents thesmoothed trend of the price.
  • The Lower Band: Represents a low-volatility floor.

How it Works in Practice

When you apply this indicator to a chart, it analyzes a specific period of historical data (the "Bands_Period"). It calculates where the "center" of the price action is and then builds a corridor around that center based on how much the price typically fluctuates.

Because of the unique digitization logic included in this script, the lines will appear more stable than standard Bollinger Bands. Instead of shifting constantly with every minor price change, the indicator waits for price movements to reach a certain threshold before "stepping" to the next value. This is intended to provide a clearer picture of the market's primary trend and volatility boundaries.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
7 Views
0 Downloads
0 Favorites
DigBollingerBands
/*
Äëÿ  ðàáîòû  èíäèêàòîðà  ñëåäóåò  ïîëîæèòü ôàéëû 
PriceSeries.mqh 
GetDigVelue.mqh
â ïàïêó (äèðåêòîðèþ): MetaTrader\experts\include\
*/
//+------------------------------------------------------------------+
//|                                            DigBollingerBands.mq4 | 
//|                        Copyright © 2006,        Nikolay Kositsin | 
//|                              Khabarovsk,   farria@mail.redcom.ru | 
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Nikolay Kositsin"
#property link "farria@mail.redcom.ru" 
//---- îòðèñîâêà èíäèêàòîðà â ãëàâíîì îêíå
#property indicator_chart_window 
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôôåðîâ
#property indicator_buffers 3
//---- öâåò èíäèêàòîðà
#property indicator_color1 Lime
#property indicator_color2 Gray
#property indicator_color3 Red
//---- ñòèëü ëèíèé Bollinger Bands
#property indicator_style1 4
#property indicator_style2 4
#property indicator_style3 4
//---- ÂÕÎÄÍÛÅ ÏÀÐÀÌÅÒÐÛ ÈÍÄÈÊÀÒÎÐÀ --------------------------------------------------------------------------------------------------+
extern int        Bands_Period = 200; // ïåðèîä  óñðåäíåíèÿ J2Bollinger Bands
extern double Bands_Deviations = 2.0; // äåâèàòàöèÿ 
extern int           MA_method = 0;   // ìåòîä óñðåäíåíèÿ
extern int             MA_Step = 6; // ïàðàìåòð, îïðåäåëÿùèé ðàçìåð êîëåáàíèé ïðèíèìàåìûõ çà ôëýò èëè øàã äèñêðåòèçàöèè ìóâèíãà
extern int          Bands_Step = 6; // ïàðàìåòð, îïðåäåëÿùèé ðàçìåð êîëåáàíèé ïðèíèìàåìûõ çà ôëýò èëè øàã äèñêðåòèçàöèè ïîëîñ Áîëëèíäæåðà
extern int         Bands_Shift = 0;   // cäâèã èíäèêàòîðà âäîëü îñè âðåìåíè 
extern int Input_Price_Customs = 0;//Âûáîð öåí, ïî êîòîðûì ïðîèçâîäèòñÿ ðàñ÷¸ò èíäèêàòîðà 
//(0-CLOSE, 1-OPEN, 2-HIGH, 3-LOW, 4-MEDIAN, 5-TYPICAL, 6-WEIGHTED, 7-Heiken Ashi Close, 8-SIMPL, 9-TRENDFOLLOW, 10-0.5*TRENDFOLLOW,
//11-Heiken Ashi Low, 12-Heiken Ashi High,  13-Heiken Ashi Open, 14-Heiken Ashi Close.)
//---- -------------------------------------------------------------------------------------------------------------------------------+
//---- èíäèêàòîðíûå áóôôåðû
double UpperBuffer[];
double LowerBuffer[];
double MovingBuffer[];
double VelueBuffer[];
//---- Ââåäåíèå êîíñòàíò ñ ïëàâàþùåé òî÷êîé 
double MA_Norma,Bands_Norma;
//+------------------------------------------------------------------+  
//----+ Ââåäåíèå ôóíêöèè PriceSeries
//----+ Ââåäåíèå ôóíêöèè PriceSeriesAlert 
#include <PriceSeries.mqh>
//+------------------------------------------------------------------+ 
//----+ Ââåäåíèå ôóíêöèè GetDigVelue
#include <GetDigVelue.mqh>
//+------------------------------------------------------------------+ 
//| DigBollingerBands initialization function                        | 
//+------------------------------------------------------------------+
int init()
  {
//---- Ñòèëü èñïîëíåíèÿ ãðàôèêà 
   SetIndexStyle(0,DRAW_LINE); 
   SetIndexStyle(1,DRAW_LINE);
//---- 4 èíäèêàòîðíûõ áóôôåðîâ èñïîëüçîâàíû äëÿ ñ÷¸òà
   IndicatorBuffers(4); 
   SetIndexBuffer(0,UpperBuffer );  
   SetIndexBuffer(1,MovingBuffer);
   SetIndexBuffer(2,LowerBuffer );
   SetIndexBuffer(3,VelueBuffer);
//---- óñòàíîâêà íîìåðà áàðà, íà÷èíàÿ ñ êîòîðîãî áóäåò îòðèñîâûâàòüñÿ èíäèêàòîð 
   int drawbegin=Bands_Period+Bands_Shift; 
   SetIndexDrawBegin(0,drawbegin);
   SetIndexDrawBegin(1,drawbegin);
   SetIndexDrawBegin(2,drawbegin);
//---- ãîðèçîíòàëüíûé ñäâèã èíäèêàòîðíûõ ëèíèé  
   SetIndexShift (0, Bands_Shift); 
   SetIndexShift (1, Bands_Shift); 
   SetIndexShift (2, Bands_Shift); 
//---- èìÿ äëÿ îêîí äàííûõ è ëýéáà äëÿ ñóáúîêîí. 
   IndicatorShortName ("Bollinger Bands( Period="+Bands_Period+", Deviations="+Bands_Deviations+")"); 
   SetIndexLabel (0, "Upper Bands, Deviations=+"+Bands_Deviations+""); 
   SetIndexLabel (2, "Lower Bands, Deviations=-"+Bands_Deviations+""); 
   string JMoving;
   switch(MA_method)
           {
            case  0: JMoving="JSMA"; break;
            case  1: JMoving="JEMA"; break;
            case  2: JMoving="JSSMA";break;
            case  3: JMoving="JLWMA";break;
            default: JMoving="JSMA";
           }
    SetIndexLabel (1, "JMoving Avereges "+JMoving+" ("+Bands_Period+")");
//----          
//---- óñòàíîâêà àëåðòîâ íà íåäîïóñòèìûå çíà÷åíèÿ âõîäíûõ ïàðàìåòðîâ ======================================================================================+ 
if(Bands_Period<1)   {Alert("Ïàðàìåòð Bands_Period äîëæåí áûòü íå ìåíåå 1 "+ " Âû ââåëè íåäîïóñòèìîå " +Bands_Period+ " áóäåò èñïîëüçîâàíî  1");}//////////|
if(MA_method<0)      {Alert("Ïàðàìåòð MA_method äîëæåí áûòü îò 0 äî 3"   + " Âû ââåëè íåäîïóñòèìîå " +MA_method+ " áóäåò èñïîëüçîâàíî 0");}////////////////|
if(MA_method>3)      {Alert("Ïàðàìåòð MA_method äîëæåí áûòü îò 0 äî 3"   + " Âû ââåëè íåäîïóñòèìîå " +MA_method+ " áóäåò èñïîëüçîâàíî 0");}////////////////|
PriceSeriesAlert(Input_Price_Customs);//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////|
//+========================================================================================================================================================+ 
//---- êîðåêöèÿ íåäîïóñòèìîãî çíà÷åíèÿ ïàðàìåòðà Bands_Period
if(Bands_Period<1)Bands_Period=1; 
//---- Èíèöèàëèçàöèÿ êîíñòàíò
MA_Norma=MA_Step*Point;
Bands_Norma=Bands_Step*Point;
//---- çàâåðøåíèå èíèöèàëèçàöèè
   return(0);
  }
//+------------------------------------------------------------------+
//| DigBollingerBands iteration function                             | 
//+------------------------------------------------------------------+
int start()
  {
//---- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷¸òà
if(Bars<=Bands_Period) return(0);
//----+ Ââåäåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé
double sum,midline,Velue,priceswing,deviation;
//----+ Ââåäåíèå öåëûõ ïåðåìåííûõ è ïîëó÷åíèå óæå ïîäñ÷èòàííûõ áàðîâ
int bar,kk,MaxBarBB,limit,counted_bars=IndicatorCounted();
//---- ïðîâåðêà íà âîçìîæíûå îøèáêè
if (counted_bars<0)return(-1);
//---- ïîñëåäíèé ïîäñ÷èòàííûé áàð äîëæåí áûòü ïåðåñ÷èòàí
if (counted_bars>0) counted_bars--;
//---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà, íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò íîâûõ áàðîâ
limit=Bars-counted_bars-1; MaxBarBB=Bars-1-Bands_Period;
//----+ çàãðóçêà âõîäíûõ öåí â áóôôåð äëÿ ðàñ÷¸òà       
for(bar=limit;bar>=0;bar--)VelueBuffer[bar]=PriceSeries(Input_Price_Customs,bar);
//---- ïðîâåðêà áàðà íà äîñòàòî÷íîñòü äëÿ ðàñ÷¸òà Bollinger Bands 
if (limit>MaxBarBB)limit=MaxBarBB;

for(bar=limit;bar>=0;bar--)
     {
      //----+ ôîðìóëà äëÿ ðàñ÷¸òà Moving Avereges
      midline=iMAOnArray(VelueBuffer,0,Bands_Period,0,MA_method, bar);
      MovingBuffer[bar]=GetDigVelue(MA_Norma,midline);
      
      //---- ÐÀÑ×¨Ò Bollinger Bands
      sum=0.0;
      kk=bar+Bands_Period-1;
      while(kk>=bar)
        {
         priceswing=PriceSeries(Input_Price_Customs,kk)-midline;
         sum+=priceswing*priceswing;
         kk--;
        }
      deviation=Bands_Deviations*MathSqrt(sum/Bands_Period);
      //----
      UpperBuffer[bar]=GetDigVelue(Bands_Norma,midline+deviation);
      //----
      LowerBuffer[bar]=GetDigVelue(Bands_Norma,midline-deviation);
      //----
    }
//----
   return(0);
  }
//+-----------------------------------------------------------+

Comments