//+------------------------------------------------------------------+ //| ColorHMA.mq5 | //| Copyright © 2010, Nikolay Kositsin | //| Khabarovsk, farria@mail.redcom.ru | //+------------------------------------------------------------------+ //| Place the SmoothAlgorithms.mqh file | //| to the directory: terminal_data_folder\\MQL5\Include | //+------------------------------------------------------------------+ #property copyright "2010, Nikolay Kositsin" #property link "farria@mail.redcom.ru" #property version "1.00" //---- drawing the indicator in the main window #property indicator_chart_window //---- two buffers are used for calculation and drawing the indicator #property indicator_buffers 2 //---- only one plot is used #property indicator_plots 1 //---- drawing the indicator as a line #property indicator_type1 DRAW_COLOR_LINE //---- Gray, MediumPurple and Red colors are used for three-color line #property indicator_color1 Gray,MediumPurple,Red //---- the indicator line is a continuous curve #property indicator_style1 STYLE_SOLID //---- indicator line width is equal to 1 #property indicator_width1 2 //+-----------------------------------+ //| Indicator input parameters | //+-----------------------------------+ input int HMA_Period=13; // Moving average period input int HMA_Shift=0; // Horizontal shift of the average in bars //+-----------------------------------+ //---- declaration of dynamic arrays that //---- will be used as indicator buffers double ExtLineBuffer[]; double ColorExtLineBuffer[]; //---- declaration of integer variables int Hma2_Period,Sqrt_Period; //---- declaration of the integer variables for the start of data calculation int min_rates_total; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { Hma2_Period=int(MathFloor(HMA_Period/2)); Sqrt_Period=int(MathFloor(MathSqrt(HMA_Period))); //---- initialization of variables of the start of data calculation min_rates_total=HMA_Period+Sqrt_Period; //---- setting the indicator values that won't be visible on a chart PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); //---- set ExtLineBuffer [] dynamic array as an indicator buffer SetIndexBuffer(0,ExtLineBuffer,INDICATOR_DATA); //---- shifting the moving average horizontally by HMAShift PlotIndexSetInteger(0,PLOT_SHIFT,HMA_Shift); //---- shifting the start of drawing of the HMAPeriod indicator PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total); //---- set ColorExtLineBuffer[] dynamic array as an indicator buffer SetIndexBuffer(1,ColorExtLineBuffer,INDICATOR_COLOR_INDEX); //---- performing the shift of the beginning of the indicator drawing PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total+1); //---- restriction to draw empty values for the indicator PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0); //---- setting the format of accuracy of displaying the indicator IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); //---- name for the data window and the label for sub-windows string short_name="HMA"; IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(HMA_Period)+")"); //---- } //---- CMoving_Average class description #include //+------------------------------------------------------------------+ //| Custom indicator 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 int begin, // bars reliable counting beginning index const double &price[]) // price array for calculation of the indicator { int begin0=min_rates_total+begin; //---- checking the number of bars to be enough for the calculation if(rates_totalrates_total || prev_calculated<=0) // checking for the first start of the indicator calculation first=begin0; //---- main loop of the signal line coloring for(bar=first; barExtLineBuffer[bar]) ColorExtLineBuffer[bar]=2; } //---- return(rates_total); } //+------------------------------------------------------------------+