//+------------------------------------------------------------------+ //| XD-RangeSwitch_HTF.mq5 | //| Copyright © 2014, Nikolay Kositsin | //| Khabarovsk, farria@mail.redcom.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2014, Nikolay Kositsin" #property link "farria@mail.redcom.ru" //--- indicator version #property version "1.60" #property description "The XD-RangeSwitch indicator with timeframe selection option available in input parameters" //--- drawing the indicator in the main window #property indicator_chart_window //--- number of indicator buffers 4 #property indicator_buffers 4 //--- four plots are used #property indicator_plots 4 //+----------------------------------------------+ //| declaration of constants | //+----------------------------------------------+ #define RESET 0 // A constant for returning the indicator recalculation command to the terminal #define INDICATOR_NAME " XD-RangeSwitch" // A constant for the indicator name #define SIZE 1 // A constant for the number of calls of the CountIndicator function in the code #define EMPTYVALUE EMPTY_VALUE // A constant for undisplayed indicator values //+----------------------------------------------+ //| Parameters of drawing a bullish indicator | //+----------------------------------------------+ //--- drawing indicator 1 as a line #property indicator_type1 DRAW_LINE //--- the Brown color is used as the color of the indicator bearish line #property indicator_color1 clrBrown //--- the line of the indicator 1 is a continuous curve #property indicator_style1 STYLE_SOLID //--- indicator 1 line width is equal to 2 #property indicator_width1 2 //--- display of the bearish indicator label #property indicator_label1 "Upper XD-RangeSwitch" //+----------------------------------------------+ //| Parameters of drawing a bearish indicator | //+----------------------------------------------+ //--- drawing indicator 2 as a line #property indicator_type2 DRAW_LINE //--- Teal color is used for the bullish line of the indicator #property indicator_color2 clrTeal //--- the line of the indicator 2 is a continuous curve #property indicator_style2 STYLE_SOLID //--- indicator 2 line width is equal to 2 #property indicator_width2 2 //--- display of the indicator bullish label #property indicator_label2 "Lower XD-RangeSwitch" //+----------------------------------------------+ //| Parameters of drawing a bullish indicator | //+----------------------------------------------+ //--- drawing the indicator 3 as a label #property indicator_type3 DRAW_ARROW //--- the Chocolate color is used for the indicator Sell #property indicator_color3 clrChocolate //--- the line of the indicator 3 is a continuous curve #property indicator_style3 STYLE_SOLID //--- indicator 3 line width is equal to 1 #property indicator_width3 1 //--- show the Sell label of the indicator #property indicator_label3 "Sell XD-RangeSwitch" //+----------------------------------------------+ //| Parameters of drawing a bearish indicator | //+----------------------------------------------+ //--- drawing the indicator 4 as a label #property indicator_type4 DRAW_ARROW //--- the CadetBlue color is used for the indicator Buy #property indicator_color4 clrCadetBlue //--- the line of the indicator 2 is a continuous curve #property indicator_style4 STYLE_SOLID //--- indicator 4 line width is equal to 1 #property indicator_width4 1 //--- show the Buy label of the indicator #property indicator_label4 "Buy XD-RangeSwitch" //+----------------------------------------------+ //| Indicator input parameters | //+----------------------------------------------+ input ENUM_TIMEFRAMES TimeFrame=PERIOD_H4; // Indicator chart period input uint N=4; // Number of peaks input int Shift=0; // Horizontal shift of the indicator in bars //+----------------------------------------------+ //--- declaration of dynamic arrays that //--- will be used as indicator buffers double Ind1Buffer[]; double Ind2Buffer[]; double Ind3Buffer[]; double Ind4Buffer[]; //--- declaration of integer variables for the start of data calculation int min_rates_total; //--- declaration of integer variables for the indicators handles int Ind_Handle; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- checking correctness of the chart periods if(!TimeFramesCheck(INDICATOR_NAME,TimeFrame)) return(INIT_FAILED); //--- initialization of variables min_rates_total=2; //--- getting the handle of the XD-RangeSwitch indicator Ind_Handle=iCustom(Symbol(),TimeFrame,"XD-RangeSwitch",N,0); if(Ind_Handle==INVALID_HANDLE) { Print(" Failed to get the handle of the XD-RangeSwitch indicator"); return(INIT_FAILED); } //--- Initialize indicator buffers IndInit(0,Ind1Buffer,EMPTYVALUE,min_rates_total,Shift); IndInit(1,Ind2Buffer,EMPTYVALUE,min_rates_total,Shift); IndInit(2,Ind3Buffer,EMPTYVALUE,min_rates_total,Shift); IndInit(3,Ind4Buffer,EMPTYVALUE,min_rates_total,Shift); //--- indicator symbol PlotIndexSetInteger(2,PLOT_ARROW,110); PlotIndexSetInteger(3,PLOT_ARROW,110); //--- creation of the name to be displayed in a separate sub-window and in a pop up help string shortname; StringConcatenate(shortname,INDICATOR_NAME,"(",EnumToString(TimeFrame),")"); IndicatorSetString(INDICATOR_SHORTNAME,shortname); //--- determining the accuracy of the indicator values IndicatorSetInteger(INDICATOR_DIGITS,_Digits); //--- initialization end return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, // number of bars in history at the current tick const int prev_calculated,// amount of history in bars at the previous tick 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 if the number of bars is 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 LastCountBar[Numb]=limit; } else limit=LastCountBar[Numb]+Rates_Total-Prev_Calculated; // Starting index for calculation of new bars //--- main indicator calculation loop for(int bar=limit; bar>=0 && !IsStopped(); bar--) { //--- reset the contents of the indicator buffers for calculation UpIndBuf[bar]=EMPTYVALUE; DnIndBuf[bar]=EMPTYVALUE; BuyIndBuf[bar]=EMPTYVALUE; SellIndBuf[bar]=EMPTYVALUE; //--- Copy new data to the IndTime array if(CopyTime(Symbol(),TFrame,iTime[bar],1,IndTime)<=0) return(RESET); if(iTime[bar]>=IndTime[0] && iTime[bar+1]