//+------------------------------------------------------------------+
//|                                                    NoRepaint.mq4 |
//|                                                         Zen_Leow |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property indicator_chart_window
#property indicator_buffers 3

#property indicator_color1 DeepSkyBlue      
#property indicator_color2 DeepPink        


#property indicator_width1 1  
#property indicator_width2 1  

extern int ArrowDistance = 3;
bool WaitForCandleClose = false;
bool ArrowOnTriggerBar = false;


double Up_Arrow_Buffer[];    
double Down_Arrow_Buffer[];  
double Right_Side_Buffer[];
int RightSideCandleNum = 0;
int SignalIndex = 0;
int ArrowOffset = 0;





//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   if (WaitForCandleClose)
   {
      SignalIndex = 1;
   }
   else
   {
      SignalIndex = 0;
   }
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexBuffer(0, Up_Arrow_Buffer);
   SetIndexArrow(0, 225); 
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexBuffer(1, Down_Arrow_Buffer);
   SetIndexArrow(1, 226); 
   SetIndexStyle(2, DRAW_NONE);
   SetIndexBuffer(2, Right_Side_Buffer);
//----
   return(0);
  }
  
  
  
  
  
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  
  
  
  
  
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i;                           
   int Counted_bars;                
   //--------------------------------------------------------------------   
   Counted_bars=IndicatorCounted();  
   i=Bars-Counted_bars-1;           
   if (i == 0)
   {
      i = 5;  
   }
   while(i>SignalIndex)            
   {      
      if (isUpSwingBar(i))
      {
         if (ArrowOnTriggerBar)
         {
            ArrowOffset = RightSideCandleNum;
         }
         else
         {
            ArrowOffset = 0;
         }
         if (RightSideCandleNum == 1 || (RightSideCandleNum == 2 && i > SignalIndex+1))
         {
            Up_Arrow_Buffer[i-ArrowOffset] = Low[i-ArrowOffset] - (ArrowDistance * Point);
            Down_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
            Right_Side_Buffer[i] = RightSideCandleNum;
         }
      }
      else if (isDownSwingBar(i))
      {
         if (ArrowOnTriggerBar)
         {
            ArrowOffset = RightSideCandleNum;
         }
         else
         {
            ArrowOffset = 0;
         }
         if (RightSideCandleNum == 1 || (RightSideCandleNum == 2 && i > SignalIndex+1))
         {
            Down_Arrow_Buffer[i-ArrowOffset] = High[i-ArrowOffset] + (ArrowDistance * Point);
            Up_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
            Right_Side_Buffer[i] = RightSideCandleNum;
         }
      }
      else
      {
         if (ArrowOnTriggerBar)
         {
            ArrowOffset = Right_Side_Buffer[i];
         }
         else
         {
            ArrowOffset = 0;
         }
         Up_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
         Down_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
         Right_Side_Buffer[i] = EMPTY_VALUE;
        // RightSideCandleNum = 0;
      }
      i--;
   }
//----
   return(0);
}

bool isUpSwingBar(int i)
{
   if (UpSwingLeftOk(i) && UpSwingRightOk(i))
   {
      return (true);
   }
   return (false);
}

bool UpSwingLeftOk(int i)
{
   if (Low[i] < Low[i+1])
   {
      if (High[i] < High[i+1])
      {
         return (true);
      }
      else
      {
         if (Low[i] < Low[i+2] && High[i] < High[i+2])
         {
            return (true);
         }
      }
   }
   return (false);
}

bool UpSwingRightOk(int i)
{
   if (Low[i] < Low[i-1])
   {
      if (High[i] < High[i-1])
      {
         RightSideCandleNum = 1;
         return (true);
      }
      else
      {
         if (Low[i] < Low[i-2] && High[i] < High[i-2])
         {
            RightSideCandleNum = 2;
            return (true);
         }
      }
   }
   return (false);
}

bool isDownSwingBar(int i)
{
   if (DownSwingLeftOk(i) && DownSwingRightOk(i))
   {
      return (true);
   }
   return (false);
}

bool DownSwingLeftOk(int i)
{
   if (High[i] > High[i+1])
   {
      if (Low[i] > Low[i+1])
      {
         return (true);
      }
      else
      {
         if (Low[i] > Low[i+2] && High[i] > High[i+2])
         {
            return (true);
         }
      }
   }
   return (false);
}

bool DownSwingRightOk(int i)
{
   if (High[i] > High[i-1])
   {
      if (Low[i] > Low[i-1])
      {
         RightSideCandleNum = 1;
         return (true);
      }
      else
      {
         if (Low[i] > Low[i-2] && High[i] > High[i-2])
         {
            RightSideCandleNum = 2;
            return (true);
         }
      }
   }
   return (false);
}
//+------------------------------------------------------------------+