//+------------------------------------------------------------------+
//|                                         |
//|                         |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_width1 3
#property indicator_width2 3

double CrossUp[];
double CrossDown[];
int CurrentTrend = 0;
int starttime = 0;
bool last_signal_down;
bool last_signal_up;

extern int BB_value = 14;
extern int Arrow_Pip_Distance = 100;
extern int Show_Alert = 1;
extern int Play_Sound = 1;
extern int Send_Mail = 0;
extern string SoundFilename = "alert.wav";

int init()
  {
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
   starttime = TimeLocal();
   
   return(0);
  }

int deinit()
  {
   Comment(" ");
   return(0);
  }

int start() {
   int limit, i, counter, loop;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);   //---- check for possible errors
   if(counted_bars>0) counted_bars--;   //---- last counted bar will be recounted

   limit=Bars-counted_bars;
   
   loop = 0;
   for(i = 0; i <= limit; i++) {
       
   double currbull = MathAbs(iBullsPower(NULL, 0, BB_value, 0, i));
   double currbear = MathAbs(iBearsPower(NULL, 0, BB_value, 0, i));
   double prevbull = MathAbs(iBullsPower(NULL, 0, BB_value, 0, i+1));
   double prevbear = MathAbs(iBearsPower(NULL, 0, BB_value, 0, +i+1));

       
      if (currbull>currbear && prevbull<prevbear)//ORDER ENTRY CONDITION LONG
         {
         CrossUp[i] = Low[i] - Arrow_Pip_Distance*Point;
         last_signal_down=false;
         last_signal_up=true;

         if ((loop == 0) && (CurrentTrend != 1) && (TimeLocal() - starttime >= 10))  {
            if (Show_Alert == 1)  {
               Alert("(M"+Period()+") "+Symbol()+" Bull-Bear Buy " + Hour()+":"+Minute()+" Ask: "+Ask);
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Bull-Bear MTF Buy " + Symbol() + " " + Hour()+":"+Minute()+" Ask: "+Ask+" (M"+Period()+")"," ");
               }
               
            CurrentTrend = 1;
            }
         
         if (loop == 0)  {
            loop = 1;
            }
      }
      else 
      if (currbull<currbear && prevbull>prevbear)//ORDER ENTRY CONDITION LONG
         {
         CrossDown[i] = High[i] + Arrow_Pip_Distance*Point;
         last_signal_down=true;
         last_signal_up=false;
    
         if ((loop == 0) && (CurrentTrend != -1) && (TimeLocal() - starttime >= 10))  {
            if (Show_Alert == 1)  {
               Alert("(M"+Period()+") "+Symbol()+" Bull-Bear Sell " + Hour()+":"+Minute()+" Bid: "+Bid);
               }
            if (Play_Sound == 1)  {
               PlaySound(SoundFilename);
               }
            if (Send_Mail == 1)  {
               SendMail("Bull-Bear Sell " + Symbol() + " " + Hour()+":"+Minute()+" Bid: "+Bid+" (M"+Period()+")"," ");
               }
               
            CurrentTrend = -1;
            
         }
         if (loop == 0)  {
            loop = 1;
            }
       }
   }
   return(0);
}

