Article Thumbnail

The V-Trade, Part 5: Technical Analysis—Moving Average Support & Resistance And Volatility Bands

 by Sylvain Vervoort

Sidebar for the article “The V-Trade, Part 5: Technical Analysis—Moving Average Support & Resistance And Volatility Bands” by Sylvain Vervoort, in the July 2018 issue of Technical Analysis of STOCKS & COMMODITIES magazine.


Sidebar — MT4 Code For Smoothed Volatility Band 2018 V1.1

//+------------------------------------------------------------------+
//|                                            SveVolatilityBand.mq4 |
//|                               Copyright © 2018, Sylvain Vervoort |
//|                                              https://stocata.org/ |
//+------------------------------------------------------------------+
#property copyright   "©2018, Sylvain vervoort"
#property link        "https://stocata.org/"
#property description "Smoothed Volatility Band 2018 V1.1"
#property strict

#property indicator_chart_window

#property indicator_buffers    3
#property indicator_color1     Magenta
#property indicator_color2     Magenta 
#property indicator_color3     Blue  
#property indicator_width1     2
#property indicator_width2     2
#property indicator_width3     2
#property indicator_style1     STYLE_SOLID
#property indicator_style2     STYLE_SOLID
#property indicator_style3     STYLE_SOLID

//---- input parameters
extern bool    VolatilityBand    = true;  // True=Volatility ,false=Bollinger
extern int     BandsPeriod       = 20;    // Bands Average Bars
extern double  BandsDeviation    =  2.4;  // Bands Volatility Range
extern double  LowbandAdjust     =  0.9;  // Low Band Adjust (Vol. Band Only)
extern int     MiddleLineLwma    = 20;    // Middle Line LWMA Average

//---- buffers
double HighChannel   [];
double LowChannel    [];
double MedianAverage [];
double AtrBuf        [];
double TempBuf       [];
//+------------------------------------------------------------------+
//| SveVolatilityBand Indicator Initialisation                       |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//---- indicators
   IndicatorBuffers(5);
   SetIndexStyle (0,DRAW_LINE);
   SetIndexBuffer(0,HighChannel);
   SetIndexDrawBegin(0,BandsPeriod*2-1);
   SetIndexStyle (1,DRAW_LINE);
   SetIndexBuffer(1,LowChannel); 
   SetIndexDrawBegin(1,BandsPeriod*2-1);
   SetIndexStyle (2,DRAW_LINE);
   SetIndexBuffer(2,MedianAverage);
   SetIndexDrawBegin(2,MiddleLineLwma);
   SetIndexBuffer(3,AtrBuf);
   SetIndexBuffer(4,TempBuf);
      
   string volband    = IntegerToString(VolatilityBand);
   string bandsper   = IntegerToString(BandsPeriod);
   string bandsdev   = DoubleToString (BandsDeviation,1);
   string lowbandadj = DoubleToString (LowbandAdjust,2);
   string medianlwma = IntegerToString(MiddleLineLwma); 
   IndicatorShortName("SveVolatilityBand("+volband+" "+bandsper+" "+bandsdev+
                      " "+lowbandadj+" "+medianlwma+")");
  //--- check input parameters validity                     
  if(BandsPeriod <= 0 || BandsDeviation <= 0 || LowbandAdjust <= 0 || MiddleLineLwma < 1)
    { 
     Alert("Faulty Input Parameter, Defaults used. Try again!");
     BandsPeriod = 20; BandsDeviation = 2.4; LowbandAdjust = 0.9; MiddleLineLwma = 20;
    }
                                     
//--- initialization done
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| SveVolatilityBand iteration function                             |
//+------------------------------------------------------------------+
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[])
  {    

   //Less than minimum required bars
   int i, limit;
   if(prev_calculated<0) return(-1); // Return error No bars available
   else limit = rates_total-1;   // -1 addressing counts to 0
   
   if(VolatilityBand) // VolatilityBand == true. Draw volatility bands
   {
    // Use a price range
    i = limit;
    while(i>=0)
     {
      if(i==limit)TempBuf[i]=high[i]-low[i];
      else TempBuf[i]=MathMax(high[i],close[i+1])-MathMin(low[i],close[i+1]);
      i--;
     }
     
    // calculate price deviation
    for (i = limit; i>=0;i--)
    AtrBuf[i]=iMAOnArray(TempBuf,0,BandsPeriod*2-1,0,MODE_SMA,i) * BandsDeviation; 
    // Create upper, lower channel and middle line
    for (i=limit;i>=0;i--) HighChannel[i]=iMA(NULL,0,BandsPeriod,0,MODE_LWMA,PRICE_CLOSE,i) +
                          (iMA(NULL,0,BandsPeriod, 0,MODE_LWMA,PRICE_CLOSE,i)*AtrBuf[i]/close[i]);
    for (i=limit;i>=0;i--) LowChannel[i]=iMA(NULL, 0,BandsPeriod,0,MODE_LWMA,PRICE_CLOSE,i) -
                          (iMA(NULL,0,BandsPeriod, 0,MODE_LWMA,PRICE_CLOSE,i)*AtrBuf[i]*
                           LowbandAdjust/close[i]);
    for (i=limit;i>=0;i--) 
         MedianAverage[i]=iMA(NULL,0,MiddleLineLwma,0,MODE_LWMA,PRICE_TYPICAL,i);
   
//----

   }
   else // If volatility Band == false, draw Bollinger bands
   {


    for (i=limit;i>=0;i--) HighChannel[i]   = 
         iBands(NULL,0,BandsPeriod,BandsDeviation,0,PRICE_CLOSE,MODE_UPPER,i);
    for (i=limit;i>=0;i--) LowChannel[i]    = 
         iBands(NULL,0,BandsPeriod,BandsDeviation,0,PRICE_CLOSE,MODE_LOWER,i);
    for (i=limit;i>=0;i--) MedianAverage[i] = 
         iMA   (NULL,0,MiddleLineLwma,0,MODE_LWMA,PRICE_TYPICAL,i);        
         
   }
   return(rates_total);
  }
// +-------------------------------------END OF PROGRAM-------------------------------------------+

— Sylvain Vervoort