//+------------------------------------------------------------------+
//|                                                   SundayBars.mq4 |
//|                                   Copyright © 2009, Serega Lykov |
//|                                       http://mtexperts.narod.ru/ |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, Serega Lykov"
#property link      "http://mtexperts.narod.ru/"

//---- property of indicator ----------------------------------------+
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue
#property indicator_color4 DeepSkyBlue
#property indicator_color5 Magenta
#property indicator_color6 Magenta
#property indicator_color7 Magenta
#property indicator_color8 Magenta
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 5
#property indicator_width4 5
#property indicator_width5 1
#property indicator_width6 1
#property indicator_width7 5
#property indicator_width8 5

//---- external parameters ------------------------------------------+

//---- buffers ------------------------------------------------------+
static double UpHShadowBuffer[];
static double UpLShadowBuffer[];
static double UpHBodyBuffer[];
static double UpLBodyBuffer[];
static double DnHShadowBuffer[];
static double DnLShadowBuffer[];
static double DnHBodyBuffer[];
static double DnLBodyBuffer[];

//---- global variables ---------------------------------------------+

//-------------------------------------------------------------------+
//---- initialization of indicator ----------------------------------+
//-------------------------------------------------------------------+
int init()
  {
   //---- set a "short" name of the indicator -----------------------+
   IndicatorShortName("SundayBars");
   //---- set a accuracy of values of the indicator -----------------+
   IndicatorDigits(Digits);
   //---- set a style for line --------------------------------------+
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(5,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(6,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(7,DRAW_HISTOGRAM,STYLE_SOLID,4);
   //---- set a arrays for line -------------------------------------+
   SetIndexBuffer(0,UpHShadowBuffer);
   SetIndexBuffer(1,UpLShadowBuffer);
   SetIndexBuffer(2,UpHBodyBuffer);
   SetIndexBuffer(3,UpLBodyBuffer);
   SetIndexBuffer(4,DnHShadowBuffer);
   SetIndexBuffer(5,DnLShadowBuffer);
   SetIndexBuffer(6,DnHBodyBuffer);
   SetIndexBuffer(7,DnLBodyBuffer);
   //---- finish of initialization ----------------------------------+
   return(0);
  }

//-------------------------------------------------------------------+
//---- deinitialization of indicator --------------------------------+
//-------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

//-------------------------------------------------------------------+
//---- SundayBars ---------------------------------------------------+
//-------------------------------------------------------------------+
int start()
  {
   //---- amount not changed bars after last call of the indicator --+
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   //---- set values of candles -------------------------------------+
   for(int i=limit; i>=0; i--)
     {
      int day_of_week = TimeDayOfWeek(Time[i]);
      if(day_of_week > 0 && day_of_week < 6)
        {
         UpHShadowBuffer[i] = EMPTY_VALUE;
         UpLShadowBuffer[i] = EMPTY_VALUE;
         UpHBodyBuffer[i]   = EMPTY_VALUE;
         UpLBodyBuffer[i]   = EMPTY_VALUE;
         DnHShadowBuffer[i] = EMPTY_VALUE;
         DnLShadowBuffer[i] = EMPTY_VALUE;
         DnHBodyBuffer[i]   = EMPTY_VALUE;
         DnLBodyBuffer[i]   = EMPTY_VALUE;
        }
      else
        {
         double open  = NormalizeDouble(Open[i],Digits);
         double close = NormalizeDouble(Close[i],Digits);
         if(close >= open)
           {
            UpHShadowBuffer[i] = NormalizeDouble(High[i],Digits);
            UpLShadowBuffer[i] = NormalizeDouble(Low[i],Digits);
            UpHBodyBuffer[i]   = close;
            UpLBodyBuffer[i]   = open;
            DnHShadowBuffer[i] = EMPTY_VALUE;
            DnLShadowBuffer[i] = EMPTY_VALUE;
            DnHBodyBuffer[i]   = EMPTY_VALUE;
            DnLBodyBuffer[i]   = EMPTY_VALUE;
           }
         else
           {
            UpHShadowBuffer[i] = EMPTY_VALUE;
            UpLShadowBuffer[i] = EMPTY_VALUE;
            UpHBodyBuffer[i]   = EMPTY_VALUE;
            UpLBodyBuffer[i]   = EMPTY_VALUE;
            DnHShadowBuffer[i] = NormalizeDouble(High[i],Digits);
            DnLShadowBuffer[i] = NormalizeDouble(Low[i],Digits);
            DnHBodyBuffer[i]   = open;
            DnLBodyBuffer[i]   = close;
           }
        }
     }
   //---- finish of iteration ---------------------------------------+
   return(0);
  }

//-------------------------------------------------------------------+