ObjectCreateAndSet – library MetaTrader 5

The attached *.mqh file contains functions for creating objects and changing object properties. It is particularly useful for me when I need to quickly write some test code which creates objects on a chart and/or changes the properties of existing objects.

The other file is a script with a usage example (creates a text label, gradually rotates it by 90 degrees changing the text and its color along the way).

I. Functions for creating objects:

The functions are based on standard functions described in MQL5 Reference. Major differences from the corresponding standard functions:

  • added lines for setting tooltips and setting the visibility of objects on different timeframes;
  • removed lines for creating functions without setting coordinates, prices, anchor points (except for VLineCreate(…) and EventCreate(…));
  • based on functions for setting (changing) object properties implemented in this library, additional conditions have been added to check the success of setting the properties.

List of functions for creating objects:

Name
Brief description
Object type

VLineCreate

Create a vertical line

OBJ_VLINE

HLineCreate

Create a horizontal line

OBJ_HLINE

TrendCreate

Creates a trend line by given coordinates

OBJ_TREND

ArrowedLineCreate

Create an arrowed line by given coordinates

OBJ_ARROWED_LINE

ChannelCreate

Create an equidistant channel by given coordinates

OBJ_CHANNEL

RectangleCreate

Create a rectangle by given coordinates

OBJ_RECTANGLE

TriangleCreate

Create a triangle by given coordinates

OBJ_TRIANGLE

EllipseCreate

Create an ellipse by given coordinates

OBJ_ELLIPSE

ArrowCreate

Create OBJ_ARROW

OBJ_ARROW
TextCreate

Create a Text object

OBJ_TEXT

LabelCreate

Create a text label

OBJ_LABEL

ButtonCreate

Create a button

OBJ_BUTTON

BitmapCreate

Create a bitmap in the chart window

OBJ_BITMAP

BitmapLabelCreate

Create a Bitmap Label object

OBJ_BITMAP_LABEL

EditCreate

Create an Edit object

OBJ_EDIT

EventCreate

Create an Event object on the chart

OBJ_EVENT

RectLabelCreate

Create a rectangle label

OBJ_RECTANGLE_LABEL

 

II. Functions for setting object properties:

Using ObjectSetDouble(…), ObjectSetInteger(…) and ObjectSetString(…) without specifying a modifier:


Function name
Enumeration
Property
Brief description
Property type
1. ObSetDouble ENUM_OBJECT_PROPERTY_DOUBLE OBJPROP_SCALE Scale (property of Gann objects and Fibonacci Arcs objects) double
OBJPROP_ANGLE
Angle. For objects with no angle specified, created from a program, the value is equal to EMPTY_VALUE
double
OBJPROP_DEVIATION Deviation of the standard deviation channel
double
2. ObSetIntegerAlign ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_ALIGN Horizontal text alignment in the Edit object (OBJ_EDIT)
ENUM_ALIGN_MODE
3. ObSetIntegerAncorPoint ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_ANCHOR
Location of the anchor point for OBJ_LABEL, OBJ_BITMAP_LABEL and OBJ_TEXT objects
ENUM_ANCHOR_POINT
4.
ObSetIntegerArrowAncor
ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_ANCHOR Location of the anchor point for OBJ_ARROW objects
ENUM_ARROW_ANCHOR
5. ObSetIntegerArrowCode
ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_ARROWCODE Arrow code for OBJ_ARROW
char
6. ObSetIntegerBool ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_BACK
Object in the background
bool
OBJPROP_FILL Fill an object with color (for OBJ_RECTANGLE, OBJ_TRIANGLE, OBJ_ELLIPSE, OBJ_CHANNEL, OBJ_STDDEVCHANNEL,OBJ_REGRESSION) bool
OBJPROP_HIDDEN Prohibit showing the name of a graphical object in the list of objects from the terminal menu “Charts” – “Objects” – “List of objects”. “true” hides an object from the list. By default, “true” is set to objects displaying calendar events, trading history and to objects created from MQL5 programs. To see such graphical objects and access their properties, click
“All” button in the “List of objects” window.
bool
OBJPROP_SELECTED
Object is selected bool
OBJPROP_READONLY
Ability to edit text in the Edit object bool
OBJPROP_SELECTABLE
Object availability bool
OBJPROP_RAY_LEFT
Ray goes to the left bool
OBJPROP_RAY_RIGHT Ray goes to the right bool
OBJPROP_RAY Vertical line goes through all windows of a chart bool
OBJPROP_ELLIPSE Display the full ellipse of the Fibonacci Arc object (OBJ_FIBOARC) bool
OBJPROP_DRAWLINES Display lines for Elliott wave marking bool
OBJPROP_STATE Button state (pressed/released) bool
OBJPROP_DATE_SCALE Time scale display flag for the Chart object bool
OBJPROP_PRICE_SCALE Price scale display flag for the Chart object bool
7. ObSetIntegerBorderType ENUM_OBJECT_PROPERTY_INTEGER
OBJPROP_BORDER_TYPE
Border type for the Rectangle label object
ENUM_BORDER_TYPE
8. ObSetIntegerColor ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_COLOR Color (depending on the object type, controls the color of lines, text etc.) color
OBJPROP_BGCOLOR Background color for OBJ_EDIT, OBJ_BUTTON, OBJ_RECTANGLE_LABEL color
OBJPROP_BORDER_COLOR Border color for OBJ_EDIT and OBJ_BUTTON color
9. ObSetIntegerCorner
ENUM_OBJECT_PROPERTY_INTEGER
OBJPROP_CORNER Chart corner for attaching a graphical object
ENUM_BASE_CORNER
10. ObSetIntegerInt ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_WIDTH Line width int
OBJPROP_LEVELS Number of levels int
OBJPROP_FONTSIZE Font size int
OBJPROP_TIMEFRAMES Visibility of an object on timeframes (a set of flags) int
OBJPROP_XDISTANCE Distance in pixels along the X axis from the anchor corner (see note in MQL5 Reference) int
OBJPROP_YDISTANCE Distance in pixels along the Y axis from the anchor corner (see note in MQL5 Reference)
int
OBJPROP_XSIZE Object width along the X axis in pixels. Specified for OBJ_LABEL (read only), OBJ_BUTTON, OBJ_CHART, OBJ_BITMAP, OBJ_BITMAP_LABEL, OBJ_EDIT, OBJ_RECTANGLE_LABEL int
OBJPROP_YSIZE Object height along the Y axis in pixels. Specified for OBJ_LABEL (read only), OBJ_BUTTON, OBJ_CHART, OBJ_BITMAP, OBJ_BITMAP_LABEL, OBJ_EDIT, OBJ_RECTANGLE_LABEL int
OBJPROP_XOFFSET The X coordinate of the upper left corner of the rectangular visible area in Bitmap Label and Bitmap graphical objects (OBJ_BITMAP_LABEL and OBJ_BITMAP). The value is set in pixels relative to the upper left corner of the original image int
OBJPROP_YOFFSET The Y coordinate of the upper left corner of the rectangular visible area in Bitmap Label and Bitmap graphical objects (OBJ_BITMAP_LABEL and OBJ_BITMAP). The value is set in pixels relative to the upper left corner of the original image int
OBJPROP_CHART_SCALE Scale for the Chart object (from 0 to 5) int
11. ObSetIntegerLineStyle ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_STYLE
Style
ENUM_LINE_STYLE
12. ObSetIntegerLong ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_ZORDER Priority of a graphical object for receiving the event of clicking on a chart (CHARTEVENT_CLICK). The default zero value is set when creating an object, but the priority can be increased if necessary. When applying objects one over another only one of them with the highest priority will receive the CHARTEVENT_CLICK event.
long

ObSetString ENUM_OBJECT_PROPERTY_STRING OBJPROP_NAME Object name
string
OBJPROP_TEXT Object description (text contained in the object) string
OBJPROP_TOOLTIP Tooltip text. If the property is not set, then the automatically generated tooltip is shown. To disable the tooltip, set the value “\n” (newline) for it string
OBJPROP_FONT Font string
OBJPROP_SYMBOL Symbol for the Chart object string
Alternative:  3D Moving Average - indicator MetaTrader 5

 

Using ObjectSetDouble(…), ObjectSetInteger(…) and ObjectSetString(…) with specifying a modifier:


Name
Enumeration Property Brief description Property type
1. ObSetDoubleMod ENUM_OBJECT_PROPERTY_DOUBLE OBJPROP_PRICE Price coordinate – double, modifier=anchor point number double
OBJPROP_LEVELVALUE Level value – double, modifier=level number
double
2.
ObSetIntegerLevelColorMod ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_LEVELCOLOR Level line color – color, modifier=level number
color
3.
ObSetIntegerLevelStyleMod
ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_LEVELSTYLE
Level line style ENUM_LINE_STYLE, modifier=level number
ENUM_LINE_STYLE
4.
ObSetIntegerLevelWidthMod
ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_LEVELWIDTH
Level line width – int, modifier=level number
int
5. ObSetIntegerTimeMod ENUM_OBJECT_PROPERTY_INTEGER OBJPROP_TIME
Time coordinate – datetime, modifier=anchor point number
datetime
6. ObSetStringMod ENUM_OBJECT_PROPERTY_STRING OBJPROP_LEVELTEXT Level description – string, modifier=level number string
OBJPROP_BMPFILE
BMP file name for the Bitmap Level object – string. See also Resources in the Reference. Modifier: 0-ON state, 1-OFF state string

 

Additional functions:



bool ObDelete (long chart_ID,string name)
Delete a single object with specified name
Alternative:  CandleMidPoint - indicator MetaTrader 5

 

III. Usage:

You can use it as an include file or you can just copy certain functions from it to your source code.

If you choose the second way, I think it’s better to first use it as an include file. Once you implement all necessary function calls you can remove the include directive (#include…).

Then, the compiler will point out which functions you need to copy from the include file.

Use as an include file:

  1. Save the attached file ObjectCreateAndSet.mqh to “Include” folder. To do so, you can use the Navigator window in MetaEditor. In the Navigator window, right-click the “Include” folder and choose “Open Folder“.
  2. To use the library in your code, copy the #include… line from the beginning of the file and paste it to your source code:
    #include <ObjectCreateAndSet.mqh>
    The attached script provides an example of including the library.
  3. After that, compile your code. It will allow to see the tooltips for included functions when you start entering their names:

Full name of a function in a tooltip after entering its first few letters

Fig.1. Full name of a function in a tooltip after entering its first few letters


Fig.2. Parameters of a function in a tooltip

By the way, you can you use the functions list button on the MetaEditor toolbar to quickly view the list of functions used in the current file:

Fig.3. Button for showing the list of functions used in a file

When you press the button, a drop-down list appears. To go to a particular function, click its name in the list.

Fig.4. Pressing the button opens the list of functions in a file (useful to quickly move to a certain function)

Updates:

  • 2015.09.14: In the ObDelete() function, added output of object’s name to the Experts tab if there is an error when deleting the object. It facilitates finding errors in the code.


📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks