//+------------------------------------------------------------------+
//|                                       RSI_BB_Bars_indicator .mq4 |
//|                                                         batttoot |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "batttoot"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window 

#property indicator_minimum -30
#property indicator_maximum 30
#property indicator_buffers 3
#property indicator_color1 clrGray
#property indicator_color2 clrRed
#property indicator_color3 clrGreen
  
extern string   Rsi_setup  = "-----< Rsi_setup2 >-----"; 
extern ENUM_APPLIED_PRICE RSI_price=PRICE_CLOSE;
extern int     RSI_period =14;
extern int      RSI_level_Up =60;
extern int      RSI_level_Down =40;
extern int      RSI_level_Up2 =80;
extern int      RSI_level_Down2 =20;
     
 extern string BBand_SetUP  = "------< BBand_SetUP >------";
extern int    BB_period        = 14;       
extern double BB_deviation     = 2;  
extern int    bands_shift      = 0; 
extern ENUM_APPLIED_PRICE price_mode =PRICE_CLOSE;   
extern string ATR_SetUP  = "------< ATR_SetUP >------";
extern int    ATR_period        = 14;  
extern double    ATR_Minimum  = 0.060;
 
string          arrowsIdentifier                  = "filterArrows";//Arrows Identifier 
input double    arrowsDisplacement          =1;           //Arrows Displacement
input double    arrowsDisplacement2          =1;           //Arrows Displacement Strongsignal
input color     arrowsUpColor               = Blue;          //Arrows Up Color      
input color     arrowsDnColor               = Red;           //Arrows Down Color
input color     arrowsUpColorStrong               = Yellow;         
input color     arrowsDnColorStrong               = Magenta;           
extern string     Alert_Setup = "------< Alert_Setup >------";
extern bool       DoAlert = true ;
extern bool       DoSoundAlert =true;
extern bool       DoMobileAlert =true;
extern bool       DoEmailAlert =true;
 //---- input parameters
 
 
 string sAlertMsg;
datetime lastAlertTime;
datetime lastAlertTime2;
datetime lastAlertTime3;
double buyprice;
double sellprice;
 //---- buffers
double up[];
double down[]; 
double ExtVolumesBuffer[];
double ExtVolumesUpBuffer[];
double ExtVolumesDownBuffer[];
double  VolumesAverage[];
double cnt ,avergma;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  SetIndexBuffer(0,ExtVolumesBuffer);       
   SetIndexBuffer(1,ExtVolumesUpBuffer);
   SetIndexBuffer(2,ExtVolumesDownBuffer);
   
   
   
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2,indicator_color1);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2,indicator_color2);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,2,indicator_color3);
     
//---- sets default precision format for indicators visualization
   IndicatorDigits(0);   
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("RSI_BB_Bars_indicator");
   SetIndexLabel(0,"RSI_BB_Bars_indicator");      
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
//---- sets drawing line empty value
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(2,0.0);       
 
 
 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
  
  
ChartRedraw();
 int    i,nLimit,nCountedBars;
//---- bars count that does not changed after last indicator launch.
   nCountedBars=IndicatorCounted();
//---- last counted bar will be recounted
   if(nCountedBars>0) nCountedBars--;
   nLimit=Bars-nCountedBars;
//----
   for(i=0; i<nLimit; i++)
     {
       double dVolume =  iRSI  ( Symbol() ,0 ,RSI_period, RSI_price, i); // blue
       double upperBB =  iBands( Symbol() ,0,BB_period,BB_deviation, bands_shift,price_mode, MODE_UPPER, i) ;
       double lowerBB =  iBands( Symbol() ,0,BB_period,BB_deviation, bands_shift,price_mode, MODE_LOWER, i); 
       double Atr     =  iATR  ( Symbol(),0,ATR_period,i);
    
         double Barval=   dVolume -50;
    
      if(dVolume!=0 )
         {
         
         ExtVolumesBuffer[i]=Barval;
      //   ExtVolumesUpBuffer[i]=0;
      //    ExtVolumesDownBuffer[i]=0;

         }
     
         
      
         if(dVolume>RSI_level_Up && Close[i]>=upperBB && Atr>=ATR_Minimum)
         {
           ExtVolumesBuffer[i]=Barval;
         ExtVolumesUpBuffer[i]=Barval;
         //up[i]=High[i]+arrowsDisplacement;
          ShowArrows(High[i]+(arrowsDisplacement*Point),Time[i],arrowsDnColor, 234, 1,false,i);
         if(i==0){ Alertsell(i);}
         // ExtVolumesDownBuffer[i]=0;
  
         }
         
        
          if(dVolume<RSI_level_Down && Close[i]<=lowerBB&& Atr>=ATR_Minimum)
         {
           ExtVolumesBuffer[i]=Barval;
         
          ExtVolumesDownBuffer[i]=Barval;
          
           ShowArrows(Low[i]-(arrowsDisplacement*Point),Time[i],arrowsUpColor, 233, 1,false,i);
                 if(i==0){    Alertbuy(i);}
         }
        
        
        
        
         if(dVolume>RSI_level_Up2 && Close[i]>=upperBB&& Atr>=ATR_Minimum)
         {
         
          ShowArrows(High[i]+(arrowsDisplacement2*Point),Time[i],arrowsDnColorStrong, 78, 1,false,i);
                   if(i==0){ Alertsell(i);}
  
         }
         
        
          if(dVolume<RSI_level_Down2 && Close[i]<=lowerBB&& Atr>=ATR_Minimum)
         {
           
           ShowArrows(Low[i]-(arrowsDisplacement2*Point) ,Time[i],arrowsUpColorStrong, 78, 1,false,i);
              if(i==0){Alertbuy(i);}
         }
          
        
     
     }   
    
//----
   return(0);
  }
//+-------------------


void Alertsell(int ind)
{
  if(DoSoundAlert==true&& lastAlertTime3!=Time[0])
      {
         sAlertMsg= Symbol()+" :Sell Signal @  " + DoubleToStr(Close[ind],Digits);
            Alert(sAlertMsg,sAlertMsg);
            lastAlertTime3 = Time[0];  
      }
   if (DoEmailAlert&& lastAlertTime!=Time[0])
         {
         sAlertMsg= Symbol()+" :Sell Signal @  " + DoubleToStr(Close[ind],Digits);
              SendMail(sAlertMsg,sAlertMsg);
            lastAlertTime = Time[0];  
          }
    if (DoMobileAlert   && lastAlertTime2!=Time[0])
         {
         sAlertMsg= Symbol()+" :Sell Signal @  " + DoubleToStr(Close[ind],Digits);
              SendNotification(sAlertMsg);
            lastAlertTime2 = Time[0];  
          }

}



void Alertbuy(int ind)
{
  if(DoSoundAlert==true&& lastAlertTime3!=Time[0])
      {
         sAlertMsg= Symbol()+"  :Buy Signal @  " + DoubleToStr(Close[ind],Digits);
            Alert(sAlertMsg,sAlertMsg);
            lastAlertTime3 = Time[0];  
      }
   if (DoEmailAlert&& lastAlertTime!=Time[0])
         {
         sAlertMsg= Symbol()+"  :Buy Signal @  " + DoubleToStr(Close[ind],Digits);
              SendMail(sAlertMsg,sAlertMsg);
            lastAlertTime = Time[0];  
          }
    if (DoMobileAlert   && lastAlertTime2!=Time[0])
         {
         sAlertMsg= Symbol()+"  :Buy Signal @  " + DoubleToStr(Close[ind],Digits);
              SendNotification(sAlertMsg);
            lastAlertTime2 = Time[0];  
          }

}


 void ShowArrows(double price , datetime Ai_0, color A_color_4, int Ai_8, int A_width_12, bool Ai_16 ,int index) {
   string name_20 = TimeToStr(Time[index],TIME_DATE|TIME_MINUTES) ;
   
   ObjectCreate(name_20, OBJ_ARROW, 0,  Ai_0 , 0);
   ObjectSet(name_20, OBJPROP_ARROWCODE, Ai_8);
   ObjectSet(name_20, OBJPROP_COLOR, A_color_4);
   ObjectSet(name_20, OBJPROP_WIDTH, A_width_12);
   if(Ai_16) {
      ObjectSet(name_20, OBJPROP_PRICE1, price + arrowsDisplacement * Point);
      return;
   }
   ObjectSet(name_20, OBJPROP_PRICE1, price - arrowsDisplacement * Point);
}
