#property strict

#property indicator_chart_window
#property indicator_buffers 2
#property strict

input ENUM_MA_METHOD ma1_method = MODE_EMA;  // MA 1 Method
input int            ma1_length = 55;         // MA 1 Length
input int            ma1_width  = 2;         // MA 1 Line Width
input int            ma1_shift  = 0;         // MA 1 Shift
input ENUM_APPLIED_PRICE ma_Applyto1=PRICE_HIGH; // MA 1 Apply to
input color          ma1_color  = clrGreen;  // MA 1 Color

input ENUM_MA_METHOD ma2_method = MODE_EMA;  // MA 2 Method
input int            ma2_length = 55;        // MA 2 Length
input int            ma2_width  = 2;         // MA 2 Line Width
input int            ma2_shift  = 0;         // MA 2 Shift
input ENUM_APPLIED_PRICE ma_Applyto2=PRICE_LOW; // MA 2 Apply to
input color          ma2_color  = clrGreen;    // MA 2 Color

input string         DisplayID   = "MA55";  // Display id
input int            button_x    = 0;        // Horizontal location
input int            button_y    = 200;        // Vertical location
input int            button_width= 45;        //Button Width
input int            button_height=15;        //Button Height
input int            button_corner=1;

double ma1[], ma2[];

int OnInit()
{
   
  if (ObjectFind(DisplayID)!=0)
  {
      ObjectCreate(ChartID(),DisplayID,OBJ_BUTTON,0,0,0);
         ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,DisplayID);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_FONTSIZE,8);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_CORNER,button_corner);
         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,button_width);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YSIZE,button_height);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_SELECTABLE,false);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_HIDDEN,true);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_STATE,true);
  }
  
  SetIndexBuffer(0, ma1); 
  SetIndexBuffer(1, ma2);  
  
  //
  //
  //
           
  if (GetButtonState(DisplayID)!="off")
  {
    SetIndexBuffer(0, ma1);  SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,ma1_width,ma1_color); SetIndexShift(0,ma1_shift);
    SetIndexBuffer(1, ma2);  SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,ma2_width,ma2_color); SetIndexShift(1,ma2_shift);
   
  }
  else for (int i=0; i<2; i++) SetIndexStyle(i,DRAW_NONE);
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{ 
      switch(reason)
      {
         case REASON_PARAMETERS  : ObjectDelete(DisplayID);//Alert("REASON_PARAMETERS");
         case REASON_CHARTCHANGE :
         case REASON_RECOMPILE   :
         case REASON_CLOSE       : break;
         default :
         {
            ObjectDelete(DisplayID);
         }                  
      }
      //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); ObjectSetInteger(ChartID(),DisplayID,OBJPROP_COLOR,clrBlack); prevState=newState; }
            else  { SetIndexStyle(0,DRAW_LINE); SetIndexStyle(1,DRAW_LINE); ObjectSetInteger(ChartID(),DisplayID,OBJPROP_COLOR,clrWhite); prevState=newState; }
            //ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,DisplayID+" "+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--)
   {
      ma1[i] = iMA(_Symbol,_Period,ma1_length,ma1_shift,ma1_method,ma_Applyto1,i);
      ma2[i] = iMA(_Symbol,_Period,ma2_length,ma2_shift,ma2_method,ma_Applyto2,i);
   }
return(rates_total);
}

//
//
//

string GetButtonState(string whichbutton)
{
      bool selected = ObjectGetInteger(ChartID(),whichbutton,OBJPROP_STATE);
      if (selected)
           { return ("on"); } 
      else { return ("off");}
}

