/* * For the indicator to work, place the * SmoothAlgorithms.mqh * in the directory: MetaTrader\\MQL5\Include */ //+------------------------------------------------------------------+ //| DigVariation.mq5 | //| Copyright © 2010, LeMan. | //| b-market@mail.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, LeMan." #property link "b-market@mail.ru" //---- indicator version number #property version "1.00" //---- drawing indicator in a separate window #property indicator_separate_window //---- number of indicator buffers 2 #property indicator_buffers 2 //---- only one plot is used #property indicator_plots 1 //+-----------------------------------+ //| Parameters of indicator drawing | //+-----------------------------------+ //---- drawing the indicator as a line #property indicator_type1 DRAW_LINE //---- HotPink color is used for the indicator line #property indicator_color1 clrHotPink //---- the indicator line is a continuous curve #property indicator_style1 STYLE_SOLID //---- Indicator line width is equal to 2 #property indicator_width1 2 //---- displaying the indicator label #property indicator_label1 "Variation" //+----------------------------------------------+ //| Parameters of displaying horizontal levels | //+----------------------------------------------+ #property indicator_level1 0.0 #property indicator_levelcolor clrGreen #property indicator_levelstyle STYLE_SOLID //+----------------------------------------------+ //| Declaration of enumerations | //+----------------------------------------------+ enum Smooth //Type of constant { dig_0=0,//0 dig_1, //1 dig_2, //2 dig_3, //3 dig_4, //4 dig_5, //5 dig_6, //6 dig_7, //7 dig_8, //8 dig_9, //9 dig_10 //10 }; //+----------------------------------------------+ //| Indicator input parameters | //+----------------------------------------------+ input int Period_=12; //period of averaging input ENUM_MA_METHOD MA_Method_=MODE_SMA; //method of averaging input Smooth SmoothPower=dig_1; //power of the signal smoothing input int Shift=0; // horizontal shift of the indicator in bars //+-----------------------------------+ //---- indicator buffers double ExtMapBuffer[]; double ExtCalcBuffer[]; int min_rates_total=0; //+------------------------------------------------------------------+ // Moving_Average class description | //+------------------------------------------------------------------+ #include //+------------------------------------------------------------------+ //| Variation indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //---- declaration of the CXMA class variables from the SmoothAlgorithms.mqh file CXMA XMA; //---- Initialization of variables of the start of data calculation min_rates_total=XMA.GetStartBars(Smooth_Method(MA_Method_),Period_,0)*2+52; //---- set dynamic array as an indicator buffer SetIndexBuffer(0,ExtMapBuffer,INDICATOR_DATA); //---- moving the indicator 1 horizontally PlotIndexSetInteger(0,PLOT_SHIFT,Shift); //---- performing the shift of beginning of indicator drawing PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total); //---- setting the indicator values that won't be visible on a chart PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); //--- create a label to display in DataWindow PlotIndexSetString(0,PLOT_LABEL,"Variation"); //---- set ExtCalcBuffer[] dynamic array as an indicator buffer SetIndexBuffer(1,ExtCalcBuffer,INDICATOR_CALCULATIONS); //---- initializations of variable for indicator short name string shortname; StringConcatenate(shortname,"Variation( Period_ = ",Period_,", MA_Method_ = ",MA_Method_,")"); //--- creation of the name to be displayed in a separate sub-window and in a pop up help IndicatorSetString(INDICATOR_SHORTNAME,shortname); //---- determination of accuracy of displaying the indicator values IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); //---- end of initialization } //+------------------------------------------------------------------+ //| Variation iteration function | //+------------------------------------------------------------------+ int OnCalculate ( const int rates_total,// number of bars in history at the current tick const int prev_calculated,// amount of history in bars at the previous tick const int begin,// number of beginning of reliable counting of bars const double &price[]// price array for calculation of the indicator ) { //---- min_rates_total+=begin; //---- Checking the number of bars to be enough for calculation if(rates_totalrates_total || prev_calculated<=0)// checking for the first start of calculation of an indicator { first=begin; // starting number for calculation of all bars //---- Initialization of variables of the start of data calculation start1=begin; if(MA_Method_!=MODE_EMA) start2=Period_+begin; else start2=begin; //--- increase the position of the start of data by 'begin' bars // as a result of calculation on the basis of data of another indicator if(begin>0) PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total); } else first=prev_calculated-1; // starting number for calculation of new bars //---- declaration of the Moving_Average class variables from the SmoothAlgorithms.mqh file static CMoving_Average MA,VR; //---- Main cycle of calculation of the indicator for(bar=first; bar=first) ExtCalcBuffer[bar]=1000*(price[bar]-(ma+vr)); else ExtCalcBuffer[bar]=EMPTY_VALUE; } if(prev_calculated>rates_total || prev_calculated<=0) first=min_rates_total; //---- Main cycle of smoothing of the indicator for(bar=first; bar