//+------------------------------------------------------------------+
//|               Price Channel Central - Colored Candles.mq4        |
//|               Reworked from Price Channel Central                |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman (YTG) - modified"
#property link      "http://ytg.com.ua/"
#property version   "1.10"
#property strict
#property indicator_chart_window

// 8 candle overlay buffers + 1 optional center line buffer
#property indicator_buffers 9
#property indicator_color1  Lime
#property indicator_color2  Lime
#property indicator_color3  Red
#property indicator_color4  Red
#property indicator_color5  Lime
#property indicator_color6  Lime
#property indicator_color7  Red
#property indicator_color8  Red
#property indicator_color9  DodgerBlue
#property indicator_width1  1     // up wick
#property indicator_width2  1     // up wick
#property indicator_width3  1     // down wick
#property indicator_width4  1     // down wick
#property indicator_width5  2     // up body
#property indicator_width6  2     // up body
#property indicator_width7  2     // down body
#property indicator_width8  2     // down body
#property indicator_width9  1     // center line, if enabled

extern int   Bars_Count       = 32;
extern color Close_Above_Color = clrLime;
extern color Close_Below_Color = clrRed;
extern color Center_Line_Color = clrDodgerBlue;
extern bool  Show_Center_Line  = false;
extern bool  Show_Signal_Label = true;

//---- buffers
double UpWickHigh[];
double UpWickLow[];
double DownWickHigh[];
double DownWickLow[];
double UpBodyHigh[];
double UpBodyLow[];
double DownBodyHigh[];
double DownBodyLow[];
double CenterLine[];

//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorShortName("PCC Colored Candles ("+IntegerToString(Bars_Count)+")");

   SetIndexBuffer(0,UpWickHigh);
   SetIndexBuffer(1,UpWickLow);
   SetIndexBuffer(2,DownWickHigh);
   SetIndexBuffer(3,DownWickLow);
   SetIndexBuffer(4,UpBodyHigh);
   SetIndexBuffer(5,UpBodyLow);
   SetIndexBuffer(6,DownBodyHigh);
   SetIndexBuffer(7,DownBodyLow);
   SetIndexBuffer(8,CenterLine);

   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1,Close_Above_Color);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1,Close_Above_Color);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1,Close_Below_Color);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1,Close_Below_Color);
   SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID,2,Close_Above_Color);
   SetIndexStyle(5,DRAW_HISTOGRAM,STYLE_SOLID,2,Close_Above_Color);
   SetIndexStyle(6,DRAW_HISTOGRAM,STYLE_SOLID,2,Close_Below_Color);
   SetIndexStyle(7,DRAW_HISTOGRAM,STYLE_SOLID,2,Close_Below_Color);

   if(Show_Center_Line)
      SetIndexStyle(8,DRAW_LINE,STYLE_SOLID,1,Center_Line_Color);
   else
      SetIndexStyle(8,DRAW_NONE);

   for(int i=0; i<9; i++)
     {
      SetIndexEmptyValue(i,EMPTY_VALUE);
      SetIndexDrawBegin(i,Bars_Count);
     }

   SetIndexLabel(0,"Above wick high");
   SetIndexLabel(1,"Above wick low");
   SetIndexLabel(2,"Below wick high");
   SetIndexLabel(3,"Below wick low");
   SetIndexLabel(4,"Above body high");
   SetIndexLabel(5,"Above body low");
   SetIndexLabel(6,"Below body high");
   SetIndexLabel(7,"Below body low");
   SetIndexLabel(8,"Center line");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDelete(0,"PCC_Label");
  }
//+------------------------------------------------------------------+
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(rates_total<=Bars_Count+1)
      return(0);

   int limit=rates_total-prev_calculated;
   if(prev_calculated==0)
      limit=rates_total-Bars_Count-1;
   else
      limit++;

   for(int i=limit; i>=0 && !IsStopped(); i--)
     {
      ClearBar(i);

      int hiIndex=iHighest(NULL,0,MODE_HIGH,Bars_Count,i+1);
      int loIndex=iLowest(NULL,0,MODE_LOW,Bars_Count,i+1);
      if(hiIndex<0 || loIndex<0)
         continue;

      double hi=high[hiIndex];
      double lo=low[loIndex];
      double center=(hi+lo)/2.0;
      CenterLine[i]=center;

      double bodyHigh=MathMax(open[i],close[i]);
      double bodyLow =MathMin(open[i],close[i]);

      if(close[i]>=center)
        {
         UpWickHigh[i]=high[i];
         UpWickLow[i]=low[i];
         UpBodyHigh[i]=bodyHigh;
         UpBodyLow[i]=bodyLow;
        }
      else
        {
         DownWickHigh[i]=high[i];
         DownWickLow[i]=low[i];
         DownBodyHigh[i]=bodyHigh;
         DownBodyLow[i]=bodyLow;
        }
     }

   if(Show_Signal_Label && CenterLine[0]!=EMPTY_VALUE)
     {
      color labelColor=clrSilver;
      string sig="AT CENTER";
      if(close[0]>CenterLine[0]) { labelColor=Close_Above_Color; sig="CLOSE ABOVE CENTER"; }
      if(close[0]<CenterLine[0]) { labelColor=Close_Below_Color; sig="CLOSE BELOW CENTER"; }
      LabelCreate(sig,labelColor);
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
void ClearBar(const int i)
  {
   UpWickHigh[i]=EMPTY_VALUE;
   UpWickLow[i]=EMPTY_VALUE;
   DownWickHigh[i]=EMPTY_VALUE;
   DownWickLow[i]=EMPTY_VALUE;
   UpBodyHigh[i]=EMPTY_VALUE;
   UpBodyLow[i]=EMPTY_VALUE;
   DownBodyHigh[i]=EMPTY_VALUE;
   DownBodyLow[i]=EMPTY_VALUE;
   CenterLine[i]=EMPTY_VALUE;
  }
//+------------------------------------------------------------------+
bool LabelCreate(string text="Label",
                 color clr=clrRed,
                 long chart_ID=0,
                 string name="PCC_Label",
                 int sub_window=0,
                 int x=10,
                 int y=10,
                 ENUM_BASE_CORNER corner=CORNER_RIGHT_UPPER,
                 ENUM_ANCHOR_POINT anchor=ANCHOR_RIGHT_UPPER,
                 string font="Arial",
                 int font_size=10)
  {
   ObjectDelete(chart_ID,name);
   ResetLastError();

   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
     {
      Print(__FUNCTION__,": failed to create label. Error = ",GetLastError());
      return(false);
     }

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   return(true);
  }
//+------------------------------------------------------------------+
