//+------------------------------------------------------------------+
//|                                                BB_Ema             |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Mod Pacois"//Mod Pacois
#property link      ""//Mod Pacois

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  Lime
#property indicator_color2 Red
#property indicator_color3  Green
#property indicator_color4  Yellow
#property indicator_color5  Red
//#property indicator_level1 0
#property indicator_width1 2
#property indicator_width2 2
//#property indicator_width3 2
//#property indicator_maximum 1.1
//#property indicator_minimum -1.1
extern int  Period_BB=20;
extern ENUM_APPLIED_PRICE PRICE_BB=0;
extern int  Period_Ema=50;
extern ENUM_APPLIED_PRICE PRICE_EMA=0;
extern ENUM_MA_METHOD  MODE_EMA=0;


extern string  ______________SetAlert;
extern bool   ArrowsDn        = false;
extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = false;
extern bool   alertsSound      = true;
extern bool   alertsNotify     = false;
extern bool   alertsEmail      = false;
extern string soundFile        = "alert2.wav";

extern string  _____________SetArrows;
extern bool   arrowsVisible    = true;
extern bool   LineVisible      = false;
extern string arrowsIdentifier = "Arrows_BB_Ema ";
extern double arrowsDisplaceUp = 1.0;
extern double arrowsDisplaceDn = 0.5;
extern color  arrowsUpColor    = Lime;
extern color  arrowsDnColor    = Red;
extern int    arrowsUpCode     = 233;
extern int    arrowsDnCode     = 234;
extern int    arrowsUpSize     = 1;
extern int    arrowsDnSize     = 1;
extern bool   Histo3            = false;

 ENUM_TIMEFRAMES    timeFrame        = 0;
 string _______Alert1Alto________;
 double    TmaDn     = -2.0;
 bool BullOn=false;
 bool SoundBull=false;
 string FileSoundBull = "alert.wav";
 string _______AlertBasso_________;

 double    RsiUp     = 50.0;
 bool BearOn=false;
 bool SoundBear=false;
 string FileSoundBear = "alert2.wav";

 int PlayedSoundHBull = False;
int PlayedSoundLBear = False;
 




string IndicatorName1 = "High_Low_Tma ";



//
//
//
//
//

double rsi1[];
double rsi2[];
double rsi3[];
double rsi4[];
double rsi5[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   timeFrame = MathMax(timeFrame,_Period);
   IndicatorBuffers(6);
   SetIndexBuffer(0,rsi1);//if(Histo1)SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(1,rsi2);if(Histo3)SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(2,rsi3);if(Histo3)SetIndexStyle(2,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(3,rsi4);//if(Histo3)SetIndexStyle(3,DRAW_HISTOGRAM,0,2);
  SetIndexBuffer(4,rsi5);//if(Histo3)SetIndexStyle(3,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(5,trend);
        
  // SetLevelValue(1,  Level1  );
  // SetLevelValue(2,  Level2  );   
 //  SetLevelValue(3,  Level3  );
  // SetLevelValue(4,  Level4  );   
         
  
   return(0);
}
int deinit()
{
   deleteArrows(); 
   
return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit = MathMin(MathMax(Bars-counted_bars,3*timeFrame/_Period),Bars-1);

   //
   //
   //
   //
   //
   //
   

   

   for (i=limit; i >= 0; i--)
   {
     // int y = iBarShift(NULL,timeFrame,Time[i]);
    
      

     
     

  
 
  
    
 
     
    
 rsi1[i]= iBands ( NULL , 0,Period_BB,2,0, PRICE_BB , MODE_HIGH , i)/Point-iMA ( NULL , 0,Period_Ema,0, MODE_EMA , PRICE_EMA , i)/Point ;

 rsi2[i]=  iBands ( NULL , 0,Period_BB,2,0, PRICE_BB , MODE_LOW , i)/Point-iMA ( NULL , 0,Period_Ema,0, MODE_EMA , PRICE_EMA , i)/Point;
  
     
         trend[i] = 0;
         
         
         
         if ( 
         
       
    rsi2[i]<0
   
   
           )
                                            
         trend[i]= 1;
                                             
       // if(ArrowsDn)
         if (    rsi1[i]>0
         )
            // if(ArrowsDn)
         trend[i]=-1;
                                                                                
                                                                                                                                    
            
         manageArrow(i); 
            
            
       
            
            
            
            
            
            
            
    }
    
    //
    //
    //
    //
    //
      
    if (alertsOn)
    {
       if (alertsOnCurrent)
            int whichBar = 0;
       else     whichBar = 1;
       if (trend[whichBar] != trend[whichBar+1])
       if (trend[whichBar] == 1)
             doAlert("Buy");
             //if(ArrowsDn)
       else  doAlert("Sell");       
    }
    
    
     if( Close[0]<Open[0] ){PlayedSoundHBull = False;}
     
   
         if(   rsi4[1]> rsi3[1]  ){PlayedSoundLBear = False;} 
    
             
     if(
     
     Close[0]>Open[0]
    // && rsi3[1]<0
      //  && rsi1[0]>20
       // &&   rsi2[0]>0
        
         && rsi2[0]> rsi3[0]
         
         && rsi1[0]>50

        
         
        // && rsi1[0]>0
        // &&  iCustom(NULL,0, "Low-high bands v1.0",HlPeriod , AvgPeriod,EmaType, 3  ,0)
      //  -iCustom(NULL,0, "TMA nrp + bands + alerts_mtf",4,0)<0
         
        //  &&  Open[0]/Point- iCustom(NULL,0, "ATR_Channels",2,0)/Point>0 
       //  && Close[0]>  iCustom(NULL,0, "FractalLevels",1,0) 
         
         // &&   rsi2[0]>rsi2[1]
     // && rsi1[0]<50
   // &&  rsi1[0]>rsi3[0]&&Close[1]>rsi3[1]
 // && rsi2[0]/Point -   rsi1[0]/Point>1
  // && rsi1[1]>Close[1]
    // && rsi1[1]> rsi1[2]
     
    
      
         && PlayedSoundHBull == False){
     if(BullOn)Alert(Symbol(), " (", Period(), " min)High_Low_TmaUp  |",  " Slope | " , " YellowDn | ",  "RedDn ");
     if (SoundBull) PlaySound(FileSoundBull);
    PlayedSoundHBull = True;}
    
   if(   rsi4[1]< rsi3[1]
    &&   rsi2[1]<TmaDn
     && rsi4[1]- rsi4[2]<-11
      &&  rsi3[0]<20
      && rsi1[0]>50
      
    && PlayedSoundLBear == False){
     if(BearOn)Alert(Symbol(), " (", Period(), " min)High_Low_TmaDn   |",    " Lime | ",   " Red | " , "TmaDn");
    if (SoundBear) PlaySound(FileSoundBear);
    PlayedSoundLBear = True;}  
    
    
    
    
    
    

return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(timeFrame+" "+Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS),"Ultra_Trend_Alert_arrows_Line",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," Ultra_Trend_Alert_arrows_Line " +" "+message));
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"Ultra_Trend_Alert_arrows_Line "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageArrow(int i)
{
   if (arrowsVisible)
   {
     if (trend[i] != trend[i+1])
     {
       deleteArrow(Time[i]);
       if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,arrowsUpSize,false);
       if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode,arrowsDnSize,true);
     }                                            
      
   }
}               

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,int theSize, bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      if(LineVisible)
         ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
         ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode );
         ObjectSet(name,OBJPROP_COLOR,    theColor);
         ObjectSet(name,OBJPROP_WIDTH,    theSize );      
         
         if (up)
              ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsDisplaceDn * gap);      
         else ObjectSet(name,OBJPROP_PRICE1, Low[i] - arrowsDisplaceUp * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}



