//+------------------------------------------------------------------+
//|                                                BBMA By Nbr42.mq4 |
//|                                https://www.forexfactory.com/nbr42|
//+------------------------------------------------------------------+
#property  copyright "Nbr42"
#property  link      "https://www.forexfactory.com/nbr42"

#property indicator_chart_window
#property indicator_buffers 8
#property strict
extern int  MA1Period   =5,     
            MA1Method   =MODE_LWMA,
            MA1Price    =PRICE_HIGH,
            MA1shift    =0,
            MA2Period   =5,
            MA2Method   =MODE_LWMA,
            MA2Price    =PRICE_LOW,
            MA2shift    =0,
            MA3Period   =10,     
            MA3Method   =MODE_LWMA,
            MA3Price    =PRICE_HIGH,
            MA3shift    =0,
            MA4Period   =10,     
            MA4Method   =MODE_LWMA,
            MA4Price    =PRICE_LOW,
            MA4shift    =0,
            MA5Period   =50,
            MA5Method   =MODE_EMA,
            MA5Price    =PRICE_CLOSE,
            MA5shift    =0,
            BBprd       = 20,
            BBdev       = 2,
            Limit_Bar   = 00;
input string         DisplayID  = "EMA x 3";  // Display id
input int            button_x   = 50;        // Horizontal location
input int            button_y   = 1;        // Vertical location            
            
extern color  BBcol     = clrGray;
extern color  MAcol     = clrSkyBlue;
double buffer1[], buffer2[], buffer3[],
       buffer4[], buffer5[], buffer6[],
       buffer7[], buffer8[];

//+------------------------------------------------------------------+

int OnInit()
{
  if (ObjectFind(DisplayID)!=0)
  {
      ObjectCreate(ChartID(),DisplayID,OBJ_BUTTON,0,0,0);
         ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,"EMA On");
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_FONTSIZE,8);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_CORNER,2);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_COLOR,clrWhite);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_BGCOLOR,clrDimGray);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YDISTANCE,button_x);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_XDISTANCE,button_y);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_XSIZE,50);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YSIZE,20);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_HIDDEN,true);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_STATE,true);
  }
  
  SetIndexBuffer(0,buffer1);
   SetIndexBuffer(1,buffer2);
   SetIndexBuffer(2,buffer3);
   SetIndexBuffer(3,buffer4);
   SetIndexBuffer(4,buffer5);
   SetIndexBuffer(5,buffer6);
   SetIndexBuffer(6,buffer7);
   SetIndexBuffer(7,buffer8);
  
  if (GetButtonState(DisplayID)!="off")
  {
    SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_LINE, 0, 2, clrMediumOrchid);
   SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_LINE, 0, 2, clrMediumOrchid);
   SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_LINE, 0, 2, clrGainsboro);
   SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_LINE, 0, 2, clrGainsboro);
   SetIndexBuffer(4,buffer5); SetIndexStyle(4,DRAW_LINE, 0, 2, MAcol);
   SetIndexBuffer(5,buffer6); SetIndexStyle(5,DRAW_LINE, 0, 2, BBcol);
   SetIndexBuffer(6,buffer7); SetIndexStyle(6,DRAW_LINE, 0, 2, BBcol);
   SetIndexBuffer(7,buffer8); SetIndexStyle(7,DRAW_LINE, 0, 2, BBcol);
   
  }
  else for (int i=0; i<5; i++) SetIndexStyle(i,DRAW_NONE);
return(INIT_SUCCEEDED);
}
  
void OnDeinit(const int reason)
{ 
      switch(reason)
      {
         case REASON_PARAMETERS  :
         case REASON_CHARTCHANGE :
         case REASON_RECOMPILE   :
         case REASON_CLOSE       : break;
         default :
         {
            ObjectDelete(DisplayID);
         }                  
      }
}
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
   static string prevState ="";
   if (id==CHARTEVENT_OBJECT_CLICK && sparam==DisplayID)
   {
      string newState = GetButtonState(DisplayID);
         if (newState!=prevState)
         if (newState=="off")
                  { SetIndexStyle(0,DRAW_NONE); SetIndexStyle(1,DRAW_NONE); SetIndexStyle(2,DRAW_NONE); SetIndexStyle(3,DRAW_NONE); SetIndexStyle(4,DRAW_NONE); prevState=newState;SetIndexStyle(5,DRAW_NONE); prevState=newState; SetIndexStyle(6,DRAW_NONE); prevState=newState; SetIndexStyle(7,DRAW_NONE); prevState=newState; }
            else  { SetIndexStyle(0,DRAW_LINE); SetIndexStyle(1,DRAW_LINE); SetIndexStyle(2,DRAW_LINE); SetIndexStyle(3,DRAW_LINE); SetIndexStyle(4,DRAW_LINE); prevState=newState; SetIndexStyle(5,DRAW_LINE); SetIndexStyle(6,DRAW_LINE); SetIndexStyle(7,DRAW_LINE); prevState=newState;}
            ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,"EMA "+newState);
   }
   
} 

int  OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
                
{
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1; 
   
   //
   //
   //
   
   for (; i>=0 && !_StopFlag; i--)
   {
      buffer1[i] = iMA(NULL,0,MA1Period, MA1shift, MA1Method,MA1Price,i);
      buffer2[i] = iMA(NULL,0,MA2Period, MA2shift, MA2Method,MA2Price,i);
      buffer3[i] = iMA(NULL,0,MA3Period, MA3shift, MA3Method,MA3Price,i);
      buffer4[i] = iMA(NULL,0,MA4Period, MA4shift, MA4Method,MA4Price,i);
      buffer5[i] = iMA(NULL,0,MA5Period, MA5shift, MA5Method,MA5Price,i);
      buffer6[i] = iBands(NULL,0, BBprd, BBdev, 0, PRICE_CLOSE, 0, i);
      buffer7[i] = iBands(NULL,0, BBprd, BBdev, 0, PRICE_CLOSE, 1, i);
      buffer8[i] = iBands(NULL,0, BBprd, BBdev, 0, PRICE_CLOSE, 2, i);
   }
return(rates_total);
}
                
//+------------------------------------------------------------------+
string GetButtonState(string whichbutton)
{
      bool selected = ObjectGetInteger(ChartID(),whichbutton,OBJPROP_STATE);
      if (selected)
           { return ("on"); } 
      else { return ("off");}
} 

