Voltility bands

Download script
Author: mladen

This script functions as a specialized technical analysis tool designed to visualize market volatility. It creates dynamic "Volatility Bands" on your trading chart, helping you identify price extremes and understand the range of typical market movement relative to a smoothed average.

Core Functionality

The logic operates by calculating three primary components:

1. The Dynamic Midline (Moving Average) Instead of using a simple flat line, the script calculates a "Weighted Moving Average" (WMA). This serves as the center point of your bands. It gives more significance to recent price data while smoothing out minor "noise," ensuring that the centerline reflects the current trend more accurately than a standard average would.

2. Volatility Measurement (ATR Logic) The script tracks the market's "True Range." It doesn't just look at the difference between high and low prices; it also takes into account previous closing prices to capture gaps or significant jumps that occur between trading sessions. By averaging this data, the script establishes a reliable measure of how much the price is currently "swinging."

3. Band Calculation & Adjustment This is where the visual bands are created. The script takes the calculated volatility and projects it above and below the Midline:

  • Upper Band: Calculated by adding a portion of the volatility to the average price.
  • Lower Band: Calculated by subtracting a portion of the volatility from the average price.
  • Asymmetric Adjustment: The logic includes a specific "Low Band Adjustment" setting. This allows you to slightly tighten or loosen the lower boundary, providing a customizable look at how the price reacts during downtrends versus uptrends.

How It Works on Your Chart

  • Real-Time Updates: The script constantly monitors new price ticks. As price action becomes more volatile (larger swings), the bands will expand. When the market stabilizes and moves in a tight range, the bands will contract.
  • Visual Guidance: By observing where the price sits in relation to these bands, traders can visually identify "overbought" or "oversold" conditions. If the price pushes to the outer edges of these bands, it indicates that the current move is significantly larger than the recent historical average.
  • Customizable Sensitivity: You can adjust the periods for the midline, the volatility calculation, and the deviation strength. This allows you to switch between viewing short-term "scalping" volatility or long-term "swing" trends depending on your trading style.
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
11 Views
0 Downloads
0 Favorites
Voltility bands
ÿþ//+------------------------------------------------------------------

#property copyright   "mladen"

#property link        "mladenfx@gmail.com"

#property description "Volatility bands"

//+------------------------------------------------------------------

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   3

#property indicator_type1   DRAW_LINE

#property indicator_type2   DRAW_LINE

#property indicator_type3   DRAW_LINE

#property indicator_color1  clrDeepSkyBlue

#property indicator_color2  clrSilver

#property indicator_color3  clrOrange

#property indicator_style2  STYLE_DOT



//

//

//



input int                inpPeriod     = 20;          // Period

input int                inpMidPeriod  = 20;          // Midline period

input ENUM_APPLIED_PRICE inpPrice      = PRICE_CLOSE; // CCI price

input double             inpDeviations = 2.4;         // bands deviation

input double             inpLowAdjust  = 0.9;         // Low band adjust



//

//

//



double mid[],bandu[],bandd[],tr[],atr[],avg[];

int ª_atrPeriod,ª_avgHandle;



//------------------------------------------------------------------

// Custom indicator initialization function

//------------------------------------------------------------------

int OnInit()

{

   //

   //--- indicator buffers mapping

   //

         SetIndexBuffer(0,bandu);

         SetIndexBuffer(1,mid);

         SetIndexBuffer(2,bandd);

         SetIndexBuffer(3,tr    ,INDICATOR_CALCULATIONS);

         SetIndexBuffer(4,atr   ,INDICATOR_CALCULATIONS);

         SetIndexBuffer(5,avg   ,INDICATOR_CALCULATIONS);

            ª_atrPeriod = inpPeriod*2-1;

            ª_avgHandle  = iMA(_Symbol,0,inpMidPeriod,0,MODE_LWMA,inpPrice);     if (!_checkHandle(ª_avgHandle,"Average")) { return(INIT_FAILED); }

            iLwma.init(inpMidPeriod);

   //         

   //--- indicator short name assignment

   //

   IndicatorSetString(INDICATOR_SHORTNAME,"Volatility bands ("+(string)inpPeriod+","+(string)inpMidPeriod+","+(string)inpDeviations+","+(string)inpLowAdjust+")");

   return (INIT_SUCCEEDED);

}

void OnDeinit(const int reason) { }



//------------------------------------------------------------------

//

//------------------------------------------------------------------

//

//---

//



#define _setPrice(_priceType,_target,_index) \

   { \

   switch(_priceType) \

   { \

      case PRICE_CLOSE:    _target = close[_index];                                              break; \

      case PRICE_OPEN:     _target = open[_index];                                               break; \

      case PRICE_HIGH:     _target = high[_index];                                               break; \

      case PRICE_LOW:      _target = low[_index];                                                break; \

      case PRICE_MEDIAN:   _target = (high[_index]+low[_index])/2.0;                             break; \

      case PRICE_TYPICAL:  _target = (high[_index]+low[_index]+close[_index])/3.0;               break; \

      case PRICE_WEIGHTED: _target = (high[_index]+low[_index]+close[_index]+close[_index])/4.0; break; \

      default : _target = 0; \

   }}

   

//

//---

//



int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

{

   int _copyCount = rates_total-prev_calculated+1; if (_copyCount>rates_total) _copyCount=rates_total;

         if (CopyBuffer(ª_avgHandle,0,0,_copyCount,avg)!=_copyCount) return(prev_calculated);

   

   //

   //---

   //

  

   int i=(prev_calculated>0?prev_calculated-1:0); for (; i<rates_total && !_StopFlag; i++)

   {

      if (i>0)

      {

         tr[i]  = (high[i]>close[i-1] ? high[i] : close[i-1]) - (low[i]<close[i-1] ? low[i] : close[i-1]);

         if (i>ª_atrPeriod)

                atr[i] = atr[i-1]+tr[i]-tr[i-ª_atrPeriod];

         else { atr[i] = tr[i]; for (int k=1; k<ª_atrPeriod && i>=k; k++) atr[i] += tr[i-k]; }

      }

      else tr[i] = atr[i] = high[i]-low[i];

      

      //

      //

      //



      double _price; _setPrice(inpPrice,_price,i);  

         if (avg[i]==EMPTY_VALUE) mid[i] = _price;

         double _atr = inpDeviations*atr[i]/(double)ª_atrPeriod; 



                bandu[i] = avg[i] + avg[i]*(_atr/_price);

                bandd[i] = avg[i] - avg[i]*(_atr*inpLowAdjust/_price);

                mid[i]  = iLwma.calculate((high[i]+low[i])/2.0,i) ;

   }

   return (i);

}

  

//------------------------------------------------------------------

//    custom functions

//------------------------------------------------------------------

//

//---

//



class CLwma

{

   #define _ringSize 32 

   private :

      struct sLwmaArrayStruct

         {

            double value;

            double wsumm;

            double vsumm;

         };

      sLwmaArrayStruct m_array[];

      int              m_arraySize;

      int              m_period;

      double           m_weight;

   public :

      CLwma() { init(1);            return; }

     ~CLwma() { ArrayFree(m_array); return; }

     

     //

     //---

     //



     void init(int period)

         { 

            m_period    = (period>1) ? period : 1; 

            m_arraySize = m_period+_ringSize;

            m_weight    = 1;

               ArrayResize(m_array,m_arraySize);

               for (int k=0; k<m_arraySize; k++)

               {

                  m_array[k].value  = 

                  m_array[k].wsumm  = 

                  m_array[k].vsumm  = 0;

               }

         }

         

     double calculate(double value, int i)

         {

            int _indC = (i)%m_arraySize; m_array[_indC].value=value;

               if (i>m_period)

               {

                  int _indP = _indC-1; if (_indP<0) _indP += m_arraySize;

                  int _indB = (i-m_period)%m_arraySize;

                     m_array[_indC].wsumm  = m_array[_indP].wsumm+value*m_period-m_array[_indP].vsumm;

                     m_array[_indC].vsumm  = m_array[_indP].vsumm+value         -m_array[_indB].value;

               }

               else

               {

                     m_weight              = 0;

                     m_array[_indC].wsumm  = 0;

                     m_array[_indC].vsumm  = 0;

                     for(int k=0, w=m_period, _indR=_indC; k<m_period && i>=k; k++,w--,_indR--)

                     {

                        if (_indR<0) _indR += m_arraySize;

                           m_weight             += w;

                           m_array[_indC].wsumm += m_array[_indR].value*(double)w;

                           m_array[_indC].vsumm += m_array[_indR].value;

                     }

               }

               return(m_array[_indC].wsumm/m_weight);

      }   

   #undef _ringSize

};

CLwma iLwma;



//

//---

//



bool _checkHandle(int _handle, string _description)

{

   static int  _handles[];

          int  _size   = ArraySize(_handles);

          bool _answer = (_handle!=INVALID_HANDLE);

          if  (_answer)

               { ArrayResize(_handles,_size+1); _handles[_size]=_handle; }

          else { for (int i=_size-1; i>=0; i--) IndicatorRelease(_handles[i]); ArrayResize(_handles,0); Alert(_description+" initialization failed"); }

   return(_answer);

}    

  

//+------------------------------------------------------------------+

Comments