//+------------------------------------------------------------------+
//|                                                    Guppy CBL.mq4 |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 Green
#property indicator_width1 1
#property indicator_width2 1

//---- input parameters 
extern int    LookbackBars         = 100;
extern int    CountbackSteps       = 2;
extern bool   UseClose             = true;
input string Arrow_Settings1_______="============| Arrows High |============";
input bool   Use_Hi                = 1;     // Use High
input bool   AlertsOn1             = TRUE;  // Alerts On
      bool   AlertsOnCurrent1      = False; // Alerts on current
input bool   AlertsMessage1        = TRUE;  // Alerts PopUp
input bool   AlertsNotify1         = TRUE;  // Alerts Notify
input bool   AlertsEmail1          = TRUE;  // Alerts Email
input bool   ArrowsON1             = TRUE;  // Arrows on/off
input int    ArrowType1            = 234;   // Arrow Type 
input int    A_Size1               = 2;     // Arrow Size
input int    A_Gap1                = 1;     // Arrow Gap
input color  Hi_Color1             = Red;   // Arrow Color
input string Arrow_Settings2_______="============| Arrows Low |============";
input bool   Use_Lo                = 1;     // Use Low
input bool   AlertsOn2             = TRUE;  // Alerts On
      bool   AlertsOnCurrent2      = False; // Alerts on current
input bool   AlertsMessage2        = TRUE;  // Alerts PopUp
input bool   AlertsNotify2         = TRUE;  // Alerts Notify
input bool   AlertsEmail2          = TRUE;  // Alerts Email
input bool   ArrowsON2             = TRUE;  // Arrows on/off
input int    ArrowType2            = 233;   // Arrow Type 
input int    A_Size2               = 2;     // Arrow Size
input int    A_Gap2                = 1;     // Arrow Gap
input color  Lo_Color2             = Lime;   // Arrow Color
//---- indicator buffers
double UpBuffer[];
double DnBuffer[];double UsBuffer[],DwBuffer[];
int Pip = 1,Value=0;string notice = "nothing";datetime Times=0;
//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndicatorBuffers(4);
  SetIndexStyle(0,DRAW_LINE);
  SetIndexStyle(1,DRAW_LINE);
  SetIndexBuffer(0,UpBuffer);
  SetIndexBuffer(1,DnBuffer);

  string short_name="GuppyCBL("+(string)CountbackSteps+")";
  IndicatorShortName(short_name);
  IndicatorDigits(5);
  if (StringFind(Symbol(),"JPY") >= 0)
    IndicatorDigits(3);
  SetIndexLabel(0,"Guppy Hi");
  SetIndexLabel(1,"Guppy Lo");
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
   SetIndexStyle(2, DRAW_ARROW, EMPTY,A_Size2,Lo_Color2);SetIndexArrow(2, ArrowType2);SetIndexBuffer(2, UsBuffer);
   SetIndexStyle(3, DRAW_ARROW, EMPTY,A_Size1,Hi_Color1);SetIndexArrow(3, ArrowType1);SetIndexBuffer(3, DwBuffer);
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
   if ((Digits == 3 || Digits == 5)) Pip = 10;
   if(Bid<10 && _Digits==5) { Value=5;}if(Bid<10 && _Digits==4) { Value=4;}
   if(Bid<10 && _Digits==3) { Value=3;}if(Bid<10 && _Digits==2) { Value=2;}
   if(Bid>10 && _Digits==3) { Value=3;}if(Bid>10 && _Digits==2) { Value=2;}   
  return(0);
}

//+------------------------------------------------------------------+
int start()  
 {
//+------------------------------------------------------------------+
  double val;int startbar = MathMin(Bars-CountbackSteps-2,LookbackBars);
  for (int i=startbar; i>=0; i--)   {
    int j=i, c=0;
    double HH=High[i], LL=Low[i];
    while (c<CountbackSteps && j<i+10 && j<startbar)  {
      j++;
      if (Low[j]<LL) c++;
      HH=MathMax(HH,High[j]);
      LL=MathMin(LL,Low[j]);
    }  
    if (High[i]==HH)
      DnBuffer[i]=Low[j];
    else
      DnBuffer[i]=DnBuffer[i+1];
    if (DnBuffer[i]!=EMPTY_VALUE && DnBuffer[i+1]!=EMPTY_VALUE)  
      DnBuffer[i]=MathMax(DnBuffer[i],DnBuffer[i+1]);  
    if (UseClose)
       val=Close[i];
    else
      val=Low[i];    
    if (val<=DnBuffer[i])
      DnBuffer[i]=EMPTY_VALUE;  

    j=i; c=0;
    HH=High[i]; LL=Low[i];
    while (c<CountbackSteps && j<i+10 && j<startbar)  {
      j++;
      if (High[j]>HH) c++;
      LL=MathMin(LL,Low[j]);
      HH=MathMax(HH,High[j]);
    }  
    if (Low[i]==LL)
      UpBuffer[i]=High[j];
    else
      UpBuffer[i]=UpBuffer[i+1];
    if (UpBuffer[i]!=EMPTY_VALUE && UpBuffer[i+1]!=EMPTY_VALUE)  
      UpBuffer[i]=MathMin(UpBuffer[i],UpBuffer[i+1]);  
    if (UseClose)
      val=Close[i];
    else
      val=High[i];    
    if (val>=UpBuffer[i])
      UpBuffer[i]=EMPTY_VALUE;  
  }
 Arrow(); return(0);	
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
void Arrow()
{
   int i; 
   int counted = IndicatorCounted();
   if(counted < 500) counted = 500;
   for(i = Bars-1-counted; i >= 0; i--) 
      {                              
      if(Use_Lo){         
      if(DnBuffer[i+1] == EMPTY_VALUE && DnBuffer[i+2] != EMPTY_VALUE) 
         {
         if (ArrowsON2){UsBuffer[i+1] = (iLow(NULL,0,i+1) - A_Gap2 * Pip * Point);}else UsBuffer[i+1] = 0.1;
         }}
      if(Use_Hi){
      if (UpBuffer[i+1] == EMPTY_VALUE && UpBuffer[i+2] != EMPTY_VALUE) 
         {
         if (ArrowsON1){DwBuffer[i+1] = (iHigh(NULL,0,i+1) + A_Gap1 * Pip * Point);}else DwBuffer[i+1] = 0.1;
         }}
   } ALERTH();ALERTL();
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
void ALERTH() {
   int sig;
   if (AlertsOn1) {
      if (AlertsOnCurrent1) sig = 0;
      else sig = 1;
      if (DwBuffer[sig] != EMPTY_VALUE && DwBuffer[sig+1] == EMPTY_VALUE) AlertsH(sig, "CBL Hi Close");
   }
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
void ALERTL() {
   int sig;
   if (AlertsOn2) {
      if (AlertsOnCurrent2) sig = 0;
      else sig = 1;
      if (UsBuffer[sig] != EMPTY_VALUE && UsBuffer[sig+1] == EMPTY_VALUE) AlertsL(sig, " CBL Lo Close");
   }
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
//+------------------------------------------------------------------+
//| Custom indicator alert send function                             |
//+------------------------------------------------------------------+
void AlertsH(int x, string z) 
{
   string Strconcat;
   if (notice != z || Times != iTime(NULL,0,x)) {
      notice = z; Times = iTime(NULL,0,x);
      Strconcat = StringConcatenate(z," Signal: ",Symbol()," - ",TF()," (",TimeToStr(TimeCurrent(), TIME_SECONDS),")");
      if (AlertsMessage1) Alert(Strconcat);
      if (AlertsNotify1) SendNotification(Strconcat);
      if (AlertsEmail1) SendMail(StringConcatenate(Symbol(), " Signal "), Strconcat);
   }
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
//+------------------------------------------------------------------+
//| Custom indicator alert send function                             |
//+------------------------------------------------------------------+
void AlertsL(int x, string z) 
{
   string Strconcat;
   if (notice != z || Times != iTime(NULL,0,x)) {
      notice = z; Times = iTime(NULL,0,x);
      Strconcat = StringConcatenate(z," Signal: ",Symbol()," - ",TF()," (",TimeToStr(TimeCurrent(), TIME_SECONDS),")");
      if (AlertsMessage2) Alert(Strconcat);
      if (AlertsNotify2) SendNotification(Strconcat);
      if (AlertsEmail2) SendMail(StringConcatenate(Symbol(), " Signal "), Strconcat);
   }
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII//
//+------------------------------------------------------------------+
//| Function: TF String()                                            |
//+------------------------------------------------------------------+
string TF() 
{
  switch(Period()) 
   {
    case PERIOD_M1:  return("M1");  // 1
    case PERIOD_M5:  return("M5");  // 5
    case PERIOD_M15: return("M15"); // 15
    case PERIOD_M30: return("M30"); // 30
    case PERIOD_H1:  return("H1");  // 60
    case PERIOD_H4:  return("H4");  // 240
    case PERIOD_D1:  return("D1");  // 1440
    case PERIOD_W1:  return("W1");  // 10080
    case PERIOD_MN1: return("MN1"); // 43200
   }
  return("current");
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII// 