true/false bool BUTTON on chart

 

Hello there,
I have an indicator that plot some right price labels by :

extern bool My_Labels=true; //Hide or show labels

And, i ask you if is it possible to control the "true/false" state by creating (in the same indicator) an external button on the chart ?

Is it really possible ?

Regards.

EDIT : in the same kind of ideas => this EA https://docs.mql4.com/constants/chartconstants/charts_samples

Examples of Working with the Chart - Chart Constants - Standard Constants, Enumerations and Structures - MQL4 Reference
Examples of Working with the Chart - Chart Constants - Standard Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
This section contains examples of working with chart properties. One or two complete functions are displayed for each property. These functions allow setting/receiving the value of the property. These functions can be used "as is" in custom mql4 applications. The screenshot below demonstrates the graphic panel illustrating how changing of the...
 
Thierry Ramaniraka:

Hello there,
I have an indicator that plot some right price labels by :

And, i ask you if is it possible to control the "true/false" state by creating (in the same indicator) an external button on the chart ?

Yes it is possible.

Create a button and then check its state in OnChartEvent().

Or you could simply press a key and check  OnChartEvent() for CHARTEVENT_KEYDOWN

 
Keith Watford:

Yes it is possible.

Create a button and then check its state in OnChartEvent().

Or you could simply press a key and check  OnChartEvent() for CHARTEVENT_KEYDOWN

Ok, 
I will take some inspiration on it.

Thank you vrey much.

 

Hello,
I found this indicator here : https://automatedtradingsoftware.co.uk/how-to-create-a-simple-toggle-button-in-mql4/

I tried and tried to have the "true" state on the chart



as it is by default in the indicator pop-up.


Finally, i come to you with the original clean code.
What do i have to modified to have it on "true" by default ?

//+------------------------------------------------------------------+
//|                                              #Toggle_Example.mq4 |
//|                       Copyright 2015, Automated Trading Software |
//|                            http://automatedtradingsoftware.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Automated Trading Software"
#property link      "http://automatedtradingsoftware.co.uk"
#property version   "1.00"
#property strict
#property indicator_chart_window

extern bool value_to_toggle = true; // The Value to Toggle

// unique identifier that we can access from multiple places throughout the code
string sButtonName = "toggle_button";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   // force the button to be drawn when the indicator is first placed on the chart
   Toggle();
   
//---
   return(INIT_SUCCEEDED);
  }
  
// this is not added automatically so you'll need to add it yourself
void OnDeinit(const int reason){
   // clean up our button when the indicator is removed from the chart
   ObjectDelete(sButtonName);
   // clean off commment too
   Comment("");
}
  
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   // if the sparam passed to the function is the unique id of the button then toggle it
   if(sparam==sButtonName) Toggle();
   
  }
//+------------------------------------------------------------------+

void Toggle(){
// function to handle toggle
   color cColor  = clrLimeGreen;
   uchar ucArrow = 233;
   // use a string type if you need text on your button
   
   if(value_to_toggle == true){
      cColor = clrRed;
      ucArrow = 234;
   }
   
   CreateButton(sButtonName,cColor,"Wingdings",CharToStr(ucArrow));
   value_to_toggle = !value_to_toggle;
   
   Comment("The value of value_to_toggle is ", value_to_toggle);
       
}

void CreateButton(string sName, color cColor, string sFont = "Wingdings", string sText = ""){
// create a button on the chart

// many of these settings are hard coded below but you can easily have them as paramaters and pass them to the function
// just like I've done already with the color, font and text

   if(ObjectFind(sName)< 0){
      ObjectCreate(0,sName,OBJ_BUTTON,0,0,0);
   }
   
   // these could be external valiables to allow the user to create the button wherever they wanted
   ObjectSetInteger(0,sName,OBJPROP_XDISTANCE,75);
   ObjectSetInteger(0,sName,OBJPROP_YDISTANCE,25);
   ObjectSetInteger(0,sName,OBJPROP_XSIZE,50);
   ObjectSetInteger(0,sName,OBJPROP_YSIZE,50);
   ObjectSetInteger(0,sName,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   
   ObjectSetString(0,sName,OBJPROP_TEXT,sText);
   ObjectSetInteger(0,sName,OBJPROP_COLOR, cColor);
   // I'm setting the background and border to the same as the chart background
   // as I'm just using a wingding arrow
   long result;

   ChartGetInteger(0,CHART_COLOR_BACKGROUND,0,result);

   color cBack = (color) result;

   ObjectSetInteger(0,sName,OBJPROP_BGCOLOR, cBack);

   ObjectSetInteger(0,sName,OBJPROP_BORDER_COLOR,cBack);
   // make sure our object shows up in the 'Objects List'
   ObjectSetInteger(0,sName,OBJPROP_HIDDEN, false);
   // commmented out some settings as they are not used
   // I would have normally deleted them but left them for completeness
   //ObjectSetInteger(0,sName,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   //ObjectSetInteger(0,sName,OBJPROP_STATE,false);
   ObjectSetString(0,sName,OBJPROP_FONT,sFont);
   ObjectSetInteger(0,sName,OBJPROP_FONTSIZE,28);
   
}

Thank you.
Kind regards.

How to Create a Simple Toggle Button in MQL4
How to Create a Simple Toggle Button in MQL4
  • 2015.08.30
  • automatedtradingsoftware.co.uk
Recently I started using an indicator that had been written by another programmer and initially it was doing a pretty good job. However,eventually I started getting annoyed with one particular implementation... ...or rather lack of implementation. The indicator itself was a pretty simple affair and in essence all it did was to draw a series …
 
 int OnInit ()
  {
 //--- indicator buffers mapping 

   // force the button to be drawn when the indicator is first placed on the chart 
   value_to_toggle = !value_to_toggle;     //add this line 
   Toggle();
   
 //--- 
   return ( INIT_SUCCEEDED );
  }

This sounds good. I will also use it.

 
Naguisa Unada:

This sounds good. I will also use it.

THANK YOU!
It's exactly the result that i want.

-------

Yes, it's a good "base mesh" to work with.
Very usefull to hide and show some "OBJ" .

Regards.

 
nicholishen:

Hi Thierry,

You should always get the objects state from the object itself instead of making a global variable in an attempt to track its state. You can do so by using ObjectGetInteger with OBPPROP_STATE.

In your case I would recommend a different tack and keep your code a lot cleaner by using the std lib classes and deriving your own button class in order to handle all functionality in-house. Here is an example. 

Hello nicholishen ,
Thank you for your great post.
I understand what you mean, in the big picture. More logical and surely more cleaner.

For now, as i am more on the beginner level than an "ultimate skilled" level... i will make it the first way to do it, and when i will gain more skills, surely i will re-code it more on your way.
I keep it in my favourite.

Thank you very much for your apreciated post.

Regards.

 

In the original article
https://automatedtradingsoftware.co.uk/how-to-create-a-simple-toggle-button-in-mql4/#comment-1011

the autor answers an idea to store the state of the bool.
Because now, the state reinitalize itself when the timeframe is changed.

But it's too light for me to understand what he means.

nicholishen , Is it what you told me to anticipate this problem ?

Can someone help me to do it please ?

How to Create a Simple Toggle Button in MQL4
How to Create a Simple Toggle Button in MQL4
  • 2015.08.30
  • automatedtradingsoftware.co.uk
Recently I started using an indicator that had been written by another programmer and initially it was doing a pretty good job. However,eventually I started getting annoyed with one particular implementation... ...or rather lack of implementation. The indicator itself was a pretty simple affair and in essence all it did was to draw a series …
 
nicholishen:

Don't do that. The object itself already stores the state across chart changes. 

Very very nice code/idea.
The state button stays always the same ... even if we change the timeframe.
Thank you.

Nb : the "start" function gave me error, So I deleted it. Is it ok ?

.: Now i am here after some slight edits on your code :.





Originally, my vision was to change the "Value to toggle" according the "state button value".
My Goal is to "Hide/Show" some important Right Price Label and other Obj.

What do you suggest :
 - To keep my 1st vision
or
 - To directly work, with the "button" and eliminate "The Value to Toggle" in the indicator's pop up window (and in the source code) ?

Nb2 : Your help is very appreciated. I sincerly thank you. God bless.

 

I have just installed ON-OFF switch on my ranking panel. It's pretty good. Thanks.

Hide RankingShow Ranking

 
Naguisa Unada:

I have just installed ON-OFF switch on my ranking panel. It's pretty good. Thanks.


Thanks to you for your help. :)
I am working on my indicator with a new version including buttons.

To be continued...

Reason: