//+------------------------------------------------------------------+ //| ClusterFilter.mq5 | //| Copyright 2013-2014, Gruzdev Konstantin | //| https://login.mql5.com/ru/users/Lizar | //+------------------------------------------------------------------+ #property copyright "Copyright 2013-2014, Gruzdev Konstantin" #property link "https://login.mql5.com/ru/users/Lizar" #property version "1.00" //--- indicator settings #property indicator_chart_window #property indicator_buffers 3 #property indicator_plots 3 //--- plot Label1 #property indicator_label1 "MA" #property indicator_type1 DRAW_LINE #property indicator_color1 clrBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot Label2 #property indicator_label2 "EMA" #property indicator_type2 DRAW_LINE #property indicator_color2 clrGreen #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot Label3 #property indicator_label3 "CF" #property indicator_type3 DRAW_LINE #property indicator_color3 clrRed #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- indicator buffers double ExtBuffer_MA[]; double ExtBuffer_EMA[]; double ExtBuffer_CF[]; //--- parameters MA int ext_period_MA = 2; //--- //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,ExtBuffer_MA,INDICATOR_DATA); SetIndexBuffer(1,ExtBuffer_EMA,INDICATOR_DATA); SetIndexBuffer(2,ExtBuffer_CF,INDICATOR_DATA); //--- sets first bar from what index will be drawn PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ext_period_MA); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ext_period_MA); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ext_period_MA); //--- set accuracy IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[]) { //--- CalculateSimpleMA(rates_total,prev_calculated,begin,price); CalculateEMA(rates_total,prev_calculated,begin,price); CalculateCF(rates_total,prev_calculated,begin,price); //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| simple moving average | //+------------------------------------------------------------------+ void CalculateSimpleMA(int rates_total,int prev_calculated,int begin,const double &price[]) { int i,limit; //--- first calculation or number of bars was changed if(prev_calculated==0)// first calculation { limit=ext_period_MA+begin; //--- set empty value for first limit bars for(i=0;iExtBuffer_CF[i-2]) { if(ExtBuffer_CF[i-1]ExtBuffer_EMA[i]) { ExtBuffer_CF[i]=ExtBuffer_MA[i]; continue; } if(ExtBuffer_CF[i-1]>ExtBuffer_MA[i] && ExtBuffer_CF[i-1]ExtBuffer_MA[i] && ExtBuffer_CF[i-1]>ExtBuffer_EMA[i]) { ExtBuffer_CF[i]=fmax(ExtBuffer_MA[i],ExtBuffer_EMA[i]); continue; } if(ExtBuffer_CF[i-1]>ExtBuffer_MA[i] && ExtBuffer_CF[i-1]ExtBuffer_EMA[i]) { ExtBuffer_CF[i]=ExtBuffer_EMA[i]; continue; } ExtBuffer_CF[i]=fmin(ExtBuffer_MA[i],ExtBuffer_EMA[i]); } } //--- } //+------------------------------------------------------------------+