 
#property copyright "Anothers MA to the link. God bless everyone!"
#property link      "https://www.forexfactory.com/thread/post/13823337#post13823337"
#property strict

#property indicator_chart_window
#property indicator_buffers 3
#property strict


extern string             MA_1       = "===== MA 1 ====";
extern int                ma1_length = 3;            // MA 1 Length
extern ENUM_MA_METHOD     ma1_method = MODE_SMA;     // MA 1 Method 
extern ENUM_APPLIED_PRICE ma1_ap_Pri = PRICE_CLOSE;  // MA 1 Price
extern color              ma1_color  = clrOrange;    // MA 1 Color
extern int                ma1_shift  = 3;            // MA 1 Shift
extern int                ma1_width  = 1;            // MA 1 Line Width

extern string             MA_2       = "===== MA 2 ====";
extern int                ma2_length = 7;            // MA 2 Length
extern ENUM_MA_METHOD     ma2_method = MODE_SMA;     // MA 2 Method
extern ENUM_APPLIED_PRICE ma2_ap_Pri = PRICE_CLOSE;  // MA 2 Price
extern color              ma2_color  = clrDeepPink;  // MA 2 Color
extern int                ma2_shift  = 5;            // MA 2 Shift
extern int                ma2_width  = 1;            // MA 2 Line Width
 
extern string             MA_3       = "===== MA 3 ====";
extern int                ma3_length = 25;           // MA 3 Length
extern ENUM_MA_METHOD     ma3_method = MODE_SMA;     // MA 3 Method
extern ENUM_APPLIED_PRICE ma3_ap_Pri = PRICE_CLOSE;  // MA 3 Price
extern color              ma3_color  = clrPaleGreen; // MA 3 Color
extern int                ma3_shift  = 5;            // MA 3 Shift
extern int                ma3_width  = 1;            // MA 3 Line Width
 
 
extern string             Button_Settings       = "== Button Settings ==";
//extern string             Text  = "3MA";             // Display Text
extern string             DisplayID  = "3MA";        // Display id
extern ENUM_BASE_CORNER   Corner     = CORNER_LEFT_UPPER;
extern int                button_x   = 0;            // Button Horizontal Location
extern int                button_y   = 130;           // Button Vertical Location
extern int                button_width  = 80;        // Button Width 
extern int                button_height = 20;        // Button Height
extern color              button_color  = clrWhite;  // Button Fore Color
extern color              button_back_color  = clrBlack; // Button Background Color

double ma1[], ma2[], ma3[];

string GetEma()
{
string rret ="";

if(ma1_method == MODE_SMA) {rret = "S";}
if(ma1_method == MODE_EMA) {rret = "E";}
if(ma1_method == MODE_SMMA){rret = "Sm";}
if(ma1_method == MODE_LWMA){rret = "L";}

return rret;
}


string GetEma2()
{
string rret ="";

if(ma2_method == MODE_SMA) {rret = "S";}
if(ma2_method == MODE_EMA) {rret = "E";}
if(ma2_method == MODE_SMMA){rret = "Sm";}
if(ma2_method == MODE_LWMA){rret = "L";}

return rret;
}

string GetEma3()
{
string rret ="";

if(ma3_method == MODE_SMA) {rret = "S";}
if(ma3_method == MODE_EMA) {rret = "E";}
if(ma3_method == MODE_SMMA){rret = "Sm";}
if(ma3_method == MODE_LWMA){rret = "L";}

return rret;
}


int OnInit()
{
  
  
 // in M5 am vzt ca merge bine cu 3,6,9 EMA ...toate cu Shift 0
  
//  if (ObjectFind(DisplayID)!=0)
//  {
//         
         ObjectDelete(ChartID(),DisplayID);
         ObjectCreate(ChartID(),DisplayID,OBJ_BUTTON,0,0,0);
         //ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,ma1_length + "." + ma1_shift + GetEma());
         ObjectSetString(ChartID(),DisplayID, OBJPROP_TOOLTIP, ma1_length + "." + ma1_shift + GetEma() + " " + ma2_length + "." + ma2_shift + GetEma2()+ " " + ma3_length + "." + ma3_shift + GetEma3());
         //ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,Text);
         ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,ma1_length + "/" + ma2_length + "/" + ma3_length);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_FONTSIZE,8);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_CORNER,Corner);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_COLOR,button_color);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_BGCOLOR,button_back_color);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_XDISTANCE,button_x);
         ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YDISTANCE,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);  
  SetIndexBuffer(2, ma3);  
    
           
  if (GetButtonState(DisplayID) == "on")
  {
    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);
    SetIndexBuffer(2, ma3);  SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,ma3_width,ma3_color); SetIndexShift(2,ma3_shift);
     
  }
  else 
  {
    for (int i=0; i<3; i++) 
        {
         SetIndexStyle(i,DRAW_NONE);
        }
  }
  
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{ 
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); prevState=newState; }
            else  { SetIndexStyle(0,DRAW_LINE); SetIndexStyle(1,DRAW_LINE);SetIndexStyle(2,DRAW_LINE); prevState=newState; }
            //ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,"MA's "+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,ma1_ap_Pri,i - ma1_shift);
               ma2[i] = iMA(_Symbol,_Period,ma2_length,ma2_shift,ma2_method,ma2_ap_Pri,i - ma2_shift);
               ma3[i] = iMA(_Symbol,_Period,ma3_length,ma3_shift,ma3_method,ma3_ap_Pri,i - ma3_shift);
     
             }
      }
      
   return(rates_total);
}


string GetButtonState(string whichbutton)
{
      bool selected = ObjectGetInteger(ChartID(),whichbutton,OBJPROP_STATE);
      if (selected)
           { return ("on"); } 
      else { return ("off");}
}

