//+---------------------------------------------------------------+
//|                                BBollinger_Sortie_Chart.mq4    |
//|                                      By GGG_Trading           |  
//|                     http://donneeseconomiques.wordpress.com/  |                                                                  |
//+---------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 LightBlue 
#property indicator_color2 LightSkyBlue 
#property indicator_color3 LightSeaGreen
#property indicator_color4 Pink
#property indicator_color5  Lime
#property indicator_color6 Orange
#property indicator_color7 Lime
#property indicator_color8 Orange
#property indicator_width1 2 
#property indicator_width2 2 
#property indicator_width3 2 
#property indicator_width4 2 
#property indicator_width5 1 
#property indicator_width6 1 
#property indicator_width7 2 
#property indicator_width8 2

//---- indicator parameters
enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased   // Heiken ashi trend biased price
};
extern int    BBPer     =  20;
extern double BBDev     =  2.0;
extern int    BBShift   =  0;
extern enPrices BBPrix    =  0;
//iCustom(NULL,0,"BBollinger_Sortie_Chart" BBPer,BBDev,BBShift,BBPrix, x,1);  
extern bool show_MA = true;
extern int  MA_Per = 3;
extern ENUM_MA_METHOD  MA_Meth   = MODE_SMA;
input bool               alertsOn        = false;                // Alerts on true/false?
input bool               alertsOnCurrent = true;                 // Alerts on still opened bar true/false?
input bool               alertsMessage   = true;                 // Alerts pop-up message true/false?
input bool               alertsSound     = false;                // Alerts sound true/false?
input bool               alertsNotify    = false;                // Alerts push notification true/false?
input bool               alertsEmail     = false;                // Alerts email true/false?
input string             soundFile       = "alert2.wav";         // Sound file
//---- buffers
double Moving_bas[];
double Moving_haut[];
double Upper[];
double Lower[];
double Fleche_up[];
double Fleche_down[];
double Fleche_up_2[];
double Fleche_down_2[];
double prices[],trend[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  IndicatorBuffers(10);
  string Nom; Nom = "BBollinger_Sortie_chart"; 
  IndicatorShortName(Nom);
    IndicatorDigits(Digits+2);
//-----   
   SetIndexBuffer(0,Moving_bas);
   SetIndexBuffer(1,Moving_haut );

   if( show_MA == false ){
       SetIndexStyle(0,DRAW_NONE);//LINE, STYLE_SOLID, 2);
       SetIndexStyle(1,DRAW_NONE);//LINE,  STYLE_SOLID, 2 );
      }
   else {    
        SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, 2);
        SetIndexStyle(1,DRAW_LINE,  STYLE_SOLID, 2 );
        } 
//-----   
   SetIndexBuffer(2,Upper);
   SetIndexStyle(2,DRAW_LINE, STYLE_SOLID, 2);
//-----   
   SetIndexBuffer(3,Lower);
   SetIndexStyle(3,DRAW_LINE, STYLE_SOLID, 2);
//-----   
   SetIndexBuffer(4, Fleche_up );
   SetIndexStyle(4, DRAW_ARROW);//, STYLE_SOLID,2);
   SetIndexArrow(4,233);
   SetIndexEmptyValue(4,0.0);
   SetIndexLabel(4,"Fleche_up");
//-----   
   SetIndexBuffer(5,Fleche_down);
   SetIndexStyle(5, DRAW_ARROW);//, STYLE_SOLID,2);
   SetIndexArrow(5,234);
   SetIndexEmptyValue(5,0.0);// 
   SetIndexLabel(5,"Fleche_down");
//-----   
   SetIndexBuffer(6, Fleche_up_2 );
   SetIndexStyle(6, DRAW_ARROW);//, STYLE_SOLID,2);
   SetIndexArrow(6,233);
   SetIndexEmptyValue(6,0.0);
   SetIndexLabel(6,"Fleche_up_2");
//-----   
   SetIndexBuffer(7,Fleche_down_2);
   SetIndexStyle(7, DRAW_ARROW);//, STYLE_SOLID,2);
   SetIndexArrow(7,234);
   SetIndexEmptyValue(7,0.0);// 
   SetIndexLabel(7,"Fleche_down_2");
   SetIndexBuffer(8,prices);
   SetIndexBuffer(9,trend);
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
//----------------------------------------------------- 
   int i,ii, counted_bars;             int History = 501;
   counted_bars = IndicatorCounted() ;
   if(counted_bars < 0 ) return(0);
   if(Bars < 100 ) return(0); 
//----------------------------------------------------- 
   if( counted_bars <= 500 )     i = Bars - 50;
   if( counted_bars >  500 )     i = Bars-counted_bars-1;   
   if( i > History )             i = History;      
//------------------------------------------------------- 11 --
   ii =i;
   while( i >= 0 ) { prices[i] = getPrice(BBPrix,Open,Close,High,Low,i); i--; } i = ii; 
   while( i >= 0 )                      
     {
       double atr = iATR(NULL,0 ,500,i);
       double dev = iStdDevOnArray(prices,0,BBPer,BBShift,MODE_SMA,i);
       double sma = iMAOnArray(prices,0,BBPer,BBShift,MODE_SMA,i);
           
      Upper[i] = sma+dev*BBDev;   
      Lower[i] = sma-dev*BBDev;   


         Moving_haut[i] =iMA(NULL, 0, MA_Per,0,MA_Meth,2,i); 
         Moving_bas[i]  =iMA(NULL, 0, MA_Per,0,MA_Meth,3,i); 
      
      trend[i] = 0;   
      if( Moving_haut[i+1] > Upper[i+1] &&  Moving_haut[i] < Upper[i])
                     { Fleche_down[i] = Moving_haut[i]+ atr; trend[i] = -1; } 
      else             Fleche_down[i] = 0.0;
  
    if( Moving_bas[i+1] < Lower[i+1] &&  Moving_bas[i] > Lower[i])
                     { Fleche_up[i] = Moving_bas[i] - atr;  trend[i] = 1; }
      else             Fleche_up[i] = 0.0;
  
     //----
     i--;
     }
//----
    if (alertsOn)
    {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0; 
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(" up");
         if (trend[whichBar] ==-1) doAlert(" down");       
       }         
    }      
   return(0);
  }
//+------------------------------------------------------------------+

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//

double workHa[][4];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars);
         int r = Bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen;
         if (r>0)
                haOpen  = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
         else   haOpen  = (open[i]+close[i])/2;
         double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
         double haHigh  = MathMax(high[i], MathMax(haOpen,haClose));
         double haLow   = MathMin(low[i] , MathMin(haOpen,haClose));

         if(haOpen  <haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else                 { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                                workHa[r][instanceNo+2] = haOpen;
                                workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (tprice)
         {
            case pr_haclose:     return(haClose);
            case pr_haopen:      return(haOpen);
            case pr_hahigh:      return(haHigh);
            case pr_halow:       return(haLow);
            case pr_hamedian:    return((haHigh+haLow)/2.0);
            case pr_hamedianb:   return((haOpen+haClose)/2.0);
            case pr_hatypical:   return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:  return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:   return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (tprice)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
   }
   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 = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" BBollinger_Sortie_Chart "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" BBollinger_Sortie_Chart ",message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
