- 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
Getting time or price at the specified line points
Many graphical objects include one or more straight lines. MQL5 allows you to interpolate and extrapolate points on these lines and get another coordinate from one coordinate, for example, price by time or time by price.
Interpolation is always available: it works "inside" the object, i.e., between anchor points. Extrapolation outside an object is possible only if the ray property in the corresponding direction is enabled for it (see Ray properties for objects with straight lines).
The ObjectGetValueByTime function returns the price value for the specified time. The ObjectGetTimeByValue function returns the time value for the specified price
double ObjectGetValueByTime(long chartId, const string name, datetime time, int line)
datetime ObjectGetTimeByValue(long chartId, const string name, double value, int line)
Calculations are made for an object named name on the chart with chartId. The time and value parameters specify a known coordinate for which the unknown should be calculated. Since an object can have several lines, several values will correspond to one coordinate, and therefore it is necessary to specify the line number in the line parameter.
The function returns the price or time value for the projection of the point with the specified initial coordinate relative to the line.
In case of an error, 0 will be returned, and the error code will be written to _LastError. For example, attempting to extrapolate a line value with the beam property disabled generates an OBJECT_GETVALUE_FAILED (4205) error.
The functions are applicable to the following objects:
- Trendline (OBJ_TREND)
- Trendline by angle (OBJ_TRENDBYANGLE)
- Gann line (OBJ_GANNLINE)
- Equidistant channel (OBJ_CHANNEL), 2 lines
- Linear regression channel (OBJ_REGRESSION); 3 lines
- Standard deviation channel (OBJ_STDDEVCHANNEL); 3 lines
- Arrow line (OBJ_ARROWED_LINE)
Let's check the operation of the function using a bufferless indicator ObjectChannels.mq5. It creates two objects with standard deviation and linear regression channels, after which it requests and displays in the comment the price of the upper and lower lines on future bars. For the standard deviation channel, the OBJPROP_RAY_RIGHT property is enabled, but for the regression channel, it is not (intentionally). In this regard, no values will be received from the second channel, and zeros are always displayed on the screen for it.
As new bars form, the channels will automatically move to the right. The length of the channels is set in the input parameter WorkPeriod (10 bars by default).
input int WorkPeriod = 10;
|
The CreateObjects function creates 2 channels and makes initial settings for them.
void CreateObjects()
|
The UpdateObjects function moves channels to the last WorkPeriod bars.
void UpdateObjects()
|
In the OnCalculate handler, we update the position of the channels on new bars, and on each tick, we call DisplayObjectData to get price extrapolation and display it as a comment.
int OnCalculate(const int rates_total,
|
In the DisplayObjectData function, we will find prices at anchor points on the middle line (OBJPROP_PRICE). Also, using ObjectGetValueByTime, we will request price values for the upper and lower channel lines through WorkPeriod bars in the future.
void DisplayObjectData()
|
It is important to note that due to the fact that the ray property is not enabled for the regression channel, it always gives zeros in the future (although if we asked for prices within the channel's time period, we would get the correct values).
Channels and price values at the points of their lines
Here, for channels that are 10 bars long, the extrapolation is also done on 10 bars ahead, which gives the future values shown in the line with "dev:", approximately corresponding to the right border of the window.