Help with my panel

 

Hello,

Started to learn mql5 coding about 1 month ago, and my goal is to create my own simple ea for trading.

For that I choose to use panels and dialogs :displaying chart's infos and buttons for adding indicators and trade assist.

The idea is to have one main panel with buttons opening 3 sub panels for informations and indicators.

I used the models from the Documentation\Standard Library section and so far I met some issues:

-when chage the timeframe my ea(panel) disappears from the chart; if I put only the main panel it's works..so I believe that the subpanels cause this...

-when hit the xbutton of the subpanel it's removes the ea from the chart and for avoiding this I created an extra button for closing the subpanel,

-one of subpanels has a check box, when checked is calling the ChartIndicatorAdd function for add a customised moving average; if I uncheck the checkbox the indicator is not been removed

How can I solve those issues?

Here is the code:

//+------------------------------------------------------------------+
//|                                                   panel_exp1.mq5 |
//|                                                                  |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "zlory"
#property link      "https://www.mql5.com"
#property version   "1.00"


#include <Controls\Dialog.mqh>
#include <Controls\Panel.mqh> 
#include <Controls\CheckBox.mqh>
#include <Controls\Label.mqh>
#include <Controls\Button.mqh>
#include <Controls\Edit.mqh>

//+------------------------------------------------------------------+ 
//| defines                                                          | 
//+------------------------------------------------------------------+ 
//--- indents and gaps 
#define INDENT_LEFT                         (11)      // indent from left (with allowance for border width) 
#define INDENT_TOP                          (11)      // indent from top (with allowance for border width) 
#define INDENT_RIGHT                        (11)      // indent from right (with allowance for border width) 
#define INDENT_BOTTOM                       (11)      // indent from bottom (with allowance for border width) 
#define CONTROLS_GAP_X                      (5)       // gap by X coordinate 
#define CONTROLS_GAP_Y                      (5)       // gap by Y coordinate 
//--- for buttons 
#define BUTTON_WIDTH                        (100)     // size by X coordinate 
#define BUTTON_HEIGHT                       (20)      // size by Y coordinate 
//--- for the indication area 
#define EDIT_WIDTH                          (30)      // size along the X-axis
#define EDIT_HEIGHT                         (16)      // size by Y coordinate 
//--- for group controls 
#define GROUP_WIDTH                         (150)     // size by X coordinate 
#define LIST_HEIGHT                         (179)     // size by Y coordinate 
#define RADIO_HEIGHT                        (56)      // size by Y coordinate 
#define CHECK_HEIGHT                        (93)      // size by Y coordinate
input int EmaPeriod = 20;
input int EmaShift= 0;
input color EmaColor=clrMagenta;

//+------------------------------------------------------------------+ 
//| Class CControlsDialog                                            | 
//| Usage: main dialog of the Controls application                   | 
//+------------------------------------------------------------------+

 class CMainPanel : public CAppDialog 

 { 
 private:
   CLabel    lab1;
   CLabel    BID;
   CLabel    lab2;
   CLabel    lab3;
   CLabel    lab4;
   CLabel    lab5;
   
   CAppDialog    subPanel1;
   CAppDialog    subPanel2;
   CAppDialog    subPanel3;
   
   CCheckBox  check1;
   
   
   CButton   but1;
   CButton   but2;
   CButton   but3;
   CButton   but4;
   CButton   but5;
   CButton   but6;
   bool              CreateLabel(const long chart,const int subwindow,CLabel &object,const string name,const string text,const uint x1,const uint y1,const uint x2,const uint y2);
   //--- Create Button
   bool              CreateButton(const long chart,const int subwindow,CButton &object,const string name,const string text,const uint x1,const uint y1,const uint x2,const uint y2); 
   bool              CreateSubPanel(const long chart,const int subwindow,CAppDialog &object,const string name,const uint x1,const uint y1,const uint x2,const uint y2);
   bool              CreateCheckBox(const long chart,const int subwindow,CCheckBox &object,CAppDialog &dialog,const string name,const uint x1,const uint y1,const int width, const int height, const int status);
   void      OnClickButton1();
   void      OnClickButton2();
   void      OnClickButton3();
   void      OnClickButton4();
   void      OnClickButton5();
   void      OnClickButton6();
   void      OnChangeCheck1();
   
 public: 
                     CMainPanel(void){}; 
                    ~CMainPanel(void){};


 //--- create 
   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2); 
   //--- chart event handler 
   virtual bool      OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
   virtual void      OnTick(void);



 };
 
 int Ema_handle;
 double EmaBuffer[];
 

 CMainPanel MainPanel;
 CMainPanel subPanel1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
 int OnInit()
  {
//---
   if(!MainPanel.Create(ChartID(),"Main "+_Symbol+"  "+_Period))
     return(INIT_FAILED);
//---
   MainPanel.Run();
   subPanel1.Run();
   
  Ema_handle=iCustom(Symbol(),_Period,"Examples\\Custom Moving Average1",EmaPeriod,EmaShift,MODE_EMA,PRICE_HIGH,EmaColor);
  
  ArraySetAsSeries(EmaBuffer,true);
  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
 void OnDeinit(const int reason)
  {
//---
   MainPanel.Destroy(reason);
   subPanel1.Destroy(reason);
   IndicatorRelease(Ema_handle);
   ChartIndicatorDelete(0,0,ChartIndicatorName(0,0,0));
   ArrayFree(EmaBuffer);
 
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
 void OnTick()
  {
//---
   MainPanel.OnTick();
   subPanel1.OnTick();
   

  }
 //+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  if( MainPanel.OnEvent(id, lparam, dparam, sparam))
    ChartRedraw();
      
  }
  bool CMainPanel::Create(const long chart,const string name,const int subwin=0,const int x1=20,const int y1=20,const int x2=280,const int y2=400)
  {
    if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))
     {
      return false;
     }
    int x11=INDENT_LEFT;
    int y11=INDENT_TOP;
    int x21=x11+EDIT_WIDTH;
    int y21=y11+EDIT_HEIGHT;
    if(!CreateLabel(chart,subwin,BID,"bidlab","BID Price:",x11,y11,x21,y21))
       return(false);
   
    if(!CreateLabel(chart,subwin,lab1,"lab1","bidprice",x21+GROUP_WIDTH,y11,EDIT_WIDTH,y21))
      { 
       return(false);
      }
    if(!CreateButton(chart,subwin,but1,"but1","Button1",x11+EDIT_WIDTH,y21+2*EDIT_HEIGHT,BUTTON_WIDTH/2,BUTTON_HEIGHT))
       return(false);
    if(!CreateButton(chart,subwin,but2,"but2","Button2",x21+2*EDIT_WIDTH,y21+2*EDIT_HEIGHT,BUTTON_WIDTH/2,BUTTON_HEIGHT))
       return(false);
       int x_size=150;
       int y_size=150;
    CreateSubPanel(chart,subwin,subPanel1,"SubPanel1",x1,x2,x_size,y_size);
      {     
       int x197=INDENT_LEFT+30;
       int y197=INDENT_TOP+80;
       int x297=x197+BUTTON_WIDTH/2;
       int y297=y197+BUTTON_HEIGHT;
       if(!but4.Create(m_chart_id,"but4",m_subwin,x197,y197,x297,y297))
          return(false);      
       but4.Text("CLOSE");
       but4.FontSize(8);
       if(!subPanel1.Add(but4))  
           return(false);   
        CreateCheckBox(0,0,check1,subPanel1,"check1",INDENT_LEFT,INDENT_TOP,20,20,false);
        CreateLabel(0,0,lab2,"lab2","Add Ema",INDENT_LEFT,INDENT_TOP-45,EDIT_WIDTH,20);
        subPanel1.Add(lab2);     
       }
      
       
  return true;
  }
 //-------- 
  
  bool CMainPanel::CreateLabel(const long chart,const int subwindow,CLabel &object,const string name,const string text,const uint x1,const uint y1,const uint x2,const uint y2)
  {

   //--- Call Create function
   if(!object.Create(chart,name,subwindow,x1,y1,x2,y2))
     {
      return false;
     }
   //--- Addjust text
   if(!object.Text(text))
     {
      return false;
     }
   
   if(!Add(object))
     {
      return false;
     }
   return true;
  }
//-----
 bool CMainPanel::CreateButton(const long chart,const int subwindow,CButton &object,const string name,const string text,const uint x,const uint y,const uint x_size,const uint y_size)
  {

   //--- Call Create function
   if(!object.Create(chart,name,subwindow,x,y,x+x_size,y+y_size))
     {
      return false;
     }
   //--- Addjust text
   if(!object.Text(text))
     {
      return false;
     }
   //--- set button flag to unlock
   object.Locking(false);
   //--- set button flag to unpressed
   if(!object.Pressed(false))
     {
      return false;
     }
   //--- Add object to controls
   if(!Add(object))
     {
      return false;
     }
   return true;
  }
//----   
   bool CMainPanel::CreateSubPanel(const long chart,const int subwindow,CAppDialog &object,const string name,const uint x,const uint y,const uint x_size,const uint y_size)
   {
     
     if(!object.Create(chart,name,subwindow,x,y,x_size,y_size))
       return(false);
      object.Width(150);
      object.Height(150);
      object.BringToTop();   
     
     if(!Add(object))
     {
      return false;
     } 
     if(!object.Hide())
     {
      return false;
     }
   
    return (true);
   }
  bool CMainPanel::CreateCheckBox(const long chart,const int subwindow,CCheckBox &object,CAppDialog &dialog,const string name,
                                        const uint x1,const uint y1,const int width, const int height, const int status)
                                  

  {
     
   object.Create(ChartID(),name,0,x1,y1,x1+width,y1+height);
   object.Checked(status);
   object.BringToTop();

   if(!dialog.Add(object))
    {
    return false;
    }
   return(true);
   
  }     
  
  void CMainPanel::OnTick(void)
  {
    lab1.Text(DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_BID),(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS)));
    lab1.Color(clrBlue);
    
    
   return;
  
   }
 //+------------------------------------------------------------------+ 
 //| Event Handling                                                   | 
 //+------------------------------------------------------------------+ 
 EVENT_MAP_BEGIN(CMainPanel)
 ON_EVENT(ON_CLICK,but1,OnClickButton1)
 ON_EVENT(ON_CLICK,but2,OnClickButton2) 
 ON_EVENT(ON_CLICK,but3,OnClickButton3)
 ON_EVENT(ON_CLICK,but4,OnClickButton4)
 ON_EVENT(ON_CLICK,but5,OnClickButton5)
 ON_EVENT(ON_CLICK,but6,OnClickButton6)
 ON_EVENT(ON_CHANGE,check1,OnChangeCheck1)
 EVENT_MAP_END(CAppDialog) 

 void CMainPanel::OnClickButton1(void)
 { 
 if(but1.Pressed(true))
   subPanel1.Show();
  return;
 }
 void CMainPanel::OnClickButton2(void)
 {
  return;
 }
 void CMainPanel::OnClickButton3(void)
 {
  return;
 }
 void CMainPanel::OnClickButton4(void)
 {
   if(but4.Pressed(true))
   subPanel1.Hide();
  return;
 }
 void CMainPanel::OnClickButton5(void)
 {
  return;
 }
 void CMainPanel::OnClickButton6(void)
 {
  return;
 }
 void CMainPanel::OnChangeCheck1(void)
 {
  if(check1.Checked(true))
    {
     CreateEma(true);
    }
   else
       ClearEma(); 
 return;
 }
 
 bool CreateEma(bool)
 {
   ChartIndicatorAdd(ChartID(),0,Ema_handle);
   return(false);
 }

 void ClearEma()
  {
     
      ChartIndicatorDelete(0,0,ChartIndicatorName(0,0,0));
    
  }
Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

Might be related to this

Please use the SRC button when posting code - it makes it much more readable.

 

Thank you Honest but that's seems to be not solved... 

If anyone can help, thanks.

 

When you change the timeframe,  EA and indicators on the chart are unloaded and then loaded again.

If you want your EA to keep appearance when you change timeframe (or instrument) on the chart, you have to write the code that will save current status of the panel(s) and then restore it on the next start.

Reason: