//+------------------------------------------------------------------+
//|                                             Candle Direction.mq4 |
//|                                   Copyright 2014, Luis Andrianto |
//|                            https://login.mql5.com/en/users/lou15 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Luis Andrianto"
#property link      "https://login.mql5.com/en/users/lou15"

#property indicator_chart_window
#property indicator_buffers 1

extern color              LabelColor  = clrLightSkyBlue;
extern ENUM_BASE_CORNER   Corner      = CORNER_RIGHT_UPPER;
extern color              UpColor     = clrLimeGreen;
extern color              DownColor   = clrRed;
extern color              NetralColor = clrSilver;
extern int                X_Start     = 20;
extern int                Y_Start     = 20;
extern bool               DisplayM1   = true;
extern bool               DisplayM5   = true;
extern bool               DisplayM15  = true;
extern bool               DisplayM30  = true;
extern bool               DisplayH1   = true;
extern bool               DisplayH4   = true;
extern bool               DisplayD1   = true;
extern bool               DisplayW1   = true;
extern bool               DisplayMN1  = true;

int TF[] = {PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1};
string Label[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN1"};
double ExtBuff[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   //---- indicators
   SetIndexBuffer(0, ExtBuff, INDICATOR_DATA);

   for (int i = 0; i <ArraySize(TF); i++)
   {
      if(ShouldDisplayTF(i))
        {
            ObjectCreate(Label[i], OBJ_LABEL, 0, 0, 0);
            ObjectCreate(Label[i] + " ARROW", OBJ_LABEL, 0, 0, 0);
        }
   }

   return (0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   for (int i = 0; i < ArraySize(TF); i++)
   {
      if(ShouldDisplayTF(i))
        {
            ObjectDelete(Label[i]);
            ObjectDelete(Label[i] + " ARROW");
        }
   }
   return (0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
double OCandle(int tf)
{
   double OC = iOpen(_Symbol, tf, 0);
   return (OC);
}

double CCandle(int tf)
{
   double CC = iClose(_Symbol, tf, 0);
   return (CC);
}

void ObSetLabel(string name, string text, int x, int y)
{
   ObjectSetText(name, text, 10, "Impact", LabelColor);
   ObjectSet(name, OBJPROP_CORNER, Corner);
   ObjectSet(name, OBJPROP_XDISTANCE, x);
   ObjectSet(name, OBJPROP_YDISTANCE, y);
   ObjectSet(name, OBJPROP_BACK, 0);
}

void ObSetArrow(string name, int code, int x, int y, color clr)
{
   ObjectSetText(name, CharToStr(code), 14, "Wingdings", clr);
   ObjectSet(name, OBJPROP_CORNER, Corner);
   ObjectSet(name, OBJPROP_XDISTANCE, x);
   ObjectSet(name, OBJPROP_YDISTANCE, y);
   ObjectSet(name, OBJPROP_BACK, 0);
}

bool ShouldDisplayTF(int index)
{
   switch (index)
   {
      case 0: return DisplayM1;
      case 1: return DisplayM5;
      case 2: return DisplayM15;
      case 3: return DisplayM30;
      case 4: return DisplayH1;
      case 5: return DisplayH4;
      case 6: return DisplayD1;
      case 7: return DisplayW1;
      case 8: return DisplayMN1;
      default: return false;
   }
}

int start()
{
   color clr;
   int xOffset = X_Start;
   int yOffset = Y_Start;

   for (int i = 0; i <ArraySize(TF); i++)
   {
      if(ShouldDisplayTF(i))
        {
            ObSetLabel(Label[i], Label[i], xOffset, yOffset);
        }      
      if (CCandle(TF[i]) > OCandle(TF[i]))
      {
         clr = UpColor;
         ExtBuff[i] = 1;
      }
      else if (CCandle(TF[i]) < OCandle(TF[i]))
      {
         clr = DownColor;
         ExtBuff[i] = 2;
      }
      else
      {
         clr = NetralColor;
         ExtBuff[i] = 0;
      }
      
      ObSetArrow(Label[i] + " ARROW", 110, xOffset, yOffset + 20, clr);
      
      xOffset += 30;
   }

   return (0);
}
