//+------------------------------------------------------------------+ //| ZigZag_NK_MTF.mq5 | //| Copyright © 2011, Nikolay Kositsin | //| Khabarovsk, farria@mail.redcom.ru | //+------------------------------------------------------------------+ //| Place the ZigZag_NK.mq5 file | //| to the terminal_data_folder\MQL5\Indicators | //+------------------------------------------------------------------+ #property copyright "Copyright © 2011, Nikolay Kositsin" #property link "farria@mail.redcom.ru" //---- indicator version #property version "1.00" //---- drawing the indicator in the main window #property indicator_chart_window //---- number of indicator buffers 2 #property indicator_buffers 2 //---- only one plot is used #property indicator_plots 1 //+----------------------------------------------+ //| Declaration of constants | //+----------------------------------------------+ #define RESET 0 // the constant for getting the command for the indicator recalculation back to the terminal #define INDICATOR_NAME "ZigZag NK MTF" // the constant for the indicator name //+----------------------------------------------+ //| Indicator drawing parameters | //+----------------------------------------------+ //---- two buffers are used for calculation and drawing the indicator #property indicator_buffers 2 //---- only one plot is used #property indicator_plots 1 //+----------------------------------------------+ //| Upper indicator drawing parameters | //+----------------------------------------------+ //---- drawing the indicator 1 as a zigzag #property indicator_type1 DRAW_ZIGZAG //---- red color is used for the indicator line #property indicator_color1 Red //---- thickness of line of the indicator 1 is equal to 1 #property indicator_width1 1 //---- displaying the indicator label #property indicator_label1 INDICATOR_NAME //+----------------------------------------------+ //| Lower indicator drawing parameters | //+----------------------------------------------+ //---- drawing the indicator 2 as a zigzag #property indicator_type2 DRAW_ZIGZAG //---- red color is used for the indicator line #property indicator_color2 Red //---- indicator 2 line width is equal to 1 #property indicator_width2 1 //---- displaying the indicator label #property indicator_label2 INDICATOR_NAME //+----------------------------------------------+ //| Indicator input parameters | //+----------------------------------------------+ input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4; // Chart period input int ExtDepth=12; input int ExtDeviation=5; input int ExtBackstep=3; //+----------------------------------------------+ //---- declaration of dynamic arrays that //---- will be used as indicator buffers double HighBuffer[]; double LowBuffer[]; //---- declaration of global variables bool Init; //---- declaration of the integer variables for the start of data calculation int min_rates_total,LimitShift; //---- declaration of integer variables for the indicators handles int ZigZag_Handle; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { Init=true; //---- checking correctness of the chart periods if(TimeFrame=end; bar--) if(Array[bar]>=Value) return(bar); //---- return(ArrayMaximum(Array,end,start-end)); } //+--------------------------------------------------------------------+ //| Searching for the first extremum according to its time series value| //+--------------------------------------------------------------------+ int FindMin(double Value,int start,int end,const double &Array[]) { //---- for(int bar=start; bar>=end; bar--) if(Array[bar]<=Value) return(bar); //---- return(ArrayMinimum(Array,end,start-end)); } //+------------------------------------------------------------------+ //| Custom 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 datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //---- 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 { limit=rates_total-min_rates_total-1; // starting index for calculation of all bars } else limit=LimitShift+rates_total-prev_calculated; // starting index for calculation of new bars //---- indexing elements in arrays as time series ArraySetAsSeries(time,true); ArraySetAsSeries(high,true); ArraySetAsSeries(low,true); //---- zero out the contents of the indicator buffers for calculation for(bar=limit; bar>=0 && !IsStopped(); bar--) { LowBuffer[bar]=0.0; HighBuffer[bar]=0.0; } //---- main indicator calculation loop for(bar=limit; bar>=0 && !IsStopped(); bar--) { //--- copy newly appeared data in the arrays if(CopyTime(NULL,TimeFrame,time[bar],1,ZigZagTime)<=0) return(RESET); if(time[bar+1]=ZigZagTime[0]) { //--- copy newly appeared data in the arrays if(CopyBuffer(ZigZag_Handle,1,time[bar],1,UpZigZag)<=0) return(RESET); if(CopyBuffer(ZigZag_Handle,0,time[bar],1,DnZigZag)<=0) return(RESET); if(UpZigZag[0]!=EMPTY_VALUE && UpZigZag[0]) { int maxbar=FindMax(UpZigZag[0],bar,0,high); HighBuffer[maxbar]=UpZigZag[0]; } if(DnZigZag[0]!=EMPTY_VALUE && DnZigZag[0]) { int minbar=FindMin(DnZigZag[0],bar,0,low); LowBuffer[minbar]=DnZigZag[0]; } } } //---- return(rates_total); } //+------------------------------------------------------------------+