//+------------------------------------------------------------------+
//|                                         ATRSupportResistance.mq4 |
//|                                  Copyright 2016, Khalil Abokwaik |
//|                             http://www.forexfactory.com/abokwaik |
//+------------------------------------------------------------------+
#property copyright     "Copyright 2016, Khalil Abokwaik"
#property link          "http://www.forexfactory.com/abokwaik"
#property description   "\nATR based Support and Resistance Lines"
#property description   "Developed by Khalil Abokwaik (abokwaik@yahoo.com)"
#property description   "Idea by http://www.forexfactory.com/goldthehun"
#property version       "1.00"
#property strict
#property indicator_chart_window
//--- input parameters
input color    SdayColor=clrGreen;//Daily Support Color
input color    RdayColor=clrOrange;//Daily Resistance Color
input int      dayWidth=1;//Daily Lines Width
input color    SweekColor=clrBlue;//Weekly Support Color
input color    RweekColor=clrRed;//Weekly Resistance Color
input int      weekWidth=2;//Weekly Lines Width
//+------------------------------------------------------------------+
datetime barTime=0;
string prefix;
double zvalues[5]={1, 1.28, 1.645, 1.96, 2.58};
double ds[5],dr[5],ws[5],wr[5];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   prefix="ASR_"+periodName(Period())+"_";
   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0,prefix);
   return;
}
//+------------------------------------------------------------------+
//| 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(Time[0]==barTime) return(rates_total);
   barTime=Time[0];
   double atr=iATR(Symbol(),PERIOD_M5,1440,1);
   for(int i=0;i<5;i++)
   {
      dr[i]=iOpen(Symbol(),PERIOD_D1,0)+(atr*zvalues[i]*MathSqrt(1440/5));
      ds[i]=iOpen(Symbol(),PERIOD_D1,0)-(atr*zvalues[i]*MathSqrt(1440/5));
      wr[i]=iOpen(Symbol(),PERIOD_W1,0)+(atr*zvalues[i]*MathSqrt(7200/5));
      ws[i]=iOpen(Symbol(),PERIOD_W1,0)-(atr*zvalues[i]*MathSqrt(7200/5));
   }
   ObjectsDeleteAll(0,prefix);
   for(int i=0;i<5;i++)
   {
      //-- Daily Support ------------------------------------------------
      string obj1=prefix+"DS_"+i;
      ObjectCreate(0,obj1,OBJ_HLINE,0,Time[0],ds[i]);
      ObjectSetString(0,obj1,OBJPROP_TOOLTIP,"Daily Support "+i);
      ObjectSetInteger(0,obj1,OBJPROP_COLOR,SdayColor);
      ObjectSetInteger(0,obj1,OBJPROP_WIDTH,dayWidth);
      TextCreate(0,obj1+"_L",0,Time[ChartGetInteger(0,CHART_VISIBLE_BARS)-1],ds[i],"D1 Sup "+i,"Arial",10,SdayColor);
      //-- Daily Resistance ---------------------------------------------
      obj1=prefix+"DR_"+i;
      ObjectCreate(0,obj1,OBJ_HLINE,0,Time[0],dr[i]);
      ObjectSetString(0,obj1,OBJPROP_TOOLTIP,"Daily Resistance "+i);      
      ObjectSetInteger(0,obj1,OBJPROP_COLOR,RdayColor);
      ObjectSetInteger(0,obj1,OBJPROP_WIDTH,dayWidth);
      TextCreate(0,obj1+"_L",0,Time[ChartGetInteger(0,CHART_VISIBLE_BARS)-1],dr[i],"D1 Res "+i,"Arial",10,RdayColor);
      //-- Weekly Support -----------------------------------------------
      obj1=prefix+"WS_"+i;
      ObjectCreate(0,obj1,OBJ_HLINE,0,Time[0],ws[i]);
      ObjectSetString(0,obj1,OBJPROP_TOOLTIP,"Weekly Support "+i);      
      ObjectSetInteger(0,obj1,OBJPROP_COLOR,SweekColor);
      ObjectSetInteger(0,obj1,OBJPROP_WIDTH,weekWidth);
      TextCreate(0,obj1+"_L",0,Time[ChartGetInteger(0,CHART_VISIBLE_BARS)-1],ws[i],"W1 Sup "+i,"Arial",10,SweekColor);      
      //-- Weekly Resistance ---------------------------------------------
      obj1=prefix+"WR_"+i;
      ObjectCreate(0,obj1,OBJ_HLINE,0,Time[0],wr[i]);
      ObjectSetString(0,obj1,OBJPROP_TOOLTIP,"Weekly Resistance "+i);            
      ObjectSetInteger(0,obj1,OBJPROP_COLOR,RweekColor);
      ObjectSetInteger(0,obj1,OBJPROP_WIDTH,weekWidth);
      TextCreate(0,obj1+"_L",0,Time[ChartGetInteger(0,CHART_VISIBLE_BARS)-1],wr[i],"W1 Sup "+i,"Arial",10,RweekColor);      
   }

   return(rates_total);
}
//+------------------------------------------------------------------+
string periodName(int period) // Convert time frame period minutes into string (ex. 60 to H1)
  {
   if(period==0) period=PeriodSeconds()/60;
   switch(period)
     {
      case 1: return("M1");
      case 5: return("M5");
      case 15: return("M15");
      case 30: return("M30");
      case 60: return("H1");
      case 240: return("H4");
      case 1440: return("D1");
      case 10080: return("W1");
      case 43200: return("MN1");
      default: return("");
     }
  }
//+------------------------------------------------------------------+ 
//| Creating Text object                                             | 
//+------------------------------------------------------------------+ 
bool TextCreate(const long              chart_ID=0,               // chart's ID 
                const string            name="Text",              // object name 
                const int               sub_window=0,             // subwindow index 
                datetime                time=0,                   // anchor point time 
                double                  price=0,                  // anchor point price 
                const string            text="Text",              // the text itself 
                const string            font="Arial",             // font 
                const int               font_size=10,             // font size 
                const color             clr=clrRed,               // color 
                const double            angle=0.0,                // text slope 
                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type 
                const bool              back=false,               // in the background 
                const bool              selection=false,          // highlight to move 
                const bool              hidden=true,              // hidden in the object list 
                const long              z_order=0)                // priority for mouse click 
  { 
//--- set anchor point coordinates if they are not set 
   
//--- reset the error value 
   ResetLastError(); 
//--- create Text object 
   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price)) 
     { 
      Print(__FUNCTION__, 
            ": failed to create \"Text\" object! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- set the text 
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); 
//--- set text font 
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font); 
//--- set font size 
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); 
//--- set the slope angle of the text 
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle); 
//--- set anchor type 
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); 
//--- set color 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 
//--- display in the foreground (false) or background (true) 
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 
//--- enable (true) or disable (false) the mode of moving the object by mouse 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 
//--- hide (true) or display (false) graphical object name in the object list 
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 
//--- set the priority for receiving the event of a mouse click in the chart 
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 
//--- successful execution 
   return(true); 
  } 
