// https://forex-station.com/viewtopic.php?p=1295478806#p1295478806 //button code1
// https://forex-station.com/viewtopic.php?p=1295478883#p1295478883 //button code2
// not for sale, rent, auction, nor lease

#include <stderror.mqh>
#include <stdlib.mqh>
#property strict

#property copyright "Copyright © 2019, Frosty"

#define VERSION "1.001";
#define INDICATOR_TEXT "GMMA"

#property version VERSION

#property indicator_chart_window

#property indicator_buffers 6
#property indicator_color1 clrBlue
#property indicator_color2 clrBlue
#property indicator_color3 clrBlue
#property indicator_color4 clrBlue
#property indicator_color5 clrBlue
#property indicator_color6 clrBlue

#property indicator_width1 1  
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 1

input string ShortTermNote = "=== Short Term EMA Settings ==";
input int ShortEMAPeriod1 = 3;
input int ShortEMAPeriod2 = 5;
input int ShortEMAPeriod3 = 8;
input int ShortEMAPeriod4 = 10;
input int ShortEMAPeriod5 = 12;
input int ShortEMAPeriod6 = 15;

//Forex-Station button template start41; copy and paste
extern string             button_note1_         = "------------------------------";
extern int                btn_Subwindow         = 0;                               // What window to put the button on.  If <0, the button will use the same sub-window as the indicator.
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER;               // button corner on chart for anchoring
extern string             btn_text              = "GMMA Short";                    // a button name
extern string             btn_Font              = "Arial";                         // button font name
extern int                btn_FontSize          = 9;                               // button font size               
extern color              btn_text_ON_color     = clrLime;                         // ON color when the button is turned on
extern color              btn_text_OFF_color    = clrRed;                          // OFF color when the button is turned off
extern color              btn_background_color  = clrDimGray;                      // background color of the button
extern color              btn_border_color      = clrBlack;                        // border color the button
extern int                button_x              = 20;                              // x coordinate of the button     
extern int                button_y              = 65;                              // y coordinate of the button     
extern int                btn_Width             = 80;                              // button width
extern int                btn_Height            = 20;                              // button height
extern string             UniqueButtonID        = "GMMAguppyShort";                // Unique ID for each button        
extern string             button_note2          = "------------------------------";

bool show_data, recalc=false;
string IndicatorObjPrefix, buttonId;
//Forex-Station button template end41; copy and paste

const string INDICATOR_NAME = INDICATOR_TEXT + " v" + VERSION;

//---- buffers
double ExtMapBuffer1[], ExtMapBuffer2[], ExtMapBuffer3[], ExtMapBuffer4[], ExtMapBuffer5[], ExtMapBuffer6[];
//+------------------------------------------------------------------------------------------------------------------+
//Forex-Station button template start42; copy and paste
int OnInit()
{
   IndicatorDigits(Digits);
   IndicatorObjPrefix = "__" + btn_text + "__";
      
   buttonId = "_" + IndicatorObjPrefix + UniqueButtonID + "_BT_";
   if (ObjectFind(buttonId)<0) 
      createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE, button_x);

   init2();

   show_data = ObjectGetInteger(0, buttonId, OBJPROP_STATE);
   
   if (show_data) ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_ON_color); 
   else ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_OFF_color);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------------------------------------------------------+
void createButton(string buttonID,string buttonText,int width2,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (0,buttonID);
      ObjectCreate    (0,buttonID,OBJ_BUTTON,btn_Subwindow,0,0);
      ObjectSetInteger(0,buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_TYPE,BORDER_RAISED);
      ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,width2);
      ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (0,buttonID,OBJPROP_FONT,font);
      ObjectSetString (0,buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(0,buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,9999);
      // Upon creation, set the initial state to "true" which is "on", so one will see the indicator by default
      ObjectSetInteger(0, buttonId, OBJPROP_STATE, true);
}
//+------------------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason) 
{
   // If just changing a TF', the button need not be deleted, therefore the 'OBJPROP_STATE' is also preserved.
   if(reason != REASON_CHARTCHANGE) ObjectDelete(buttonId);
}
//+------------------------------------------------------------------------------------------------------------------+
void OnChartEvent(const int id, //don't change anything here
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   // If another indy on the same chart has enabled events for create/delete/mouse-move, just skip this events up front because they aren't
   //    needed, AND in the worst case, this indy might cause MT4 to hang!!  Skipping the events seems to help, along with other (major) changes to the code below.
   if(id==CHARTEVENT_OBJECT_CREATE || id==CHARTEVENT_OBJECT_DELETE) return; // This appears to make this indy compatible with other programs that enabled CHART_EVENT_OBJECT_CREATE and/or CHART_EVENT_OBJECT_DELETE
   if(id==CHARTEVENT_MOUSE_MOVE    || id==CHARTEVENT_MOUSE_WHEEL)   return; // If this, or another program, enabled mouse-events, these are not needed below, so skip it unless actually needed. 

   if (id==CHARTEVENT_OBJECT_CLICK && sparam == buttonId)
   {
      show_data = ObjectGetInteger(0, buttonId, OBJPROP_STATE);
      
      if (show_data)
      {
         ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_ON_color); 
         init2();
         // Is it a problem to call 'start()' ??  Possibly it makes no difference, but now calling "mystart()" instead of "start()"; and "start()" simply runs "mystart()", so should be same as before.
         recalc=true;
         mystart();
      }
      else
      {
         ObjectSetInteger(0,buttonId,OBJPROP_COLOR,btn_text_OFF_color);
         for (int ForexStation=0; ForexStation<indicator_buffers; ForexStation++)
              SetIndexStyle(ForexStation,DRAW_NONE);
      }
   }
}
//Forex-Station button template end42; copy and paste
//+------------------------------------------------------------------------------------------------------------------+
int init2()
{  
   SetIndexStyle(0,DRAW_LINE,EMPTY);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE,EMPTY);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE,EMPTY);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE,EMPTY);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexStyle(4,DRAW_LINE,EMPTY);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexStyle(5,DRAW_LINE,EMPTY);
   SetIndexBuffer(5,ExtMapBuffer6);
   
   SetIndexLabel (0, "GMMA ("+(string)ShortEMAPeriod1+")");
   SetIndexLabel (1, "GMMA ("+(string)ShortEMAPeriod2+")");
   SetIndexLabel (2, "GMMA ("+(string)ShortEMAPeriod3+")");
   SetIndexLabel (3, "GMMA ("+(string)ShortEMAPeriod4+")");
   SetIndexLabel (4, "GMMA ("+(string)ShortEMAPeriod5+")");
   SetIndexLabel (5, "GMMA ("+(string)ShortEMAPeriod6+")");
   return 0;
}
//+------------------------------------------------------------------------------------------------------------------+
int start() {return(mystart()); }
//+------------------------------------------------------------------------------------------------------------------+
int mystart()
  {
   if (show_data)
      {
   if (Bars <= 3) 
      return(0);
   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) 
      return(-1);

        if(recalc) 
        {
           // If a button goes from off-to-on, everything must be recalculated.  The 'recalc' variable is used as a trigger to do this.
           counted_bars = 0;
           recalc=false;
        }

   int limit = Bars - 2;
   if (counted_bars > 2 && !recalc) 
      limit = Bars - counted_bars - 1;

   recalc = false;

   for(int i = 0; i< limit; i++)
   {
         //short periods
         ExtMapBuffer1[i]=iMA(NULL,0,ShortEMAPeriod1,0,MODE_EMA,PRICE_CLOSE,i);
         ExtMapBuffer2[i]=iMA(NULL,0,ShortEMAPeriod2,0,MODE_EMA,PRICE_CLOSE,i);
         ExtMapBuffer3[i]=iMA(NULL,0,ShortEMAPeriod3,0,MODE_EMA,PRICE_CLOSE,i);
         ExtMapBuffer4[i]=iMA(NULL,0,ShortEMAPeriod4,0,MODE_EMA,PRICE_CLOSE,i);
         ExtMapBuffer5[i]=iMA(NULL,0,ShortEMAPeriod5,0,MODE_EMA,PRICE_CLOSE,i);
         ExtMapBuffer6[i]=iMA(NULL,0,ShortEMAPeriod6,0,MODE_EMA,PRICE_CLOSE,i);
   } //for(i = 0; i< limit; i++)
      } //if (show_data)  
   return 0;
}
//+------------------------------------------------------------------+

