//+------------------------------------------------------------------+
//|                                                   DataWeekly.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  Gold
#property indicator_color2  Maroon
#property indicator_color3  Gold
#property indicator_color4  Red
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  4
#property indicator_width4  4

//
//

extern bool  ShowBar             = True;
extern int   CandleShift         = 10;

extern bool  ShowInfo            = True;
extern int   InfoAreaYShift      = 350;
extern bool  ShowBackground      = True;
extern color zoneColor           = SlateGray;
extern int   BarsToCalculateAvg  = 10;

extern bool  ShowPivotsLines     = True;
extern bool  ShowPivotsValues    = False;       // problem with placement, ZOOM & TimeFrame dependant
extern int   PivotsValuesYShift  = 10;          // manual addjustment of PP values placement

extern int   PivotsLineStyle     = STYLE_SOLID; //0-4: STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT



//
//
double WeekHigh[];
double WeekLow[];
double WeekOpen[];
double WeekClose[];
//

double prevCurr =-1;
//
int    DataBar_W;

string indNames = "DataWeekly";

//---------------------------------
double W_day_high=0;
double W_day_low=0;
double W_yesterday_high=0;
double W_yesterday_open=0;
double W_yesterday_low=0;
double W_yesterday_close=0;
double W_today_open=0;
double W_today_high=0;
double W_today_low=0;
double W_P=0;
double W_Q=0;
double W_R1,W_R2,W_R3;
double W_S1,W_S2,W_S3;
double W_nQ=0;
double W_nD=0;
double W_D=0;
double W_rates_d1[2][6];
double W_ExtMapBuffer[];
//---------------------------------
double AvgBarsRange;
double CloseToHigh;
double CloseToLow;
//
double   decPoint;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
   SetIndexBuffer(0, WeekHigh);
   SetIndexBuffer(1, WeekLow);
   SetIndexBuffer(2, WeekClose);
   SetIndexBuffer(3, WeekOpen);
      if (ShowBar)
         for (int i=0;i<4;i++)
            {
               SetIndexStyle(i,DRAW_HISTOGRAM);
               SetIndexShift(i,CandleShift);
               SetIndexLabel(i,"Weekly data");
            }            
      else  for (i=0;i<4;i++) SetIndexStyle(i,DRAW_NONE);
   //
   //
   if (Point == 0.00001) decPoint = 0.0001; //6 digits
   else if (Point == 0.001) decPoint = 0.01; //3 digits (for Yen based pairs)
   else decPoint = Point; //Normal 

   prevCurr = -1;
   
   //---- S&R pivot values
   W_R1=0; W_R2=0; W_R3=0;
   W_S1=0; W_S2=0; W_S3=0;
   //----
   AvgBarsRange = 0;
   CloseToHigh  = 0;
   CloseToLow   = 0;

   return(0);
}
int deinit()
{
   for (int counter=-1;counter<21;counter++) ObjectDelete(indNames+counter);
   
   ObjectDelete("W_R1 Label");
   ObjectDelete("W_R1 Value");
   ObjectDelete("W_R1 Line");
   ObjectDelete("W_R2 Label");
   ObjectDelete("W_R2 Value");
   ObjectDelete("W_R2 Line");
   ObjectDelete("W_R3 Label");
   ObjectDelete("W_R3 Value");
   ObjectDelete("W_R3 Line");
   ObjectDelete("W_S1 Label");
   ObjectDelete("W_S1 Value");
   ObjectDelete("W_S1 Line");
   ObjectDelete("W_S2 Label");
   ObjectDelete("W_S2 Value");
   ObjectDelete("W_S2 Line");
   ObjectDelete("W_S3 Label");
   ObjectDelete("W_S3 Value");
   ObjectDelete("W_S3 Line");
   ObjectDelete("W_P Label");
   ObjectDelete("W_P Value");
   ObjectDelete("W_P Line");
      
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
   int      digits   = MarketInfo(Symbol(),MODE_DIGITS);
   double   modifier = 1;
   int      barIndex = 0;
   
      if (digits==3 || digits==5) modifier = 10.0;
      else 
            if (ObjectFind(indNames+"21") != -1)
                ObjectDelete(indNames+"21");
      //
      //
      DataBar_W  = iBarShift(NULL,PERIOD_W1,Time[0]);
         double HiWEEKLY  = iHigh (NULL,PERIOD_W1,DataBar_W);
         double LoWEEKLY  = iLow  (NULL,PERIOD_W1,DataBar_W);
         double current  = iClose(NULL,PERIOD_W1,DataBar_W);
         double range    = getRange(PERIOD_W1,0)/modifier;
         double change   = (current-iOpen(NULL,PERIOD_W1,DataBar_W))/(Point*modifier);
         int    limit, i, x;
      //
      //
      if (ShowInfo)
      {
          if (ObjectFind(indNames+"1") ==-1) {
                  objectCreate("1",70,InfoAreaYShift,"Weekly",10);
                  objectCreate("2",10,InfoAreaYShift+10,"--------------------------------------",10,NULL);
                  objectCreate("3",35,InfoAreaYShift+32,"distance from high : ");
                  objectCreate("4",35,InfoAreaYShift+44,"distance from low : " );
                  objectCreate("5",10,InfoAreaYShift+56,"--------------------------------------",10,NULL);
                  
                  objectCreate("6",35,InfoAreaYShift+20,"current bar range : "       );
                  objectCreate("7",35,InfoAreaYShift+64,"distance from open : ");
                  objectCreate("8",10,InfoAreaYShift+76,"--------------------------------------",10,NULL);
                  objectCreate("9",35,InfoAreaYShift+88,"avg. " + BarsToCalculateAvg + " bar(s) range : ");
                  objectCreate("10",35,InfoAreaYShift+100,"avg. " + BarsToCalculateAvg + " bar(s) C/H : ");
                  objectCreate("11",35,InfoAreaYShift+112,"avg. " + BarsToCalculateAvg + " bar(s) C/L : ");
                  objectCreate("12",10,InfoAreaYShift+124,"--------------------------------------",10,NULL);
         }
         //
         //
         if (prevCurr != current)
            {
               prevCurr = current;
                  double currFromHi = (HiWEEKLY-current)/(Point*modifier);
                  double currFromLo = (current-LoWEEKLY)/(Point*modifier);
                  //
                  //
                  objectCreate("13",10,InfoAreaYShift+20,DoubleToStr(range,0)        ,10,"Arial bold",Gold);
                  objectCreate("14",10,InfoAreaYShift+32,DoubleToStr(currFromHi,0)   ,10,"Arial bold",YellowGreen);
                  objectCreate("15",10,InfoAreaYShift+44,DoubleToStr(currFromLo,0)   ,10,"Arial bold",YellowGreen);
                  
                  if (change<0)
                        objectCreate("16",10,InfoAreaYShift+64,DoubleToStr(change,0) ,10,"Arial bold",Red);
                  else  objectCreate("16",10,InfoAreaYShift+64,DoubleToStr(change,0) ,10,"Arial bold",Gold);
                  
                  string currentValue = DoubleToStr(current,digits);
                     if (modifier !=1)
                        objectCreate("17",10, InfoAreaYShift+0,StringSubstr(currentValue,StringLen(currentValue)-1),12,"Arial bold",Gold);
                  
                  // avg range of given weekly bars calculation
                                    
                  AvgBarsRange = CalculateAVG(BarsToCalculateAvg);
                  CloseToHigh  = CalculateAVGCloseToHigh(BarsToCalculateAvg);
                  CloseToLow   = CalculateAVGCloseToLow(BarsToCalculateAvg);
                  
                  objectCreate("18",10,InfoAreaYShift+88,DoubleToStr(AvgBarsRange,0) ,10,"Arial bold",Gold);
                  objectCreate("19",10,InfoAreaYShift+100,DoubleToStr(CloseToHigh,0) ,10,"Arial bold",Gold);
                  objectCreate("20",10,InfoAreaYShift+112,DoubleToStr(CloseToLow,0) ,10,"Arial bold",Gold);
                  //
            }
      }
      
      setBarIndicator(change,HiWEEKLY,LoWEEKLY,iOpen(NULL,PERIOD_W1,DataBar_W),current);
      
      for(i=0,limit=Bars-1;i<4;i++) SetIndexDrawBegin(i,limit);

// PIVOT Point calculation
   ArrayCopyRates(W_rates_d1, Symbol(), 10080);

   W_yesterday_close = W_rates_d1[1][4];
   W_yesterday_open = W_rates_d1[1][1];
   W_today_open = W_rates_d1[0][1];
   W_yesterday_high = W_rates_d1[1][3];
   W_yesterday_low = W_rates_d1[1][2];
   W_day_high = W_rates_d1[0][3];
   W_day_low = W_rates_d1[0][2];

//---- Calculate Pivots

   W_D = (W_day_high - W_day_low);
   W_Q = (W_yesterday_high - W_yesterday_low);
   W_P = (W_yesterday_high + W_yesterday_low + W_yesterday_close) / 3;
   W_R1 = (2*W_P)-W_yesterday_low;
   W_S1 = (2*W_P)-W_yesterday_high;
   W_R2 = W_P+(W_yesterday_high - W_yesterday_low);
   W_S2 = W_P-(W_yesterday_high - W_yesterday_low);
   W_R3 = (2*W_P)+(W_yesterday_high-(2*W_yesterday_low));
   W_S3 = (2*W_P)-((2* W_yesterday_high)-W_yesterday_low);

   if (W_Q > 5)
   {
      W_nQ = W_Q;
   }
   else
   {
     W_nQ = W_Q*10000;
   }

   if (W_D > 5)
   {
       W_nD = W_D;
   }
   else
   {
      W_nD = W_D*10000;
   }

   // DAILY Pivot Lines
   if (ShowPivotsLines==true)
   {
      drawPivotValues("W_S3 value","W_S3 label","W_S3",W_S3,"Arial",8,C'200,200,200',PivotsValuesYShift);
      drawPivotValues("W_S2 value","W_S2 label","W_S2",W_S2,"Arial",8,C'200,200,200',PivotsValuesYShift);
      drawPivotValues("W_S1 value","W_S1 label","W_S1",W_S1,"Arial",8,C'200,200,200',PivotsValuesYShift);
      drawPivotValues("W_P value","W_P label","W_P",W_P,"Arial",8,Khaki,PivotsValuesYShift);
      drawPivotValues("W_R1 value","W_R1 label","W_R1",W_R1,"Arial",8,C'200,200,200',PivotsValuesYShift);
      drawPivotValues("W_R2 value","W_R2 label","W_R2",W_R2,"Arial",8,C'200,200,200',PivotsValuesYShift);
      drawPivotValues("W_R3 value","W_R3 label","W_R3",W_R3,"Arial",8,C'200,200,200',PivotsValuesYShift);
      //--- Draw Pivot lines on chart
      drawPivotLine("W_S3 line",W_S3,LightGreen,1);
      drawPivotLine("W_S2 line",W_S2,LightGreen,1);
      drawPivotLine("W_S1 line",W_S1,LightGreen,1);
      drawPivotLine("W_P line",W_P,Khaki,2);
      drawPivotLine("W_R1 line",W_R1,Red,1);
      drawPivotLine("W_R2 line",W_R2,Red,1);
      drawPivotLine("W_R3 line",W_R3,Red,1);
   }
   //---- End of Pivot Line Draw

   return(0);
  }

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+  

void drawPivotValues(string objectName, string objectLabel, string objectDescripton, double objectValue, string fontName, int fontSize, color textColor, int object_Y_shift)
{
   int digitsPoints = Digits; // display 4 digits for nonJPY and 2 digidts for JPY crosses
   int index = StringFind(Symbol(),"JPY",0);

// prevent from plasing labels to close to PivotLines if using 5-digits broker
   if (index > 0)
   {
      if (digitsPoints>2)
         object_Y_shift = object_Y_shift*10;
   }
   else
   {
      if (digitsPoints>4)
         object_Y_shift = object_Y_shift*10;
   }
//+--------------------------------------------------------
   if(ObjectFind(objectName) != 0)
   {
      ObjectCreate(objectLabel, OBJ_TEXT, 0, Time[0]+(CandleShift-4)*Period()*60, objectValue);
      ObjectSetText(objectLabel, objectDescripton, fontSize, fontName, textColor);
   }
   else
   {
      ObjectMove(objectLabel, 0, Time[0]+(CandleShift-4)*Period()*60, objectValue);
   }
   
   if(ShowPivotsValues)
      if(ObjectFind(objectName) != 0)
      {
         ObjectCreate(objectName, OBJ_TEXT, 0, Time[0]+(CandleShift-4)*Period()*60, objectValue+object_Y_shift*Point);
         if (index > 0)
         {
            ObjectSetText(objectName, DoubleToStr(objectValue,2), fontSize, fontName, textColor);
         }
         else
         {
            ObjectSetText(objectName, DoubleToStr(objectValue,4), fontSize, fontName, textColor);
         }
      }
      else
      {
         ObjectMove(objectName, 0, Time[0]+(CandleShift-4)*Period()*60, objectValue+object_Y_shift*Point);
      }
}
//
//
void drawPivotLine(string lineName, double lineValue, color lineColor, int lineWidth)
{
if(ObjectFind(lineName) != 0)
   {
      ObjectCreate(lineName, OBJ_TREND, 0, Time[0]+(CandleShift-7)*Period()*60, lineValue,Time[0]+(CandleShift-1)*Period()*60, lineValue);
      ObjectSet(lineName, OBJPROP_STYLE, PivotsLineStyle);
      ObjectSet(lineName, OBJPROP_WIDTH, lineWidth);
      ObjectSet(lineName, OBJPROP_COLOR, lineColor);
      ObjectSet(lineName, OBJPROP_RAY,   False);
   }
   else
   {
      ObjectMove(lineName, 0,Time[0]+(CandleShift-7)*Period()*60,lineValue);
      ObjectMove(lineName, 1,Time[0]+(CandleShift-1)*Period()*60,lineValue);
   }
}
//
//
double CalculateAVG(int BarsToCount)
{
   int   tempSum = 0;
   int   v;
   
   if (BarsToCount<=0) BarsToCount=1;
   
   for (v=1; v<=BarsToCount; v++)
      tempSum = tempSum + getRange(PERIOD_W1,v);
   
   tempSum = tempSum/BarsToCount;

   return (NormalizeDouble(tempSum,0));
}
//
//
double CalculateAVGCloseToHigh(int BarsToCount)
{
   int    tempSum = 0;
   double tmpCloseToHigh = 0;
   int    v;
   
   if (BarsToCount<=0) BarsToCount=1;
   
   for (v=1; v<=BarsToCount; v++)
   {
      tmpCloseToHigh = ( iHigh(NULL, PERIOD_W1, v) - iClose(NULL, PERIOD_W1, v) )/decPoint;
      tempSum = tempSum + tmpCloseToHigh;
   }
   tempSum = tempSum/BarsToCount;

   return (NormalizeDouble(tempSum,0));
}
//
//
double CalculateAVGCloseToLow(int BarsToCount)
{
   int    tempSum = 0;
   double tmpCloseToLow = 0;
   int    v;
   
   if (BarsToCount<=0) BarsToCount=1;
   
   for (v=1; v<=BarsToCount; v++)
   {
      tmpCloseToLow = ( iClose(NULL, PERIOD_W1, v) - iLow(NULL, PERIOD_W1, v) )/decPoint;
      tempSum = tempSum + tmpCloseToLow;
   }
   
   tempSum = tempSum/BarsToCount;

   return (NormalizeDouble(tempSum,0));
}
//
//
double getRange(int period, int barIdx)
{
   double   range;

      range = iHigh(NULL, period, barIdx) - iLow(NULL, period, barIdx);
      return (NormalizeDouble(range/decPoint,0));
}
//
//
void setBarIndicator(double change, double hi,double low, double open, double close)
{
   string name_1  = indNames+"-1";
   int    shift = 0;
   
   if (change>0) { WeekHigh[shift]  = hi;  WeekLow[shift] = low; }            
   else          { WeekHigh[shift]  = low; WeekLow[shift] = hi;  }            
                   WeekOpen[shift]  = open;
                   WeekClose[shift] = close;

   if (ShowBackground)
      {                         
         if (ObjectFind(name_1) == -1)
             ObjectCreate(name_1,OBJ_RECTANGLE,0,0,0);
             ObjectSet(name_1,OBJPROP_TIME1,iTime(NULL,PERIOD_W1,DataBar_W));
             ObjectSet(name_1,OBJPROP_TIME2,iTime(NULL,         0,0));
             ObjectSet(name_1,OBJPROP_PRICE1,hi);
             ObjectSet(name_1,OBJPROP_PRICE2,low);
             ObjectSet(name_1,OBJPROP_COLOR,zoneColor);
      }             
}
//
//
void objectCreate(string name,int x,int y,string text="-",int size=10,
                  string font="Arial",color colour=C'200,200,200',int window = 0)
{
   if (ObjectFind(indNames+name) == -1)
   {
      ObjectCreate(indNames+name,OBJ_LABEL,window,0,0);
         ObjectSet(indNames+name,OBJPROP_CORNER,1);
         ObjectSet(indNames+name,OBJPROP_XDISTANCE,x);
         ObjectSet(indNames+name,OBJPROP_YDISTANCE,y);
   }               
   ObjectSetText(indNames+name,text,size,font,colour);
}

