//+------------------------------------------------------------------+ //| iFreeNumFractals.mq4 | //| Copyright 2016, Artem A. Trishkin, Skype artmedia70 | //| https://login.mql5.com/ru/users/artmedia70 | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, Artem A. Trishkin, Skype artmedia70" #property link "https://login.mql5.com/ru/users/artmedia70" #property version "2.00" #property strict #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 2 //--- plot UpperFractal #property indicator_label1 "Upper Fractal" #property indicator_type1 DRAW_ARROW #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot LowerFractal #property indicator_label2 "Lower Fractal" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrSteelBlue #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- input parameters input int LeftNumUp=2; // The number of bars on the left for upper fractals int leftNumUp; // Количество баров слева для верхнего фрактала input int RightNumUp=2; // The number of bars on the right for upper fractals int rightNumUp; // Количество баров справа для верхнего фрактала //--- input int LeftNumDn=2; // The number of bars on the left for lower fractals int leftNumDn; // Количество баров слева для нижнего фрактала input int RightNumDn=2; // The number of bars on the right for lower fractals int rightNumDn; // Количество баров справа для нижнего фрактала //--- indicator buffers double BufferUpperFractal[]; double BufferLowerFractal[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,BufferUpperFractal); SetIndexBuffer(1,BufferLowerFractal); //--- setting a code from the Wingdings charset as the property of PLOT_ARROW PlotIndexSetInteger(0,PLOT_ARROW,217); PlotIndexSetInteger(1,PLOT_ARROW,218); SetIndexArrow(0,217); SetIndexArrow(1,218); SetIndexEmptyValue(0,EMPTY_VALUE); SetIndexEmptyValue(1,EMPTY_VALUE); //--- leftNumUp=(LeftNumUp<1?1:LeftNumUp); rightNumUp=(RightNumUp<1?1:RightNumUp); leftNumDn=(LeftNumDn<1?1:LeftNumDn); rightNumDn=(RightNumDn<1?1:RightNumDn); string short_name=MQLInfoString(MQL_PROGRAM_NAME)+"("+(string)leftNumUp+","+(string)rightNumUp+")("+(string)leftNumDn+","+(string)rightNumDn+")"; IndicatorSetString(INDICATOR_SHORTNAME,short_name); IndicatorSetInteger(INDICATOR_DIGITS,Digits()); //--- 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[]) { //--- if(rates_total0) { ArrayInitialize(BufferUpperFractal,0.0); ArrayInitialize(BufferUpperFractal,0.0); limit=rates_total-fmax(leftNumUp,leftNumDn)-1; } //--- for(int i=limit; i>fmin(rightNumUp,rightNumDn); i--) { if(GetFreeUpperFractal(Symbol(),PERIOD_CURRENT,i,leftNumUp,rightNumUp)>0) BufferUpperFractal[i]=high[i]; if(GetFreeLowerFractal(Symbol(),PERIOD_CURRENT,i,leftNumDn,rightNumDn)>0) BufferLowerFractal[i]=low[i]; } //--- return value of prev_calculated for next call return(rates_total); } //+----------------------------------------------------------------------------+ double GetFreeLowerFractal(const string symbol_name,ENUM_TIMEFRAMES timeframe,int shift,int left_dimension=2,int right_dimension=2) { int bars=Bars(symbol_name,timeframe); if(left_dimension<1) left_dimension=1; if(right_dimension<1) right_dimension=1; if(shift-right_dimension<1 || shift+left_dimension>bars-1) return(-1); for(int i=shift-1; i>=shift-right_dimension; i--) if(GetPriceLow(symbol_name,timeframe,i)bars-1) return(-1); for(int i=shift-1; i>=shift-right_dimension; i--) if(GetPriceHigh(symbol_name,timeframe,i)>GetPriceHigh(symbol_name,timeframe,i+1)) return(-1); for(int i=shift+1; i<=shift+left_dimension; i++) if(GetPriceHigh(symbol_name,timeframe,i)>GetPriceHigh(symbol_name,timeframe,i-1)) return(-1); return(GetPriceHigh(symbol_name,timeframe,shift)); } //+----------------------------------------------------------------------------+ double GetPriceHigh(const string symbol_name, ENUM_TIMEFRAMES timeframe, int shift){ double array[1]; if(CopyHigh(symbol_name,timeframe,shift,1,array)==1) return(array[0]); return(-1); } //+----------------------------------------------------------------------------+ double GetPriceLow(const string symbol_name, ENUM_TIMEFRAMES timeframe, int shift){ double array[1]; if(CopyLow(symbol_name,timeframe,shift,1,array)==1) return(array[0]); return(-1); } //+----------------------------------------------------------------------------+