//+------------------------------------------------------------------+
//|                                                   COG_Arrows.mq4 |
//|                                                         Zen Leow |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Zen Leow"
#property link      ""

#property indicator_chart_window
// The number of buffers for calculation, up to 8
#property indicator_buffers 2

// The color for displaying arrows
#property indicator_color1 Green       // Long signal
#property indicator_color2 Red      // Short signal

// Width of the arrows
#property indicator_width1 3  // Long signal arrow
#property indicator_width2 3  // Short signal arrow

extern int ArrowDistance = 10;

double Up_Arrow_Buffer[];    // Long buffer for display
double Down_Arrow_Buffer[];   // Short buffer for display
int PipFactor = 1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
// Cater for fractional pips
   if (Digits == 3 || Digits == 5)
   {
      PipFactor = 10;
   }
   
//---- indicators
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexBuffer(0, Up_Arrow_Buffer);
   SetIndexArrow(0, 233); // Up arrow
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexBuffer(1, Down_Arrow_Buffer);
   SetIndexArrow(1, 234); // Down arrow
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

double get_COG_Red(int index)
{
   return (iCustom(NULL,0,"COG",0,index));
}

double get_COG_Blue(int index)
{
   return (iCustom(NULL,0,"COG",1,index));
}

bool isBuy(int index)
{
   bool result = false;
   if (get_COG_Red(index+1) > get_COG_Blue(index+1))
   {
      if (get_COG_Red(index+2) < get_COG_Blue(index+2))
      {
         result = true;
      }
      if (get_COG_Red(index+2) == get_COG_Blue(index+2))
      {
         int i=3;
         while(true)
         {
            if (get_COG_Red(index+i) > get_COG_Blue(index+i))
            {
               result = false;
               break;
            }
            if (get_COG_Red(index+i) < get_COG_Blue(index+i))
            {
               result = true;
               break;
            }
            i++;
         }
      }
   }
   return (result);
}

bool isSell(int index)
{
   bool result = false;
   if (get_COG_Blue(index+1) > get_COG_Red(index+1))
   {
      if (get_COG_Blue(index+2) < get_COG_Red(index+2))
      {
         result = true;
      }
      if (get_COG_Blue(index+2) == get_COG_Red(index+2))
      {
         int i=3;
         while(i<10)
         {
            if (get_COG_Blue(index+i) > get_COG_Red(index+i))
            {
               result = false;
               break;
            }
            if (get_COG_Blue(index+i) < get_COG_Red(index+i))
            {
               result = true;
               break;
            }
            i++;
         }
      }
   }
   return (result);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i;                           // Bar index       
   int Counted_bars;                // Number of counted bars
   //--------------------------------------------------------------------   
   Counted_bars=IndicatorCounted(); // Number of counted bars   
   i=Bars-Counted_bars-1;           // Index of the first uncounted   
   
   while(i>=0)                      // Loop for uncounted bars     
   {
      Up_Arrow_Buffer[i] = EMPTY_VALUE;
      Down_Arrow_Buffer[i] = EMPTY_VALUE;
      
      if (isBuy(i))
      {
         Up_Arrow_Buffer[i] = Low[i] - (ArrowDistance * Point * PipFactor);
         Down_Arrow_Buffer[i] = EMPTY_VALUE;
      }
      if (isSell(i))
      {
         Down_Arrow_Buffer[i] = High[i] + (ArrowDistance * Point * PipFactor);
         Up_Arrow_Buffer[i] = EMPTY_VALUE;
      }
      
      i--;
   }
   return(0);
}
//+------------------------------------------------------------------+