Indicatore MetaTrader 

//+------------------------------------------------------------------+ //| ATR Levels New.mq4 | //| Tamir Bleicher | //+------------------------------------------------------------------+ #property copyright "Tamir Bleicher" #property indicator_chart_window extern int ATRPeriod = 10; extern string TimeFrame = "D"; extern bool IgnoreSunday = true; extern int MinVolume = 1; extern color DynamicLevel = Teal; extern color StaticLevel=Indigo; extern color OpenLevel=Navy; extern int LevelBarDisplay=40; extern string Remark1 = "=========================================================================="; extern string Remark2 = "TimeFrames: MN-Monthly W-Weekly D-Daily H4-4 Hours H1- 1 Hour M30-30 Minutes M15-15 Minutes M5-5 Minutes M1-1 Minute =="; extern string Remark3 = "IgnoreSunday is relevant only for Daily levels"; extern string Remark4 = "MinVolue is the minimum volume to take in ATR calculation in order to skip holidays"; extern string Remark5 = "LevelBarDisplay is the line titles and value position in bars, counting backward from the last bar"; extern string Remark6 = "Levels are: Dynamic levels (support and resist), Static Levels (support and resist) and open period level"; double rates_d1[][6]; double Static_Resist, Dynamic_Resist, Static_Support, Dynamic_Support, atr; int periods, TF; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators //---- if (TimeFrame=="MN" || TimeFrame=="mn" || TimeFrame=="M" || TimeFrame=="m") TF=PERIOD_MN1; else if (TimeFrame=="W" || TimeFrame=="w") TF=PERIOD_W1; else if (TimeFrame=="D" || TimeFrame=="d") TF=PERIOD_D1; else if (TimeFrame=="H4" || TimeFrame=="h4") TF=PERIOD_H4; else if (TimeFrame=="H1" || TimeFrame=="h1") TF=PERIOD_H1; else if (TimeFrame=="M30" || TimeFrame=="m30") TF=PERIOD_M30; else if (TimeFrame=="M15" || TimeFrame=="m15") TF=PERIOD_M15; else if (TimeFrame=="M5" || TimeFrame=="m5") TF=PERIOD_M5; else if (TimeFrame=="M1" || TimeFrame=="m1") TF=PERIOD_M1; else TF=PERIOD_D1; return(0); } int deinit() { ObjectDelete("ATR Static Resist"); ObjectDelete("ATR Static Support"); ObjectDelete("ATR Static Support Label"); ObjectDelete("ATR Static Resist Label"); ObjectDelete("Period Open"); ObjectDelete("ATR Dynamic Resist"); ObjectDelete("ATR Dynamic Support"); ObjectDelete("ATR Dynamic Support Label"); ObjectDelete("ATR Dynamic Resist Label"); ObjectDelete("Period Open Label"); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int counted_bars=IndicatorCounted(); string Remark; int Yesterday; ArrayCopyRates(rates_d1, Symbol(), TF); atr = ATR(Symbol(), TF, ATRPeriod, 1, IgnoreSunday); switch(TF) { case PERIOD_MN1: Remark="(Monthly)"; break; case PERIOD_W1: Remark="(Weekly)"; break; case PERIOD_D1: if (IgnoreSunday) Remark="(Daily ignoring Sundays)"; else Remark="(Daily including Sundays)"; break; case PERIOD_H4: Remark="(4 Hours)"; break; case PERIOD_H1: Remark="(1 Hour)"; break; case PERIOD_M30: Remark="(30 Minutes)"; break; case PERIOD_M15: Remark="(15 Minutes)"; break; case PERIOD_M5: Remark="(5 Minutes)"; break; case PERIOD_M1: Remark="(1 Minute)"; break; } Comment("ATR (",ATRPeriod,") = ",atr," ",Remark); if (TF==PERIOD_D1 && IgnoreSunday && TimeDayOfWeek(iTime(NULL,TF,1))==0) Yesterday=2; else Yesterday=1; Static_Support = rates_d1[Yesterday][3] - atr; Static_Resist = rates_d1[Yesterday][2] + atr; Dynamic_Support = rates_d1[0][3] - atr; Dynamic_Resist = rates_d1[0][2] + atr; if (ObjectFind("ATR Static Resist") != 0) { ObjectCreate("ATR Static Resist",OBJ_HLINE,0,Time[0],Static_Resist); ObjectSet("ATR Static Resist",OBJPROP_COLOR,StaticLevel); ObjectSet("ATR Static Resist",OBJPROP_WIDTH,1); } else { ObjectMove("ATR Static Resist", 0,Time[0],Static_Resist); } if (ObjectFind("ATR Static Support") != 0) { ObjectCreate("ATR Static Support",OBJ_HLINE,0,Time[0],Static_Support); ObjectSet("ATR Static Support",OBJPROP_COLOR,StaticLevel); ObjectSet("ATR Static Support",OBJPROP_WIDTH,1); } else { ObjectMove("ATR Static Support", 0,Time[0],Static_Support); } if (ObjectFind("ATR Dynamic Resist") != 0) { ObjectCreate("ATR Dynamic Resist",OBJ_HLINE,0,Time[0],Dynamic_Resist); ObjectSet("ATR Dynamic Resist",OBJPROP_COLOR,DynamicLevel); ObjectSet("ATR Dynamic Resist",OBJPROP_WIDTH,1); } else { ObjectMove("ATR Dynamic Resist", 0,Time[0],Dynamic_Resist); } if (ObjectFind("ATR Dynamic Support") != 0) { ObjectCreate("ATR Dynamic Support",OBJ_HLINE,0,Time[0],Dynamic_Support); ObjectSet("ATR Dynamic Support",OBJPROP_COLOR,DynamicLevel); ObjectSet("ATR Dynamic Support",OBJPROP_WIDTH,1); } else { ObjectMove("ATR Dynamic Support", 0,Time[0],Dynamic_Support); } if (ObjectFind("Period Open") != 0) { ObjectCreate("Period Open",OBJ_HLINE,0,Time[LevelBarDisplay],iOpen(0,TF,0)); ObjectSet("Period Open",OBJPROP_COLOR,OpenLevel); ObjectSet("Period Open",OBJPROP_STYLE,STYLE_DOT); } else { ObjectMove("Period Open", 0,Time[LevelBarDisplay],iOpen(0,TF,0)); } if(ObjectFind("ATR Static Resist Label") != 0) { ObjectCreate("ATR Static Resist Label", OBJ_TEXT, 0, Time[LevelBarDisplay], Static_Resist); ObjectSetText("ATR Static Resist Label", "ATR Static res: " + DoubleToStr(Static_Resist,4), 8, "Verdana", Black); } else { ObjectMove("ATR Static Resist Label", 0, Time[LevelBarDisplay], Static_Resist); } if(ObjectFind("ATR Static Support Label") != 0) { ObjectCreate("ATR Static Support Label", OBJ_TEXT, 0, Time[LevelBarDisplay], Static_Support); ObjectSetText("ATR Static Support Label", "ATR Static sup: " + DoubleToStr(Static_Support,4), 8, "Verdana", Black); } else { ObjectMove("ATR Static Support Label", 0, Time[LevelBarDisplay], Static_Support); } if(ObjectFind("ATR Dynamic Resist Label") != 0) { ObjectCreate("ATR Dynamic Resist Label", OBJ_TEXT, 0, Time[LevelBarDisplay], Dynamic_Resist); ObjectSetText("ATR Dynamic Resist Label", "ATR Dynamic res: " + DoubleToStr(Dynamic_Resist,4), 8, "Verdana", Black); } else { ObjectMove("ATR Dynamic Resist Label", 0, Time[LevelBarDisplay], Dynamic_Resist); } if(ObjectFind("ATR Dynamic Support Label") != 0) { ObjectCreate("ATR Dynamic Support Label", OBJ_TEXT, 0, Time[LevelBarDisplay], Dynamic_Support); ObjectSetText("ATR Dynamic Support Label", "ATR Dynamic sup: " + DoubleToStr(Dynamic_Support,4), 8, "Verdana", Black); } else { ObjectMove("ATR Dynamic Support Label", 0, Time[LevelBarDisplay], Dynamic_Support); } if(ObjectFind("Period Open Label") != 0) { ObjectCreate("Period Open Label", OBJ_TEXT, 0, Time[LevelBarDisplay], iOpen(0,TF,0)); ObjectSetText("Period Open Label", "Period Open: " + DoubleToStr(iOpen(0,TF,0),4), 8, "Verdana", Black); } else { ObjectMove("Period Open Label", 0, Time[LevelBarDisplay], iOpen(0,TF,0)); } return(0); } //+------------------------------------------------------------------+ double ATR(string Sym, int TimeFrame, int Per, int Shift, bool Sunday_FLG) { int i,j, Today_DOW,Yesterday_DOW; double h,l,c,tr,tr_sum=0; bool Sunday_Case; if (TimeFrame!=PERIOD_D1 || Sunday_FLG==false) return(iATR(Sym, TimeFrame, Per, Shift)); else for (i=0, j=0; i<Per; i++, j++) { while(iVolume(Sym,TimeFrame,Shift+j)<=MinVolume) j++; Today_DOW=TimeDayOfWeek(iTime(Sym,TimeFrame,Shift+j)); Yesterday_DOW=TimeDayOfWeek(iTime(Sym,TimeFrame,Shift+j+1)); if (Today_DOW==1 && Yesterday_DOW==0) { Sunday_Case=true; c=iClose(Sym,TimeFrame,Shift+j+2); h=MathMax(iHigh(Sym,TimeFrame,Shift+j),iHigh(Sym,TimeFrame,Shift+j+1)); l=MathMin(iLow(Sym,TimeFrame,Shift+j),iLow(Sym,TimeFrame,Shift+j+1)); } else { Sunday_Case=false; c=iClose(Sym,TimeFrame,Shift+j+1); h=iHigh(Sym,TimeFrame,Shift+j); l=iLow(Sym,TimeFrame,Shift+j); } if (c>h) tr=c-l; else if(c<l) tr=h-c; else tr=h-l; tr_sum=tr_sum+tr; // Ignore days with no volume if (Sunday_Case) j++; } return(tr_sum/Per); }