//+------------------------------------------------------------------+ //| Counter_Trend_test2.mq5| //| Counter Trend test2 Copyright 2015, fxborg | //| http://fxborg-labo.hateblo.jp/ | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, fxborg" #property link "http://fxborg-labo.hateblo.jp/" #property version "1.0" #include #define PIP ((_Digits <= 3) ? 0.01 : 0.0001) #property indicator_chart_window #property indicator_buffers 10 #property indicator_plots 2 #property indicator_chart_window #property indicator_type1 DRAW_LINE #property indicator_type2 DRAW_LINE #property indicator_type3 DRAW_LINE #property indicator_type4 DRAW_LINE #property indicator_type5 DRAW_LINE #property indicator_color1 clrDodgerBlue #property indicator_color2 clrRed #property indicator_color3 clrDodgerBlue #property indicator_color4 clrRed #property indicator_color5 clrLimeGreen #property indicator_width1 1 #property indicator_width2 1 //--- input parameters input int SlowPeriod=25; // MaPeriod input int FastPeriod=8; // MaPeriod int MiniPeriod=4; // MaPeriod input ENUM_MA_METHOD MaMethod=MODE_SMMA; // Ma Method input ENUM_APPLIED_PRICE MaPriceMode=PRICE_TYPICAL; // Ma Price Mode input int InpHiLoPeriod=25; // High & Low Period input double InpStdDev=1.2;// Angle StdDev double JointPip=3*PIP; //+------------------------------------------------------------------+ int CalcMinPeriod=int(InpHiLoPeriod*0.3); //--- int min_rates_total; double MiniMaBuffer[]; double FastMaBuffer[]; double SlowMaBuffer[]; //--- indicator buffers double FlagH_Buffer[]; double FlagL_Buffer[]; //---- for calc double HighesBuffer[]; double LowesBuffer[]; double SigH_Buffer[]; double SigL_Buffer[]; double PriceBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //---- Initialization of variables of data calculation starting point min_rates_total=InpHiLoPeriod+5; if(MiniPeriod>=FastPeriod) { Alert("Fast Period is too Small"); return(INIT_FAILED); } if(FastPeriod>=SlowPeriod) { Alert("Slow Period is too Small"); return(INIT_FAILED); } //--- indicator buffers SetIndexBuffer(6,P4aBuffer,INDICATOR_DATA); SetIndexBuffer(0,FlagH_Buffer,INDICATOR_DATA); SetIndexBuffer(1,FlagL_Buffer,INDICATOR_DATA); SetIndexBuffer(2,MiniMaBuffer,INDICATOR_DATA); SetIndexBuffer(3,FastMaBuffer,INDICATOR_DATA); SetIndexBuffer(4,SlowMaBuffer,INDICATOR_DATA); //--- calc buffers SetIndexBuffer(5,SigH_Buffer,INDICATOR_CALCULATIONS); SetIndexBuffer(6,SigL_Buffer,INDICATOR_CALCULATIONS); SetIndexBuffer(7,HighesBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(8,LowesBuffer,INDICATOR_CALCULATIONS); SetIndexBuffer(9,PriceBuffer,INDICATOR_CALCULATIONS); PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE); PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(8,PLOT_EMPTY_VALUE,0); PlotIndexSetDouble(9,PLOT_EMPTY_VALUE,0); //--- string short_name="Counter Trend test2"; IndicatorSetString(INDICATOR_SHORTNAME,short_name); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator 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[]) { //--- int i,j,first; //--- check for bars count if(rates_total<=min_rates_total) return(0); //+----------------------------------------------------+ //|Set High Low Buffeer | //+----------------------------------------------------+ first=SlowPeriod+1; if(first+1SlowMaBuffer[i]); bool isDown=(MathMax(MiniMaBuffer[i],FastMaBuffer[i])MiniMaBuffer[i]) { SigH_Buffer[i]=high[i]; } FlagL_Buffer[i]=EMPTY_VALUE; if(isDown && FastMaBuffer[i]low[i-j]){ dmin=low[i-j];btm_pos=j;} //--- double max_cl = MathMax(MathMax(close[i-1],close[i-2]),close[i-3]); double min_cl = MathMin(MathMin(close[i-1],close[i-2]),close[i-3]); if(top_pos>CalcMinPeriod) { if(FlagH_Buffer[i-1]==EMPTY_VALUE || (FlagH_Buffer[i-1]!=EMPTY_VALUE &&FlagH_Buffer[i-1]JointPip) FlagH_Buffer[i-top_pos]=EMPTY_VALUE; } } } if(btm_pos>CalcMinPeriod) { if(FlagL_Buffer[i-1]==EMPTY_VALUE || (FlagL_Buffer[i-1]!=EMPTY_VALUE &&FlagL_Buffer[i-1]>max_cl)) { double angle=calc_low_line(low,btm_pos-1,i); if(angle!=EMPTY_VALUE) { is_updateL=true; for(j=0;j<=btm_pos;j++) FlagL_Buffer[i-(btm_pos-j)]=low[i-btm_pos]+(angle*j); if(MathAbs(FlagL_Buffer[i-btm_pos-1]-FlagL_Buffer[i-btm_pos])>JointPip) FlagL_Buffer[i-btm_pos]=EMPTY_VALUE; } } } } if(!is_updateH && FlagH_Buffer[i-1]!=EMPTY_VALUE) { FlagH_Buffer[i]=FlagH_Buffer[i-1]*2-FlagH_Buffer[i-2]; } if(!is_updateL && FlagL_Buffer[i-1]!=EMPTY_VALUE) { FlagL_Buffer[i]=FlagL_Buffer[i-1]*2-FlagL_Buffer[i-2]; } if((SlowMaBuffer[i-3]FastMaBuffer[i-2]) || (SlowMaBuffer[i-3]>FastMaBuffer[i-3] && SlowMaBuffer[i-2]FlagL_Buffer[i])FlagL_Buffer[i]=EMPTY_VALUE; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ double calc_high_Line(const double &high[],const int from_pos,const int i) { //--- double result=EMPTY_VALUE; int j; int cnt=from_pos; double angles[][2]; ArrayResize(angles,cnt); int n=0; for(j=from_pos-1;j>=0;j--) { int pos=i-j-1; angles[n][0]=(high[i-from_pos-1]-high[pos])/((i-from_pos-1)-pos); angles[n][1]=pos; n++; } //--- ArraySort(angles); //--- calc mean double sum=0.0; for(j=0; j=0;j--) { int pos=i-j-1; angles[n][0]=(low[i-from_pos-1]-low[pos])/((i-from_pos-1)-pos); angles[n][1]=pos; n++; } //--- ArraySort(angles); //--- calc mean double sum=0.0; for(j=0; j=0;j--) if(angles[j][0]>=mean-StdDev) result=angles[j][0]; return result; } //+------------------------------------------------------------------+