//+------------------------------------------------------------------+
//|                                                 5EMAs_RBv1.01.mq4 |
//| Draws 5 customizable Moving Averages with toggle                 |
//+------------------------------------------------------------------+
#property copyright "For Master Robinhood"
#property link      "https://www.forexfactory.com/thread/917569-trading-made-simpler"
#property version   "1.03"
#property strict
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   5

//--- Enum for line width
enum ENUM_LINE_WIDTH
{
   WIDTH_1 = 1, // Width 1
   WIDTH_2 = 2, // Width 2
   WIDTH_3 = 3, // Width 3
   WIDTH_4 = 4, // Width 4
   WIDTH_5 = 5  // Width 5
};

//--- Indicator parameters
//--- MA1 Settings
input string   MA1_Label = "MA1 Settings"; // MA1 Settings
input int      MAPeriod1 = 3;          // MA 1 Period
input ENUM_MA_METHOD MAMethod1 = MODE_EMA; // MA 1 Method
input ENUM_APPLIED_PRICE MAPrice1 = PRICE_CLOSE; // MA 1 Applied Price
input int      MAShift1 = 0;           // MA 1 Shift
input color    MAColor1 = clrGold;     // MA 1 Color
input ENUM_LINE_WIDTH MAWidth1 = WIDTH_1; // MA 1 Width

//--- MA2 Settings
input string   MA2_Label = "MA2 Settings"; // MA2 Settings
input int      MAPeriod2 = 8;          // MA 2 Period
input ENUM_MA_METHOD MAMethod2 = MODE_EMA; // MA 2 Method
input ENUM_APPLIED_PRICE MAPrice2 = PRICE_CLOSE; // MA 2 Applied Price
input int      MAShift2 = 0;           // MA 2 Shift
input color    MAColor2 = clrMagenta;  // MA 2 Color
input ENUM_LINE_WIDTH MAWidth2 = WIDTH_2; // MA 2 Width

//--- MA3 Settings
input string   MA3_Label = "MA3 Settings"; // MA3 Settings
input int      MAPeriod3 = 21;         // MA 3 Period
input ENUM_MA_METHOD MAMethod3 = MODE_EMA; // MA 3 Method
input ENUM_APPLIED_PRICE MAPrice3 = PRICE_CLOSE; // MA 3 Applied Price
input int      MAShift3 = 0;           // MA 3 Shift
input color    MAColor3 = clrAqua;     // MA 3 Color
input ENUM_LINE_WIDTH MAWidth3 = WIDTH_2; // MA 3 Width

//--- MA4 Settings
input string   MA4_Label = "MA4 Settings"; // MA4 Settings
input int      MAPeriod4 = 34;         // MA 4 Period
input ENUM_MA_METHOD MAMethod4 = MODE_EMA; // MA 4 Method
input ENUM_APPLIED_PRICE MAPrice4 = PRICE_CLOSE; // MA 4 Applied Price
input int      MAShift4 = 0;           // MA 4 Shift
input color    MAColor4 = clrBlue;     // MA 4 Color
input ENUM_LINE_WIDTH MAWidth4 = WIDTH_2; // MA 4 Width

//--- MA5 Settings
input string   MA5_Label = "MA5 Settings"; // MA5 Settings
input int      MAPeriod5 = 55;         // MA 5 Period
input ENUM_MA_METHOD MAMethod5 = MODE_EMA; // MA 5 Method
input ENUM_APPLIED_PRICE MAPrice5 = PRICE_CLOSE; // MA 5 Applied Price
input int      MAShift5 = 0;           // MA 5 Shift
input color    MAColor5 = clrOrange;   // MA 5 Color
input ENUM_LINE_WIDTH MAWidth5 = WIDTH_3; // MA 5 Width

//--- Button Settings
input string   Button_Label = "Button Settings"; // Button Settings
input int      ButtonX = 325;          // Button X Coordinate
input int      ButtonY = 1;            // Button Y Coordinate

//--- Indicator buffers
double MA1Buffer[];
double MA2Buffer[];
double MA3Buffer[];
double MA4Buffer[];
double MA5Buffer[];

//--- Global variables
bool ShowMAs = true;                   // Toggle state for MAs
string IndicatorName = "5EMAs_RBv1.01";
string ButtonName;

//+------------------------------------------------------------------+
//| Generate unique indicator name                                   |
//+------------------------------------------------------------------+
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   Print("Generated Indicator Name: ", name);
   return name;
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                          |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- Set unique indicator name
   IndicatorName = GenerateIndicatorName(IndicatorName);
   ButtonName = IndicatorName + "_ToggleEMAsButton";
   IndicatorShortName(IndicatorName);
   
   //--- Load visibility state from global variable
   double val;
   if (GlobalVariableGet(IndicatorName + "_Visibility", val))
      ShowMAs = val != 0;
   
   //--- Indicator buffers mapping
   if (!SetIndexBuffer(0, MA1Buffer) || !SetIndexBuffer(1, MA2Buffer) ||
       !SetIndexBuffer(2, MA3Buffer) || !SetIndexBuffer(3, MA4Buffer) ||
       !SetIndexBuffer(4, MA5Buffer))
   {
      Print("Failed to set index buffers");
      return(INIT_FAILED);
   }
   
   //--- Set indicator styles
   SetIndexStyle(0, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_DOT, MAWidth1, MAColor1);
   SetIndexStyle(1, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth2, MAColor2);
   SetIndexStyle(2, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth3, MAColor3);
   SetIndexStyle(3, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth4, MAColor4);
   SetIndexStyle(4, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth5, MAColor5);
   
   //--- Set indicator labels
   SetIndexLabel(0, "MA1("+string(MAPeriod1)+")");
   SetIndexLabel(1, "MA2("+string(MAPeriod2)+")");
   SetIndexLabel(2, "MA3("+string(MAPeriod3)+")");
   SetIndexLabel(3, "MA4("+string(MAPeriod4)+")");
   SetIndexLabel(4, "MA5("+string(MAPeriod5)+")");
   
   //--- Set individual shifts
   SetIndexShift(0, MAShift1);
   SetIndexShift(1, MAShift2);
   SetIndexShift(2, MAShift3);
   SetIndexShift(3, MAShift4);
   SetIndexShift(4, MAShift5);
   
   //--- Delete existing button to avoid conflicts
   ObjectDelete(0, ButtonName);
   
   //--- Create toggle button
   if(!ObjectCreate(0, ButtonName, OBJ_BUTTON, 0, 0, 0))
   {
      Print("Failed to create button: ", GetLastError());
      return(INIT_FAILED);
   }
   
   ObjectSetInteger(0, ButtonName, OBJPROP_XDISTANCE, ButtonX);
   ObjectSetInteger(0, ButtonName, OBJPROP_YDISTANCE, ButtonY);
   ObjectSetInteger(0, ButtonName, OBJPROP_XSIZE, 75); // Width to fit label
   ObjectSetInteger(0, ButtonName, OBJPROP_YSIZE, 20); // Smaller height
   ObjectSetString(0, ButtonName, OBJPROP_TEXT, ShowMAs ? "Hide EMAs" : "Show EMAs");
   ObjectSetInteger(0, ButtonName, OBJPROP_COLOR, clrBlue); // Blue font
   ObjectSetInteger(0, ButtonName, OBJPROP_BGCOLOR, clrYellow); // Yellow background
   ObjectSetInteger(0, ButtonName, OBJPROP_BORDER_COLOR, clrBlack);
   ObjectSetInteger(0, ButtonName, OBJPROP_FONTSIZE, 10);
   ObjectSetString(0, ButtonName, OBJPROP_FONT, "Arial Bold"); // Bold font
   ObjectSetInteger(0, ButtonName, OBJPROP_SELECTABLE, 0);
   
   //--- Initialize button state
   HandleButtonClicks();
   
   Print("OnInit: ShowMAs=", ShowMAs, ", Button Text=", ShowMAs ? "Hide EMAs" : "Show EMAs", ", Indicator Name=", IndicatorName);
   ChartRedraw(0);
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                        |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   ObjectDelete(0, ButtonName);
   GlobalVariableDel(IndicatorName + "_Visibility");
   Print("OnDeinit: Reason=", reason, ", Indicator Name=", IndicatorName);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                               |
//+------------------------------------------------------------------+
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 limit = prev_calculated == 0
      ? rates_total - MathMax(MAPeriod1, MathMax(MAPeriod2, MathMax(MAPeriod3, MathMax(MAPeriod4, MAPeriod5)))) - 1
      : rates_total - prev_calculated + 1;
   
   limit = MathMin(limit, rates_total);
   
   for(int i = 0; i < limit; i++)
   {
      MA1Buffer[i] = iMA(NULL, 0, MAPeriod1, 0, MAMethod1, MAPrice1, i);
      MA2Buffer[i] = iMA(NULL, 0, MAPeriod2, 0, MAMethod2, MAPrice2, i);
      MA3Buffer[i] = iMA(NULL, 0, MAPeriod3, 0, MAMethod3, MAPrice3, i);
      MA4Buffer[i] = iMA(NULL, 0, MAPeriod4, 0, MAMethod4, MAPrice4, i);
      MA5Buffer[i] = iMA(NULL, 0, MAPeriod5, 0, MAMethod5, MAPrice5, i);
   }
   
   return(rates_total);
}

//+------------------------------------------------------------------+
//| Handle button clicks                                             |
//+------------------------------------------------------------------+
void HandleButtonClicks()
{
   if(ObjectGetInteger(ChartID(), ButtonName, OBJPROP_STATE))
   {
      ShowMAs = !ShowMAs;
      ObjectSetInteger(ChartID(), ButtonName, OBJPROP_STATE, false);
      ObjectSetString(0, ButtonName, OBJPROP_TEXT, ShowMAs ? "Hide EMAs" : "Show EMAs");
      
      GlobalVariableSet(IndicatorName + "_Visibility", ShowMAs ? 1.0 : 0.0);
      
      SetIndexStyle(0, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_DOT, MAWidth1, MAColor1);
      SetIndexStyle(1, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth2, MAColor2);
      SetIndexStyle(2, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth3, MAColor3);
      SetIndexStyle(3, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth4, MAColor4);
      SetIndexStyle(4, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth5, MAColor5);
      
      Print("Toggle EMAs: ", ShowMAs ? "Show" : "Hide", ", Button Text=", ShowMAs ? "Hide EMAs" : "Show EMAs");
      ChartRedraw(0);
   }
   else
   {
      // Ensure styles match ShowMAs state
      SetIndexStyle(0, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_DOT, MAWidth1, MAColor1);
      SetIndexStyle(1, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth2, MAColor2);
      SetIndexStyle(2, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth3, MAColor3);
      SetIndexStyle(3, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth4, MAColor4);
      SetIndexStyle(4, ShowMAs ? DRAW_LINE : DRAW_NONE, STYLE_SOLID, MAWidth5, MAColor5);
      ObjectSetString(0, ButtonName, OBJPROP_TEXT, ShowMAs ? "Hide EMAs" : "Show EMAs");
      ChartRedraw(0);
   }
}

//+------------------------------------------------------------------+
//| Chart event handler                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   HandleButtonClicks();
}

//+------------------------------------------------------------------+