Trying to create a Indicators on screen Show/hide buttons. Please help

 

I tryin to easy show/hide indicators on the chart window, better that switch templates, I think. I did the interface and the buttons for each one, this example is with easy moving averages, but I think can be used for Ichimoku, BBands, etc, please check on the screen shot.

I have the buttons done, but my problem is the linking Boolean variables to define the show/hide, as well as the logic to execute it. Please check on my code, I tied with some variables but cant get linked batwing OnCalculate and OnChartEvent


Thank you

//---Include for Buttom functions
#include <Controls\Dialog.mqh>
CAppDialog OurInterface;
#include <Controls\Button.mqh>
CButton MA14Button, MA28Button;

//--- indicator buffers
double         Label1Buffer[];
double         Label2Buffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 
      ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); //Detect Mouse
      OurInterface.Create(0,"On Chart Indicators",0,5,10,170,290); //Zize of the interface Window
 //Create the buttons
 
      MA14Button.Create(0,"MA14Button",0,5,10,150,40);//Create MA14Button
      MA14Button.Text("MA @ 14");                         //Label
      OurInterface.Add(MA14Button);                     //Add it inside of the interface
      
       MA28Button.Create(0,"MA28Button",0,5,50,150,80);//Create MA28Button
      MA28Button.Text("MA @ 28");
      OurInterface.Add(MA28Button);
      
      
      OurInterface.Run();  // Make it closable
      
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer);
   SetIndexBuffer(1,Label2Buffer);
     
   
//---
   return(INIT_SUCCEEDED);
  }
  
  //+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
      OurInterface.Destroy(reason); 
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---set the boolean variables for visible onchart indicators
   bool MA14_Ind=false;
   bool MA28_Ind=false;
   
 //Calculations
  
 int limit = rates_total-prev_calculated;
   if (prev_calculated>0) limit++;
   
   for (int i=limit-1; i>=0; i--)
   {
   //Calculates each MA
     double MA14=iMA(NULL,0,14,0,0,0,i);
     double MA28=iMA(NULL,0,14,0,0,0,i);
   
   //Plot the curves if selected shown by flags
   if (MA14_Ind=false)
        { return(0);}
         else
            {Label1Buffer[i]=MA14;}
         
   if (MA14_Ind=false)
        { return(0);}
         else
            {Label1Buffer[i]=MA28;}

   }
   
//--- 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)
  {
//---Observes cursor position, Highlight button and detect Click event

   OurInterface.OnEvent(id,lparam,dparam,sparam);
   
   if(MA14Button.Contains(lparam,dparam))
      MA14Button.Pressed(true);   //Dtect cursor on the button
   else
      MA14Button.Pressed(false);   
      
   if(MA28Button.Contains(lparam,dparam))
      MA28Button.Pressed(true);  //Dtect cursor on the button
   else
      MA28Button.Pressed(false);   

  //Tried this way to manage indicators flags for hide/show but didnt work
           
/*   if (id==CHARTEVENT_OBJECT_CLICK&&sparam=="MA14Button")
            if (MA14_Ind=false) MA14_Ind=true;
            else MA14_Ind=false;
         
   if (id==CHARTEVENT_OBJECT_CLICK&&sparam=="MA28Button")
            if (MA28_Ind=false) MA28_Ind=true;
            else MA28_Ind=false;*/
            
   
  }
Moving Average - Trend Indicators - Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis
Moving Average - Trend Indicators - Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
Files:
Graph.jpg  28 kb
 

Try as follows;

for (int i = limit - 1; i >= 0; i--)
{
   //Calculates each MA
   Label1Buffer[i] = iMA(NULL, 0, 14, 0, 0, 0, i);
   Label2Buffer[i] = iMA(NULL, 0, 28, 0, 0, 0, i);
}
if (id == CHARTEVENT_OBJECT_CLICK && sparam == "MA14Button")
{
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, clrWhite);
   SetIndexStyle(1, DRAW_NONE, STYLE_SOLID, 1, clrNONE);
}

if (id == CHARTEVENT_OBJECT_CLICK && sparam == "MA28Button")
{
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1, clrRed);
   SetIndexStyle(0, DRAW_NONE, STYLE_SOLID, 1, clrNONE);
}
 
Nagisa Unada #:

Try as follows;

You are awesome!!!

 I wanted to show both of them if the case, not only switch one to other, I took your advise and got this. works awesome 


Thank you!!

 if (id == CHARTEVENT_OBJECT_CLICK && sparam == "MA14Button")
            { switch (MA14_Ind)
            {   case (true):  SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, clrRed); MA14_Ind=false; break;         
                case (false): SetIndexStyle(0, DRAW_NONE, STYLE_SOLID, 1, clrNONE); MA14_Ind=true; break;
            } 
            }

   if (id == CHARTEVENT_OBJECT_CLICK && sparam == "MA28Button")
             { switch (MA28_Ind)
            {   case (true):  SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1, clrWhite); MA28_Ind=false; break;         
                case (false): SetIndexStyle(1, DRAW_NONE, STYLE_SOLID, 1, clrNONE); MA28_Ind=true; break;
            } 
            }