- Object types and features of specifying their coordinates
- Time and price bound objects
- Objects bound to screen coordinates
- Creating objects
- Deleting objects
- Finding objects
- Overview of object property access functions
- Main object properties
- Price and time coordinates
- Anchor window corner and screen coordinates
- Defining anchor point on the object
- Managing the object state
- Priority of objects (Z-Order)
- Object display settings: color, style, and frame
- Font settings
- Rotating text at an arbitrary angle
- Determining object width and height
- Visibility of objects in the context of timeframes
- Assigning a character code to a label
- Ray properties for objects with straight lines
- Managing object pressed state
- Adjusting images in bitmap objects
- Cropping (outputting part) of an image
- Input field properties: alignment and read-only
- Standard deviation channel width
- Setting levels in level objects
- Additional properties of Gann, Fibonacci, and Elliot objects
- Chart object
- Moving objects
- Getting time or price at the specified line points
Finding objects
There are three functions to search for objects on the chart. The first two, the ObjectsTotal and ObjectName, allow you to sort through objects by name, and then, if necessary, use the name of each object to analyze its other properties (we will describe how this is done in the next section). The third function, ObjectFind, allows you to check the existence of an object by a known name. The same could be done by simply requesting some property via the ObjectGet function: if there is no object with the passed name, we will get an error in _LastError, but this is less convenient than calling ObjectFind. Besides, the function immediately returns the number of the window in which the object is located.
int ObjectsTotal(long chartId, int window = -1, int type = -1)
The function returns the number of objects on the chart with the chartId identifier (0 means current chart). Only objects in the subwindow with the specified window number are considered in the calculation (0 represents the main window, -1 represents the main window and all subwindows). Note that only objects of the specific type specified in the type parameter are taken into account (-1 indicates all types by default). The value of type can be an element from the ENUM_OBJECT enumeration.
The function is executed synchronously, that is, it blocks the execution of the calling MQL program until the result is received.
string ObjectName(long chartId, int index, int window = -1, int type = -1)
The function returns the name of the object under the index number on the chart with the chartId identifier. When compiling the internal list, within which the object is searched, the specified subwindow number (window) and object type (type) are taken into account. The list is sorted by object names in lexicographic order, that is, in particular, alphabetically, case sensitive.
Like ObjectsTotal, during its execution, ObjectName waits for the entire queue of chart commands to be fetched, and then returns the name of the object from the updated list of objects.
In case of an error, an empty string will be obtained, and the OBJECT_NOT_FOUND (4202) error code will be stored in _LastError.
To test the functionality of these two functions, let's create a script called ObjectFinder.mq5 that logs all objects on all charts. It uses chart iteration functions (ChartFirst and ChartNext), as well as functions for getting chart properties (ChartSymbol, ChartPeriod, and ChartGetInteger).
#include <MQL5Book/Periods.mqh>
|
For each chart, we determine the number of subwindows (ChartGetInteger(id, CHART_WINDOWS_TOTAL)), call ObjectsTotal for each subwindow, and call ObjectName in the inner loop. Next, by name, we find the type of object and display them together in the log.
Below is a version of the possible result of the script (with abbreviations).
EURUSD H1 (132358585987782873)
|
Here, in particular, you can see that on the XAUUSD, D1 chart there are objects generated by the ObjectSimpleShowcase.mq5 script. There are no objects in some charts and in some subwindows.
int ObjectFind(long chartId, const string name)
The function searches for an object by name on the chart specified by the identifier and, if successful, returns the number of the window where it was found.
If the object is not found, the function returns a negative number. Like the previous functions in this section, the ObjectFind function uses a synchronous call.
We will see an example of using this function in the ObjectCopy.mq5 script in the next section.