Mass_Index_BB

Author: Copyright � 2011, Nikolay Kositsin
2 Views
0 Downloads
0 Favorites
Mass_Index_BB
//+---------------------------------------------------------------------+
//|                                                   Mass_Index_BB.mq5 | 
//|                                Copyright © 2011,   Nikolay Kositsin | 
//|                                 Khabarovsk,   farria@mail.redcom.ru | 
//+---------------------------------------------------------------------+
//| For the indicator to work, place the file SmoothAlgorithms.mqh      |
//| in the directory: terminal_data_folder\MQL5\Include                 |
//+---------------------------------------------------------------------+
#property copyright "Copyright © 2011, Nikolay Kositsin"
#property link "farria@mail.redcom.ru"
#property description "Mass Index"
//---- indicator version
#property version   "1.00"
//---- drawing the indicator in a separate window
#property indicator_separate_window
//---- number of indicator buffers
#property indicator_buffers 9 
//---- seven plots are used
#property indicator_plots   7
//+-----------------------------------+
//|  Indicator 1 drawing parameters   |
//+-----------------------------------+
//---- drawing the indicator as a color histogram
#property indicator_type1   DRAW_COLOR_HISTOGRAM
//---- the following colors are used in the histogram
#property indicator_color1 Gray,Green,Magenta
//---- the indicator line is a continuous curve
#property indicator_style1  STYLE_SOLID
//---- the width of the indicator line is 3
#property indicator_width1  3
//---- displaying the indicator label
#property indicator_label1  "Mass Index"
//+-----------------------------------+
//|  Indicator 2 drawing parameters   |
//+-----------------------------------+
//---- drawing the indicator as a three color line
#property indicator_type2   DRAW_COLOR_LINE
//---- the following colors are used for the indicator line
#property indicator_color2 Gray,Blue,DarkOrange
//---- the indicator line is a stroke
#property indicator_style2  STYLE_DASH
//---- indicator line width is equal to 2
#property indicator_width2  2
//---- displaying the indicator label
#property indicator_label2  "Signal line"
//+--------------------------------------------+
//|  BB levels indicator drawing parameters    |
//+--------------------------------------------+
//---- drawing Bollinger Bands as lines
#property indicator_type3   DRAW_LINE
#property indicator_type4   DRAW_LINE
#property indicator_type5   DRAW_LINE
#property indicator_type6   DRAW_LINE
#property indicator_type7   DRAW_LINE
//---- selection of Bollinger Bands colors
#property indicator_color3  Blue
#property indicator_color4  Red
#property indicator_color5  Gray
#property indicator_color6  Red
#property indicator_color7  Blue
//---- Bollinger Bands are dott-dash curves
#property indicator_style3 STYLE_DASHDOTDOT
#property indicator_style4 STYLE_DASHDOTDOT
#property indicator_style5 STYLE_DASHDOTDOT
#property indicator_style6 STYLE_DASHDOTDOT
#property indicator_style7 STYLE_DASHDOTDOT
//---- Bollinger Bands width is equal to 1
#property indicator_width3  1
#property indicator_width4  1
#property indicator_width5  1
#property indicator_width6  1
#property indicator_width7  1
//---- display of Bollinger Bands labels
#property indicator_label3  "+2Sigma"
#property indicator_label4  "+Sigma"
#property indicator_label5  "Middle"
#property indicator_label6  "-Sigma"
#property indicator_label7  "-2Sigma"
//+-------------------------------------------------+
//| Description of smoothing classes and indicators |
//+-------------------------------------------------+
#include <SmoothAlgorithms.mqh>
//+-----------------------------------+
//---- declaration of the CXMA and CStdDeviation classes variables from the SmoothAlgorithms.mqh file
CXMA XMA1,XMA2,XMA3,SUM,SIGN;
CStdDeviation STD;
//+-----------------------------------+
//|  Declaration of enumerations      |
//+-----------------------------------+
enum Applied_price_      // Type of constant
  {
   PRICE_CLOSE_ = 1,     // Close
   PRICE_OPEN_,          // Open
   PRICE_HIGH_,          // High
   PRICE_LOW_,           // Low
   PRICE_MEDIAN_,        // Median Price (HL/2)
   PRICE_TYPICAL_,       // Typical Price (HLC/3)
   PRICE_WEIGHTED_,      // Weighted Close (HLCC/4)
   PRICE_SIMPLE_,        // Simple Price (OC/2)
   PRICE_QUARTER_,       // Quarted Price (HLOC/4) 
   PRICE_TRENDFOLLOW0_,  // TrendFollow_1 Price 
   PRICE_TRENDFOLLOW1_   // TrendFollow_2 Price 
  };
enum IndStyle // The indicator display style
  {
   COLOR_LINE = DRAW_COLOR_LINE,          // Colored line
   COLOR_HISTOGRAM=DRAW_COLOR_HISTOGRAM,  // Colored histogram
   COLOR_ARROW=DRAW_COLOR_ARROW           // Colored labels
  };
/*enum Smooth_Method is declared in the SmoothAlgorithms.mqh file
  {
   MODE_SMA_,  // SMA
   MODE_EMA_,  // EMA
   MODE_SMMA_, // SMMA
   MODE_LWMA_, // LWMA
   MODE_JJMA,  // JJMA
   MODE_JurX,  // JurX
   MODE_ParMA, // ParMA
   MODE_T3,    // T3
   MODE_VIDYA, // VIDYA
   MODE_AMA,   // AMA
  }; */
//+-----------------------------------+
//|  Indicator input parameters       |
//+-----------------------------------+
input int APeriod=25;                       // Summing period
input Smooth_Method DSmoothMethod=MODE_EMA; // Indicator smoothing method
input int DPeriod=9;                        // Indicator smoothing period
input int DPhase=100;                       // Indicator smoothing parameter
input Smooth_Method SSmoothMethod=MODE_EMA; // Signal line smoothing method
input int SPeriod=7;                        // Signal line period
input int SPhase=100;                       // Signal line parameter
input int Shift=0;                          // Horizontal shift of the indicator in bars
input IndStyle Style=COLOR_HISTOGRAM;       // MI display style
input int BBPeriod=100;                     // Bollinger Bands period
input double BBDeviation1 = 1.0;            // Small deviation
input double BBDeviation2 = 1.6;            // Big deviation
//+-----------------------------------+
//---- declaration of dynamic arrays that further 
//---- will be used as indicator buffers
double MIBuffer[],XMIBuffer[];
double ColorMIBuffer[],ColorXMIBuffer[];
double ExtLineBuffer1[],ExtLineBuffer2[],ExtLineBuffer3[],ExtLineBuffer4[],ExtLineBuffer5[];
//---- declaration of the Bollinger Bands proportion variable
double quotient;
//---- declaration of the integer variables for the start of data calculation
int StartBars,StartBarsA,StartBarsD,StartBarsD2,StartBarsS,StartBarsB;
//+------------------------------------------------------------------+   
//| Mass Index indicator initialization function                     | 
//+------------------------------------------------------------------+ 
void OnInit()
  {
//---- initialization of variables of the start of data calculation
   StartBarsD=XMA1.GetStartBars(DSmoothMethod,DPeriod,DPhase);
   StartBarsA=2*StartBarsD+1+APeriod;
   StartBarsD2=2*StartBarsD+1;
   StartBarsS=StartBarsD2+XMA1.GetStartBars(SSmoothMethod,SPeriod,SPhase);
   StartBarsB=StartBarsD+BBPeriod;
   StartBars=MathMax(StartBarsS,StartBarsB);
//---- setting up alerts for invalid values of external variables
   XMA1.XMALengthCheck("DPeriod", DPeriod);
   XMA1.XMALengthCheck("SPeriod", SPeriod);
//---- setting up alerts for invalid values of external variables
   XMA1.XMAPhaseCheck("DPhase",DPhase,DSmoothMethod);
   XMA1.XMAPhaseCheck("SPhase",SPhase,SSmoothMethod);
//---- initialization of the Bollinger Bands proportion variable
   quotient=BBDeviation2/BBDeviation1;

//---- set MIBuffer[] dynamic array as an indicator buffer
   SetIndexBuffer(0,MIBuffer,INDICATOR_DATA);
//---- moving the indicator 1 horizontally
   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- performing the shift of the beginning of the indicator drawing
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartBarsA+1);
//---- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- changing of the indicator display style   
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,Style);

//---- set ColorMIBuffer[] dynamic array as an indicator buffer   
   SetIndexBuffer(1,ColorMIBuffer,INDICATOR_COLOR_INDEX);
//---- performing the shift of the beginning of the indicator drawing
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,StartBarsA+1);

//---- set XMIBuffer[] dynamic array as an indicator buffer
   SetIndexBuffer(2,XMIBuffer,INDICATOR_DATA);
//---- shifting the indicator 2 horizontally
   PlotIndexSetInteger(2,PLOT_SHIFT,Shift);
//---- performing the shift of the beginning of the indicator drawing
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,StartBarsA+1);
//---- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---- set ColorXMIBuffer[] dynamic array as an indicator buffer   
   SetIndexBuffer(3,ColorXMIBuffer,INDICATOR_COLOR_INDEX);
//---- performing the shift of the beginning of the indicator drawing
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,StartBars+1);

//---- set dynamic arrays as indicator buffers
   SetIndexBuffer(4,ExtLineBuffer1,INDICATOR_DATA);
   SetIndexBuffer(5,ExtLineBuffer2,INDICATOR_DATA);
   SetIndexBuffer(6,ExtLineBuffer3,INDICATOR_DATA);
   SetIndexBuffer(7,ExtLineBuffer4,INDICATOR_DATA);
   SetIndexBuffer(8,ExtLineBuffer5,INDICATOR_DATA);
//---- set the position, from which the Bollinger Bands drawing starts
   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,StartBars);
   PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,StartBars);
   PlotIndexSetInteger(6,PLOT_DRAW_BEGIN,StartBars);
   PlotIndexSetInteger(7,PLOT_DRAW_BEGIN,StartBars);
   PlotIndexSetInteger(8,PLOT_DRAW_BEGIN,StartBars);
//---- restriction to draw empty values for the indicator
   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(8,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---- initializations of a variable for the indicator short name
   string shortname,Smooth;
   Smooth=XMA1.GetString_MA_Method(DSmoothMethod);
   StringConcatenate(shortname,"Mass Index(",string(DPeriod),",",Smooth,")");
//---- creating a name for displaying in a separate sub-window and in a tooltip
   IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- determination of accuracy of displaying the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//---- initialization end
  }
//+------------------------------------------------------------------+ 
//| Mass Index iteration function                                    | 
//+------------------------------------------------------------------+ 
int OnCalculate(const int rates_total,    // number of bars in history at the current tick
                const int prev_calculated,// number of bars calculated at previous call
                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[])
  {
//---- checking the number of bars to be enough for the calculation
   if(rates_total<StartBars) return(0);
//---- declaration of variables with a floating point  
   double dprice_,x1xma,x2xma,sum,mi,xmi,bbxma,stdev1,stdev2;
//---- declaration of integer variables and getting calculated bars
   int first1,first2,first3,bar;
//---- calculation of the 'first' starting index for the bars recalculation loop
   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of the indicator calculation
     {
      first1=0; // starting index for calculation of all bars
      first2=StartBarsD+1;
      first3=StartBarsS+1;
     }
   else
     {
      first1=prev_calculated-1; // starting index for calculation of new bars
      first2=first1;
      first3=first1;
     }
//---- main indicator calculation loop
   for(bar=first1; bar<rates_total; bar++)
     {
      //---- call of the PriceSeries function to get incrementation of the input price dprice_
      dprice_=high[bar]-low[bar];
      //---- two calls of the XMASeries function.  
      x1xma=XMA1.XMASeries(0,prev_calculated,rates_total,DSmoothMethod,DPhase,DPeriod,dprice_,bar,false);
      sum=SUM.XMASeries(StartBarsD,prev_calculated,rates_total,MODE_SMA_,0,APeriod,x1xma,bar,false)*APeriod;
      x2xma=XMA2.XMASeries(StartBarsD,prev_calculated,rates_total,DSmoothMethod,DPhase,DPeriod,x1xma,bar,false);
      //---- preventing zero divide on empty values
      if(x2xma==0 || x2xma==EMPTY_VALUE) mi=EMPTY_VALUE;
      else                               mi=sum/x2xma;
      //---- loading the obtained value in the indicator buffer
      MIBuffer[bar]=mi;
      //---- signal line calculation
      xmi=SIGN.XMASeries(StartBarsA,prev_calculated,rates_total,SSmoothMethod,SPhase,SPeriod,mi,bar,false);
      //---- loading the obtained value in the indicator buffer
      MIBuffer[bar]=mi;
      XMIBuffer[bar]=xmi;
      //---- Bollinger Bands calculation 
      bbxma=XMA3.XMASeries(StartBarsA,prev_calculated,rates_total,MODE_SMA_,0,BBPeriod,mi,bar,false);
      stdev1=STD.StdDevSeries(StartBarsA,prev_calculated,rates_total,BBPeriod,BBDeviation1,xmi,bbxma,bar,false);
      stdev2=stdev1*quotient;
      //---- 
      if(bar<=StartBarsB+BBPeriod)
        {
         bbxma=EMPTY_VALUE;
         stdev1=0.0;
         stdev2=0.0;
        }

      ExtLineBuffer1[bar]=bbxma+stdev2;
      ExtLineBuffer2[bar]=bbxma+stdev1;
      ExtLineBuffer3[bar]=bbxma;
      ExtLineBuffer4[bar]=bbxma-stdev1;
      ExtLineBuffer5[bar]=bbxma-stdev2;
     }
//---- Bollinger Bands main calculation loop
   for(bar=first3; bar<rates_total; bar++)
     {

     }
//---- main cycle of the indicator coloring
   for(bar=first2; bar<rates_total; bar++)
     {
      ColorMIBuffer[bar]=0;

      if(MIBuffer[bar]>0)
        {
         if(MIBuffer[bar]>MIBuffer[bar-1]) ColorMIBuffer[bar]=1;
         if(MIBuffer[bar]<MIBuffer[bar-1]) ColorMIBuffer[bar]=2;
        }
     }
//---- main loop of the signal line coloring
   for(bar=first3; bar<rates_total; bar++)
     {
      ColorXMIBuffer[bar]=0;
      if(MIBuffer[bar]>XMIBuffer[bar-1]) ColorXMIBuffer[bar]=1;
      if(MIBuffer[bar]<XMIBuffer[bar-1]) ColorXMIBuffer[bar]=2;
     }
//----     
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---