//+------------------------------------------------------------------+ //| KG Support & Resistance.mq4 | //| Copyright © 2007, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ //***modified by silv3r4sh //Edited by NanningBob Feb. 2010 //Edited by NanningBob Nov. 2011 /* Modified by Squalou: * 2011.10.14: added selectable timeframe inputs; * 2011.10.17: added Num_Nearest_**_SR inputs to display the SR lines that are the nearest to the current price; * added DaysBack input (replaces previous BarsBack input): will go back this amount in the past to search for SR lines; * * This indicator is for use in NanningBob's 10.0 and 10.1 Trend Trading System at http://www.forexfactory.com/showthread.php?t=308304 */ #property copyright "Kang_Gun" #property link "http://www.free-knowledge.com" #property indicator_chart_window #property indicator_buffers 8 //***modified by silv3r4sh to hide all other support and resistance lines of other timeframes #property indicator_color1 DimGray //M15 #property indicator_color2 DimGray //M15 #property indicator_color3 DimGray //H1 #property indicator_color4 DimGray //H1 #property indicator_color5 OrangeRed//H4 #property indicator_color6 OrangeRed//H4 #property indicator_color7 Navy //D1 #property indicator_color8 Navy //D1 //***modified by silv3r4sh to increase size of dotted line #property indicator_width1 0 #property indicator_width2 0 #property indicator_width3 1 #property indicator_width4 1 #property indicator_width5 2 #property indicator_width6 2 #property indicator_width7 2 #property indicator_width8 2 //---- input parameters extern bool Show_D1_SR = true; extern int Num_Nearest_D1_SR = 2; // number of nearest D1 SR lines to extend to "now" extern bool Show_H4_SR = true; extern int Num_Nearest_H4_SR = 0; // number of nearest H4 SR lines to extend to "now" extern bool Show_H1_SR = false; extern int Num_Nearest_H1_SR = 0; // number of nearest H1 SR lines to extend to "now" extern bool Show_M15_SR = false; extern int Num_Nearest_M15_SR = 0; // number of nearest M15 SR lines to extend to "now" extern color SR_Color_Above = Black; extern color SR_Color_Below = Red; int BarsBack = 10000; extern int DaysBack = 60; string prefix; //---- buffers double M15_Resistance[]; double M15_Support[]; double H1_Resistance[]; double H1_Support[]; double H4_Resistance[]; double H4_Support[]; double D1_Resistance[]; double D1_Support[]; // list of SR levels double M15_SR[1]; double H1_SR[1]; double H4_SR[1]; double D1_SR[1]; datetime lasttime; //+------------------------------------------------------------------+ int init() //+------------------------------------------------------------------+ { //---- indicators int b=-1; b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,M15_Resistance); SetIndexLabel(b,"M15 Resistance"); SetIndexArrow(b, 158); b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,M15_Support); SetIndexLabel(b,"M15 Support"); SetIndexArrow(b, 158); b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,H1_Resistance); SetIndexLabel(b,"H1 Resistance"); SetIndexArrow(b, 158); b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,H1_Support); SetIndexLabel(b,"H1 Support"); SetIndexArrow(b, 158); b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,H4_Resistance); SetIndexLabel(b,"H4 Resistance"); SetIndexArrow(b, 158); b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,H4_Support); SetIndexLabel(b,"H4 Support"); SetIndexArrow(b, 158); b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,D1_Resistance); SetIndexLabel(b,"D1 Resistance"); SetIndexArrow(b, 158); b++;SetIndexStyle(b,DRAW_ARROW); SetIndexBuffer(b,D1_Support); SetIndexLabel(b,"D1 Support"); SetIndexArrow(b, 158); //---- BarsBack = iBarShift(NULL,0,iTime(NULL,PERIOD_D1,0) - DaysBack*PERIOD_D1*60); prefix = WindowExpertName(); RemoveObjects(prefix); return(0); } //+------------------------------------------------------------------+ int deinit() //+------------------------------------------------------------------+ { RemoveObjects(prefix); return(0); } //------------------------------------------------------------------ double Fractal (int mode, int period, int shift) //------------------------------------------------------------------ { if (Period()>period) return(0); period = period/Period()*2+MathCeil(period/Period()/2); if (shiftBars-period) return(0); for (int i=1;i<=period;i++) { if (mode==MODE_UPPER) { if (High[shift+i]>High[shift])return(0); if (High[shift-i]>=High[shift])return(0); } else { if (Low[shift+i] 0) counted_bars--; int limit = Bars - counted_bars; if (BarsBack>0 && limit>BarsBack) limit=BarsBack; if (Show_M15_SR) calc_SR(limit, PERIOD_M15,M15_Resistance,M15_Support,M15_SR); if (Show_H1_SR) calc_SR(limit, PERIOD_H1,H1_Resistance,H1_Support,H1_SR); if (Show_H4_SR) calc_SR(limit, PERIOD_H4,H4_Resistance,H4_Support,H4_SR); if (Show_D1_SR) calc_SR(limit, PERIOD_D1,D1_Resistance,D1_Support,D1_SR); } //------------------------------------------------------------------ int calc_SR(int limit, int tf, double &Resistance[], double &Support[], double &SR[]) //------------------------------------------------------------------ { // calc the lower limit bar for which a Fractal can be found int min_bar = tf/Period()*2+MathCeil(tf/Period()/2); limit = MathMax(limit,min_bar+1); for(int i = limit; i>min_bar; i--) { if (Fractal(MODE_UPPER,tf,i)>0) { if (Resistance[i]!=High[i]) add_SR(SR,High[i],i); Resistance[i] = High[i]; } else Resistance[i] = Resistance[i+1]; if (Fractal(MODE_LOWER,tf,i)>0) { if (Support[i]!=Low[i]) add_SR(SR,Low[i],i); Support[i]=Low[i]; } else Support[i] = Support[i+1]; } // the remaining bars until "now" cannot contain new fractals, // so simply fill-in the rest of the Buffers with the most recent S/R levels for(; i>=0; i--) { Resistance[i] = Resistance[i+1]; Support[i] = Support[i+1]; } // display the SR lines that are nearest to the current Price if (Show_M15_SR && Num_Nearest_M15_SR>0) show_nearest_SR(Num_Nearest_M15_SR, "M15", M15_SR); if (Show_H1_SR && Num_Nearest_H1_SR>0) show_nearest_SR(Num_Nearest_H1_SR, "H1___", H1_SR); if (Show_H4_SR && Num_Nearest_H4_SR>0) show_nearest_SR(Num_Nearest_H4_SR, "H4______", H4_SR); if (Show_D1_SR && Num_Nearest_D1_SR>0) show_nearest_SR(Num_Nearest_D1_SR, "D1_________", D1_SR); return(0); } //------------------------------------------------------------------ int add_SR(double &array[], double SR_level, int i) //------------------------------------------------------------------ { //grow array to store the next element int size = ArraySize(array); //Print("Adding SR "+SR_level+" bar "+i+"("+size+")"); ArrayResize(array,size+1); array[size] = SR_level; // keep array sorted to easily find the SR levels that are nearest to price (array[0] is not used) ArraySort(array,size,1,MODE_ASCEND); } //-------------------------------------------------------------------------------------- int show_nearest_SR(int n, string name, double SR_array[]) //-------------------------------------------------------------------------------------- { // find the nearest N lines above and below current price, // and extend their levels to show on "today's" candles int size,i,k; double price = Close[0]; size = ArraySize(SR_array); for (i=1; i= price) break; // found the first SR line above current price } for (k=0; k