// Version Notes  
//   A1       Orginal idea from Barracuda and coded by RJD007(Rookie)
//   V1.00    Modified by Tootall to only allow one alert
//   V1.01    Added Ontimer to help speed up signals
//   V1.02    Moved DCbands to init section so it is only called once

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Red

double BufferUp[];
double BufferDn[];
double BufferDir[];
double BufferRSI[];
extern int ChannelPeriod = 1;
extern int Shift = 1;
extern int MAPeriod = 1;
extern int MAMethod = 1;
extern int MAAppliedPrice = 1;
extern double dRangeDivider = 0.25;
extern int U_Trigger = 50;
extern int L_Trigger = -50;
extern bool Alerts = true;
extern bool Multi_Alerts = true;
int ArrowDirection = 0;
bool ArrowUp = FALSE;
bool ArrowDn = FALSE;
bool Startup = False;
double GVar0 = 0.0;
//********************
int Zero = 2147483647;
bool limitDone=false;
//********************

string note =  "======= Authentication SETTINGS ======";
string email = "mail@test.com";
string CBR = "FIB11XXX";
int cp = 2;
int shift = 2;
int MAP = 2;
int MAM = 1;
int MAAP = 5;

int init() {
   SetIndexStyle(0, DRAW_ARROW, EMPTY,5);
   SetIndexArrow(0, SYMBOL_ARROWUP);
   SetIndexBuffer(0, BufferUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,5);
   SetIndexArrow(1, SYMBOL_ARROWDOWN);
   SetIndexBuffer(1, BufferDn);
   SetIndexBuffer(2, BufferDir);
   SetIndexBuffer(3, BufferRSI);
   GVar0 = iCustom(NULL, 0, "FIB - DCBands",note,email,CBR,cp,shift,MAP,MAM,MAAP,0,1);
   bool good = EventSetTimer(1);
   return (0);
}

int deinit() {
   EventKillTimer();
   return (0);
}

int start(){
  int ret = update_Indi();
  return(0);
}

void OnTimer(){
  if(Startup) int ret = update_Indi();
}


int update_Indi() {
   double Var1,Var2,Var3;
   int j;
   double pointer_dist;
   double CandleLength;
   
   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   for (int i = limit; i >= 1; i--) {
      Var1 = iCustom(NULL, 0, "FIB - DCDots",ChannelPeriod,Shift,MAPeriod,MAMethod,MAAppliedPrice,dRangeDivider,1,i);
      Var2 = iCustom(NULL, 0, "FIB - DCDots",ChannelPeriod,Shift,MAPeriod,MAMethod,MAAppliedPrice,dRangeDivider,0,i);
      BufferDir[i] = BufferDir[i+1];
 //I prefer to use Zero = 2147483647 **************************
 //Sometimes buffers are pre-filled so does not always work.
      if(Var1 != Zero){
        BufferDir[i] = 1;
        ArrowDn = FALSE;
        ArrowUp = FALSE;
        ArrowDirection = 0;
        }
      if(Var2 != Zero){
        BufferDir[i] = -1;
        ArrowDn = FALSE;
        ArrowUp = FALSE;
        }
 //***********************************************************     
      Var3 = (iRSI(NULL, 0, 2, PRICE_CLOSE, i) - 50.0) * 1.5;
      BufferRSI[i] = 0;
      if(Var3 < L_Trigger) BufferRSI[i] = 1;
      if(Var3 > U_Trigger) BufferRSI[i] = -1;
     }
   for (i = limit; i >= 1; i--) { 
      j = i;
      pointer_dist = 0;
      CandleLength = 0;
      for (j = i; j <= i + 9; j++) 
         CandleLength += MathAbs(High[j] - Low[j]);
      pointer_dist = CandleLength / 10.0; 
      
      if(Multi_Alerts) ArrowDirection = 0;
      if (BufferDir[i]==1  && BufferDir[i+1]==1  && BufferDir[i+2]==1  && BufferRSI[i] == 1 && ArrowDirection != 1
         && !limitDone) {
         BufferUp[i] = Low[i] - 1.3 * pointer_dist;
        // Print(i," ",Alerts," ",!ArrowUp);
  //****Use global to know if limit has been breached
         limitDone=True;
         if (i <= 1 && Alerts && (!ArrowUp)) {
            Alert(Symbol(), " ", Period(), "  Barracuda Signal:CALL NOW!");
            ArrowUp = TRUE;
            ArrowDn = FALSE;
         }
         ArrowDirection = 1;
      } else {
         if (BufferDir[i]==-1 && BufferDir[i+1]==-1 && BufferDir[i+2]==-1  && BufferRSI[i] == -1 && ArrowDirection != 2
            && !limitDone) {
            BufferDn[i] = High[i] + 1.3 * pointer_dist;
           // Print(i," ",Alerts," ",!ArrowDn);
   //****Use global to know if limit has been breached
            limitDone=True;
            if (i <= 1 && Alerts && (!ArrowDn)) {
               Alert(Symbol(), " ", Period(), "   Barracuda Signal:PUT NOW!");
               ArrowDn = TRUE;
               ArrowUp = FALSE;
            }
            ArrowDirection = 2;
         }
      }
      //Changed direction so reset to false.
      if(BufferDir[i]!=BufferDir[i+1])limitDone=false;
   }
   Startup = true;
   return (0);
}
