Add Toggle On/Off button to a GapFinder indicator

 

Hi Everybody,

I've found and modified a gap finder .mq4 indicator for MT4 and I'd love to add a simple toggle On/Off button on chart.
Could someone help me?
I'm new to coding and I'm struggling figuring out how to properly set it.

CODE:

#property indicator_chart_window
#property indicator_buffers 1
#property description "Automatically render boxes for liquidity gaps "
#property indicator_color1 Black

extern double Gap_Size_Minimum = 100;
extern int ExtendBars = 1000;
extern int    History = 1000;
extern color Gap_Up = Aqua;
extern color Gap_Down = Tomato;
input ENUM_LINE_STYLE Rectangle_Style = STYLE_SOLID;

double Pip;
int init()
  {
    Pip = Point;
   if(Digits==3 || Digits==5) Pip = 10*Point;
   return(0);
  }

int deinit()
  {
    string ObjName;
    for(int i = ObjectsTotal()-1; i>=0; i--)
    {
      ObjName = ObjectName(i);
      if(StringFind(ObjName,"liquidity_gaps",0)>=0)
        ObjectDelete(ObjName);
    }
    return(0);
  }

int start()
  {
   int i, limit, counted_bars=IndicatorCounted();
   limit = MathMin(History,Bars-counted_bars-1);
   string ObjName;
   for(i=limit; i>=0; i--)
   {
     if(i>Bars-2) continue;
     if(MathAbs(Open[i]-Close[i+1]) > Gap_Size_Minimum*Pip)
     {
       ObjName = "liquidity_gaps_Up_"+Time[i];
       color ObjColor;
       if(Open[i] > Close[i+1]) ObjColor = Gap_Up;
       else ObjColor = Gap_Down;
       if(ObjectFind(ObjName)<0)
       {  
         ObjectCreate(ObjName,OBJ_RECTANGLE,0,Time[i+1],Close[i+1],Time[i],Open[i]);
         ObjectSet(ObjName,OBJPROP_BACK,0);         
         ObjectSet(ObjName,OBJPROP_COLOR,ObjColor);                 
         ObjectSet(ObjName,OBJPROP_STYLE,Rectangle_Style);
       }
     }
     
     //datetime thistime = Time[i];     
     int ib = i+ExtendBars;
     while(ib>=0)
     
     {
       ObjName = "liquidity_gaps_Up_"+Time[ib];
       if(ObjectFind(ObjName)>=0)
       {
         double pr2 = ObjectGet(ObjName,OBJPROP_PRICE2);
         double pr1 = ObjectGet(ObjName,OBJPROP_PRICE2);
         ObjectSet(ObjName,OBJPROP_TIME2,Time[i]);
         ObjectSet(ObjName,OBJPROP_PRICE2,pr2);
       }
       ib--;
     }
   }
   return(0);
  }

Thanks a lot in advance for your help

Files:
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.

 
Nicolo Maragno: Could someone help me?

Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
          No free help (2017)

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum (2018)

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
          No free help (2017)

 
William Roeder #:

Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
          No free help (2017)

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum (2018)

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
          No free help (2017)

Hi William, thanks for your reply.

Here is my latest attempt to create the button with the help of a template I found for on/off button.

#property indicator_chart_window
#property indicator_buffers 1
#property description "Automatically render boxes for liquidity gaps "
#property indicator_color1 Black

extern double Gap_Size_Minimum = 100;
extern int ExtendBars = 1000;
extern int    History = 1000;
extern color Gap_Up = Aqua;
extern color Gap_Down = Tomato;
input ENUM_LINE_STYLE Rectangle_Style = STYLE_SOLID;
input string          gapsID             = "Time1";// Lines ID

extern string             button_note1          = "------------------------------";
extern int                btn_Subwindow = 0;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "Gaps";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 10;                            
extern color              btn_text_ON_color     = clrLime;
extern color              btn_text_OFF_color    = clrRed;
extern string             btn_pressed           = "Gaps_ON";            
extern string             btn_unpressed         = "Gaps_OFF";
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 850;                                 
extern int                button_y              = 0;                                   
extern int                btn_Width             = 120;                                 
extern int                btn_Height            = 20;                                
extern string             soundBT               = "tick.wav";  
extern string             button_note2          = "------------------------------";

bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}


int init()
  {
//---- indicators
  IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(WindowExpertName());
   IndicatorDigits(Digits);
      double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
   show_data = val != 0;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix+btn_text;
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (ChartID(),buttonID);
      ObjectCreate (ChartID(),buttonID,OBJ_BUTTON,btn_Subwindow,0,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font);
      ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999);
}

int deinit()
  {
//----

ObjectsDeleteAll(0,"Gaps");
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
   
   
   int i;
  
  
  
   for (i=0;i<Bars;i++)
    {
    ObjectsDeleteAll(0,gapsID+":");
    }   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

 bool recalc = true;
 void handleButtonClicks()
{
   if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
      start();
   }
}
void OnChartEvent(const int id, 
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
   if (id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   {
   if (soundBT!="") PlaySound(soundBT);     
   }
}

int start()
{
   handleButtonClicks();
   recalc = false;
   
   start2();
     int    counted_bars=IndicatorCounted(),
//----
   limit,
   i=0; 
   if(counted_bars>0)
      counted_bars--;
   
   limit=Bars-counted_bars;
   
   
   
   if (show_data)
      {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed);
      }
      else
      {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed);
      
  
  
  
   for (i=0;i<Bars;i++)
    {
   ObjectsDeleteAll(0,gapsID+":");
    }   
   
      //template code     
      }
       return(0);
      }



double Pip;

int init1()
  {
    Pip = Point;
   if(Digits==3 || Digits==5) Pip = 10*Point;
   return(0);
  }

int deinit1()
  {
    string ObjName;
    for(int i = ObjectsTotal()-1; i>=0; i--)
    {
      ObjName = ObjectName(i);
      if(StringFind(ObjName,"liquidity_gaps",0)>=0)
        ObjectDelete(ObjName);
    }
    return(0);
  }

int start2()
  {
   int i, limit, counted_bars=IndicatorCounted();
   limit = MathMin(History,Bars-counted_bars-1);
   string ObjName;
   for(i=limit; i>=0; i--)
   {
     if(i>Bars-2) continue;
     if(MathAbs(Open[i]-Close[i+1]) > Gap_Size_Minimum*Pip)
     {
       ObjName = "liquidity_gaps_Up_"+Time[i];
       color ObjColor;
       if(Open[i] > Close[i+1]) ObjColor = Gap_Up;
       else ObjColor = Gap_Down;
       if(ObjectFind(ObjName)<0)
       {  
         ObjectCreate(ObjName,OBJ_RECTANGLE,0,Time[i+1],Close[i+1],Time[i],Open[i]);
         ObjectSet(ObjName,OBJPROP_BACK,0);         
         ObjectSet(ObjName,OBJPROP_COLOR,ObjColor);                 
         ObjectSet(ObjName,OBJPROP_STYLE,Rectangle_Style);
       }
     }
     
     //datetime thistime = Time[i];     
     int ib = i+ExtendBars;
     while(ib>=0)
     
     {
       ObjName = "liquidity_gaps_Up_"+Time[ib];
       if(ObjectFind(ObjName)>=0)
       {
         double pr2 = ObjectGet(ObjName,OBJPROP_PRICE2);
         double pr1 = ObjectGet(ObjName,OBJPROP_PRICE2);
         ObjectSet(ObjName,OBJPROP_TIME2,Time[i]);
         ObjectSet(ObjName,OBJPROP_PRICE2,pr2);
       }
       ib--;
     }
   }
   return(0);
  } 

The button shows up on chart and also the indicator but the trigger is not working at all plus I can't modified the parameters of the indicators from the Indicator inputs anymore.

I'm a noobie but I'm trying to understand how to readapt pieces of code from other indicators to this code.

Even this part of the code I don't need it but I don't really now how to properly remove it without affecting other parts of the code.

{
   handleButtonClicks();
   if (id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   {
   if (soundBT!="") PlaySound(soundBT);     
   }

I understand the "interafce design" part of the button, I understand what it is set for but I can't connect the template to the functions of the code, they are still separated plus there are coding issues.

I'm hoping to understand if I'm far away from the solution or if I'm close to it and what it needs to be done in order to fix it.

Thanks again for your help