How to draw a rectangle on chart

Hello my friends,

Could you please help me to make this code better.**

I succeeded at building a Menu for my EA that help me to view important information. the Menu look like this:

I want to add a rectangle frame to the background so it will look like this:

02__1

Here is the code of the Menu:

//===Choose to show or hide Menu
#define SHOW 0
#define HIDE 1
enum  SH { Show,Hide };
//---Expert Proprities
extern SH     ShowMenu    = SHOW;
extern double LOTSIZE     = 0.01;
extern int    Profit      = 50;
extern int    StopLoss    = 50;
extern color  MenuColor   = clrWhite;
extern int    MenuFont    = 11;
extern int    MagicNumber = 0;

//---initialize global variables
int    medzera,i;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  if(ShowMenu == SHOW)
   {  
      DrawMenu();
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   if(ShowMenu == SHOW)
   {
      ObjectDelete("LotLabel");
      ObjectDelete("TPLabel");
      ObjectDelete("SPLabel");
      ObjectDelete("LotValue");
      ObjectDelete("TPValue");
      ObjectDelete("SPValue");
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//---Show or hide Menu
if(ShowMenu == SHOW)
   {
      ReDrawMenu();
   }
   return(0);
}
//+------------------------------------------------------------------+
//| DRAW MENU OBJECTS FUNCTIONS                                      |
//+------------------------------------------------------------------+

//--- Function to draw Menu for the first time
bool DrawMenu()
{
      medzera = 8;
//--- Create Labels Cells
      
      ObjectCreate("LotLabel",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("TPLabel",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("SPLabel",OBJ_LABEL,0,0,0,0,0);
//---Modify Label Cells
     
     ObjectSetText(     "LotLabel", "Lot Size: " , MenuFont, "Arial", MenuColor);
     ObjectSet("LotLabel",OBJPROP_XDISTANCE,medzera * MenuFont);     
     ObjectSet("LotLabel",OBJPROP_YDISTANCE,10+1*MenuFont);
     ObjectSet("LotLabel",OBJPROP_CORNER,1);
          
     ObjectSetText("TPLabel", "Take Profit in Pips: ", MenuFont, "Arial",MenuColor);
     ObjectSet("TPLabel",OBJPROP_XDISTANCE,medzera * MenuFont);     
     ObjectSet("TPLabel",OBJPROP_YDISTANCE,15+2*(MenuFont+2));
     ObjectSet("TPLabel",OBJPROP_CORNER,1);
          
     ObjectSetText("SPLabel", "Stop Loss in Pips: ", MenuFont, "Arial",MenuColor);
     ObjectSet("SPLabel",OBJPROP_XDISTANCE,medzera * MenuFont);     
     ObjectSet("SPLabel",OBJPROP_YDISTANCE,20+3*(MenuFont+2));
     ObjectSet("SPLabel",OBJPROP_CORNER,1);
//--- Create Numerical Value Cells
     
      ObjectCreate("LotValue",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("TPValue",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("SPValue",OBJ_LABEL,0,0,0,0,0);

//--- Modify numerical Value Cells      
                
     ObjectSetText("LotValue", ""+ LOTSIZE, MenuFont, "Arial",MenuColor);
     ObjectSet("LotValue",OBJPROP_XDISTANCE, 3 * MenuFont);     
     ObjectSet("LotValue",OBJPROP_YDISTANCE,10+1*(MenuFont+2));
     ObjectSet("LotValue",OBJPROP_CORNER,1);
     
     ObjectSetText("TPValue"," "+Profit, MenuFont, "Arial",MenuColor);
     ObjectSet("TPValue",OBJPROP_XDISTANCE,3 * MenuFont);     
     ObjectSet("TPValue",OBJPROP_YDISTANCE,15+2*(MenuFont+2));
     ObjectSet("TPValue",OBJPROP_CORNER,1);
              
     ObjectSetText("SPValue", " "+ StopLoss, MenuFont, "Arial",MenuColor);
     ObjectSet("SPValue",OBJPROP_XDISTANCE,3 * MenuFont);     
     ObjectSet("SPValue",OBJPROP_YDISTANCE,20+3*(MenuFont+2));
     ObjectSet("SPValue",OBJPROP_CORNER,1);
    
      
return(0);
}

//--- Function to redraw Menu on each tick
bool ReDrawMenu()
{
      medzera = 8;
            
     ObjectSetText("LotValue", ""+LOTSIZE, MenuFont, "Arial",MenuColor);
     ObjectSetText("TPValue", Profit, MenuFont, "Arial",MenuColor);
     ObjectSetText("SPValue", StopLoss, MenuFont, "Arial",MenuColor);

return(0);
}

Files: Draw_Menu_TEST.mq4 5 KB

Add this object:

 // *** Background
   ObjectCreate(ChartID(), objectBackgroundName, OBJ_RECTANGLE_LABEL, 0, 0, 0);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_XDISTANCE, 10);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_YDISTANCE, 10);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_XSIZE, 500);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_YSIZE, 275);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_BGCOLOR, clrBlack);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_BORDER_COLOR, clrBlack);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_BORDER_TYPE, BORDER_FLAT);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_WIDTH, 0);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_BACK, false);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_SELECTABLE, false);
   ObjectSetInteger(ChartID(), objectBackgroundName, OBJPROP_HIDDEN, true);


#property link      "http://www.mql4.com"
#property version   "1.00"
#property strict

#include <Controls\Panel.mqh>
#include <Charts\Chart.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
      ShowPanel(300,200);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   MyPanel.Destroy();
   MyChart.Detach();
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
      static int chartwidth;
      
      if (id==CHARTEVENT_CHART_CHANGE)
         {
         if (MyChart.WidthInPixels()!=chartwidth)
            {
            chartwidth=MyChart.WidthInPixels();
            MyPanel.Move(chartwidth-300,0);
            }
         }
      
  }
//+------------------------------------------------------------------+

CPanel MyPanel;
CChart MyChart;

void ShowPanel(int width, int height)
   {
   int x;

   MyChart.Attach(ChartID());
   x=MyChart.WidthInPixels()-width;
   
   if (!MyPanel.Create(NULL,"MyPanel",NULL,x,0,x+width,height))
      return;
      
   MyPanel.ColorBackground(Green);
   MyPanel.Show(); 
   }

The sample keeps the rectangle in place even when the chart has been changed in its size.

Rather use MQL5 Classes for things like that :wink:

Hope this helps …

Hello, please take here:

//===Choose to show or hide Menu
#define SHOW 0
#define HIDE 1
enum  SH { Show,Hide };
//---Expert Proprities

extern SH     ShowMenu    = SHOW;
extern double LOTSIZE     = 0.01;
extern int    Profit      = 50;
extern int    StopLoss    = 50;
extern color  MenuColor   = clrWhite;
extern color  LiningColor = clrDarkGreen;
extern int    MenuFont    = 11;
extern int    MagicNumber = 0;

//---initialize global variables
int    medzera,i;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  if(ShowMenu == SHOW)
   {  
      DrawMenu();
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   if(ShowMenu == SHOW)
   {
      ObjectDelete("Lining");
      ObjectDelete("LotLabel");
      ObjectDelete("TPLabel");
      ObjectDelete("SPLabel");
      ObjectDelete("LotValue");
      ObjectDelete("TPValue");
      ObjectDelete("SPValue");
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//---Show or hide Menu
if(ShowMenu == SHOW)
   {
      ReDrawMenu();
   }
   return(0);
}
//+------------------------------------------------------------------+
//| DRAW MENU OBJECTS FUNCTIONS                                      |
//+------------------------------------------------------------------+

//--- Function to draw Menu for the first time
bool DrawMenu()
{
      medzera = 8;
//--- Create Labels Cells

      ObjectCreate("Lining",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("LotLabel",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("TPLabel",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("SPLabel",OBJ_LABEL,0,0,0,0,0);
//---Modify Label Cells

     ObjectSetText("Lining", "ggg",5 * MenuFont, "Webdings",LiningColor);
     ObjectSet("Lining",OBJPROP_XDISTANCE,MenuFont);     
     ObjectSet("Lining",OBJPROP_YDISTANCE,MenuFont+3);
     ObjectSet("Lining",OBJPROP_CORNER,1);

     ObjectSetText(     "LotLabel", "Lot Size: " , MenuFont, "Arial", MenuColor);
     ObjectSet("LotLabel",OBJPROP_XDISTANCE,medzera * MenuFont);     
     ObjectSet("LotLabel",OBJPROP_YDISTANCE,10+1*MenuFont);
     ObjectSet("LotLabel",OBJPROP_CORNER,1);
          
     ObjectSetText("TPLabel", "Take Profit in Pips: ", MenuFont, "Arial",MenuColor);
     ObjectSet("TPLabel",OBJPROP_XDISTANCE,medzera * MenuFont);     
     ObjectSet("TPLabel",OBJPROP_YDISTANCE,15+2*(MenuFont+2));
     ObjectSet("TPLabel",OBJPROP_CORNER,1);
          
     ObjectSetText("SPLabel", "Stop Loss in Pips: ", MenuFont, "Arial",MenuColor);
     ObjectSet("SPLabel",OBJPROP_XDISTANCE,medzera * MenuFont);     
     ObjectSet("SPLabel",OBJPROP_YDISTANCE,20+3*(MenuFont+2));
     ObjectSet("SPLabel",OBJPROP_CORNER,1);
//--- Create Numerical Value Cells
     
      ObjectCreate("LotValue",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("TPValue",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("SPValue",OBJ_LABEL,0,0,0,0,0);

//--- Modify numerical Value Cells      
                
     ObjectSetText("LotValue", ""+ LOTSIZE, MenuFont, "Arial",MenuColor);
     ObjectSet("LotValue",OBJPROP_XDISTANCE, 3 * MenuFont);     
     ObjectSet("LotValue",OBJPROP_YDISTANCE,10+1*(MenuFont+2));
     ObjectSet("LotValue",OBJPROP_CORNER,1);
     
     ObjectSetText("TPValue"," "+Profit, MenuFont, "Arial",MenuColor);
     ObjectSet("TPValue",OBJPROP_XDISTANCE,3 * MenuFont);     
     ObjectSet("TPValue",OBJPROP_YDISTANCE,15+2*(MenuFont+2));
     ObjectSet("TPValue",OBJPROP_CORNER,1);
              
     ObjectSetText("SPValue", " "+ StopLoss, MenuFont, "Arial",MenuColor);
     ObjectSet("SPValue",OBJPROP_XDISTANCE,3 * MenuFont);     
     ObjectSet("SPValue",OBJPROP_YDISTANCE,20+3*(MenuFont+2));
     ObjectSet("SPValue",OBJPROP_CORNER,1);


return(0);
}

//--- Function to redraw Menu on each tick
bool ReDrawMenu()
{
      medzera = 8;
            
     ObjectSetText("LotValue", ""+LOTSIZE, MenuFont, "Arial",MenuColor);
     ObjectSetText("TPValue", Profit, MenuFont, "Arial",MenuColor);
     ObjectSetText("SPValue", StopLoss, MenuFont, "Arial",MenuColor);

return(0);
}

Files:Draw_Menu_TEST_x1i.mq4 5 KB

And by the way, to keep your other objects in place too, is just 8-10 lines of code more, when your labels are created as CLabel Objects.

You mean I should Use OBJ_RECTANGLE_LABEL instead of OBJ_RECTANGLE.

OK, I did it :slight_smile:

I also changed the way of changing the size of the Menu, instead of using MenuFont I will use MenuSize parameter. It work fine now and I can change the size of the Menu , and I can Hide or Show it.

Now the code look like this:

//===Choose to show or hide Menu
#define SHOW 0
#define HIDE 1
enum  SH { Show,Hide };
//---Expert Proprities
extern SH     ShowMenu    = SHOW;
extern double LOTSIZE     = 0.01;
extern int    Profit      = 50;
extern int    StopLoss    = 50;
extern color  MenuColor   = clrWhite;
extern color  FrameColor  = clrGreen;
extern int    MenuSize    = 1;
extern int    MagicNumber = 0;

//---initialize global variables
int    medzera,i;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  if(ShowMenu == SHOW)
   {  
      DrawMenu();
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   if(ShowMenu == SHOW)
   {
      ObjectDelete("FrameLabel");
      ObjectDelete("LotLabel");
      ObjectDelete("TPLabel");
      ObjectDelete("SPLabel");
      ObjectDelete("LotValue");
      ObjectDelete("TPValue");
      ObjectDelete("SPValue");
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//---Show or hide Menu
if(ShowMenu == SHOW)
   {
      ReDrawMenu();
   }
   return(0);
}
//+------------------------------------------------------------------+
//| DRAW MENU OBJECTS FUNCTIONS                                      |
//+------------------------------------------------------------------+

//--- Function to draw Menu for the first time
bool DrawMenu()
{
      medzera = 8;
 int  MenuFont = MenuSize*11;
//--- Create Labels Cells

      ObjectCreate("FrameLabel",OBJ_RECTANGLE_LABEL,0,0,0,0,0,0);
      ObjectCreate("LotLabel",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("TPLabel",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("SPLabel",OBJ_LABEL,0,0,0,0,0);
//---Modify Label Cells

//****
   
   ObjectSet("FrameLabel",OBJPROP_BGCOLOR,FrameColor);
   ObjectSet("FrameLabel",OBJPROP_CORNER,1);
   ObjectSet("FrameLabel",OBJPROP_BACK,true);
   ObjectSet("FrameLabel",OBJPROP_XDISTANCE,MenuSize*220);
   ObjectSet("FrameLabel",OBJPROP_YDISTANCE,MenuSize*18);
   ObjectSet("FrameLabel",OBJPROP_XSIZE,MenuSize*200);
   ObjectSet("FrameLabel",OBJPROP_YSIZE,MenuSize*60);
      
//***
     
     ObjectSetText(     "LotLabel", "Lot Size: " , MenuFont, "Arial", MenuColor);
     ObjectSet("LotLabel",OBJPROP_XDISTANCE,MenuSize*88);     
     ObjectSet("LotLabel",OBJPROP_YDISTANCE,MenuSize*21);
     ObjectSet("LotLabel",OBJPROP_CORNER,1);
          
     ObjectSetText("TPLabel", "Take Profit in Pips: ", MenuFont, "Arial",MenuColor);
     ObjectSet("TPLabel",OBJPROP_XDISTANCE,MenuSize*88);     
     ObjectSet("TPLabel",OBJPROP_YDISTANCE,MenuSize*41);
     ObjectSet("TPLabel",OBJPROP_CORNER,1);
          
     ObjectSetText("SPLabel", "Stop Loss in Pips: ", MenuFont, "Arial",MenuColor);
     ObjectSet("SPLabel",OBJPROP_XDISTANCE,MenuSize*88);     
     ObjectSet("SPLabel",OBJPROP_YDISTANCE,MenuSize*59);
     ObjectSet("SPLabel",OBJPROP_CORNER,1);
//--- Create Numerical Value Cells
     
      ObjectCreate("LotValue",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("TPValue",OBJ_LABEL,0,0,0,0,0);
      ObjectCreate("SPValue",OBJ_LABEL,0,0,0,0,0);

//--- Modify numerical Value Cells      
                
     ObjectSetText("LotValue", ""+ LOTSIZE, MenuFont, "Arial",MenuColor);
     ObjectSet("LotValue",OBJPROP_XDISTANCE, MenuSize*33);     
     ObjectSet("LotValue",OBJPROP_YDISTANCE,MenuSize*21);
     ObjectSet("LotValue",OBJPROP_CORNER,1);
     
     ObjectSetText("TPValue"," "+Profit, MenuFont, "Arial",MenuColor);
     ObjectSet("TPValue",OBJPROP_XDISTANCE,MenuSize*33);     
     ObjectSet("TPValue",OBJPROP_YDISTANCE,MenuSize*41);
     ObjectSet("TPValue",OBJPROP_CORNER,1);
              
     ObjectSetText("SPValue", " "+ StopLoss, MenuFont, "Arial",MenuColor);
     ObjectSet("SPValue",OBJPROP_XDISTANCE,MenuSize*33);     
     ObjectSet("SPValue",OBJPROP_YDISTANCE,MenuSize*59);
     ObjectSet("SPValue",OBJPROP_CORNER,1);
    
      
return(0);
}

//--- Function to redraw Menu on each tick
bool ReDrawMenu()
{
      medzera = 8;
 int  MenuFont = MenuSize*11;
            
     ObjectSetText("LotValue", ""+LOTSIZE, MenuFont, "Arial",MenuColor);
     ObjectSetText("TPValue", Profit, MenuFont, "Arial",MenuColor);
     ObjectSetText("SPValue", StopLoss, MenuFont, "Arial",MenuColor);

return(0);
}

Files:Draw_Menu_TEST.mq4 5 KB

Thanks for your effort gsidworth21

You’re welcome :slight_smile: But one question: Why don’t you use the classes? Its much safer and easier than this native stuff.

Thanks a lot, I’m still new to MT4 programming and I might try it in the future.

I am not new to programming, but also new to MT … you shouldn’t do it in the future, you should do it from the beginning if youre familiar with oop.