As a follow-up, the following posts can be of use to understanding how to use the equation "y = mx + c" for the ZigZag lines.
Even though the posts below are for calculating a Trend-Line, it is the same principle to using DRAW_LINE buffer to draw a line for the ZigZag lines. In other words, it word be the simulating of a TrendLine using DRAW_LINE buffer.
No! Do you even understand what the ObjectGetValueByShif() does?
The function, does exactly the same thing as the "y=mx+c" equation. It calculates the price for the bars either inside or outside of the two connecting points of the trend-line.
Here is an example code of a Script, to demonstrate the two methods, that has been fully tested with log results:
#property strict#property script_show_inputs// Script input parametersinputuint
nStartPointBarShift = 5, // Bar shift of starting point of trend-line
nEndPointBarShift = 15, // Bar shift of ending point of trend-line
nQueryPointBarShift = 10; // Bar shift of query point of trend-line// Script start event handlervoidOnStart()
{
// Check if parameters are acceptableif( nStartPointBarShift == nEndPointBarShift )
{
Print( "Invalid Parameters!" );
return;
};
// Calculate valuesint
nDeltaBarShift = (int) nEndPointBarShift - (int) nStartPointBarShift; // Change in bar shiftdatetime
dtStartPointTime = iTime( _Symbol, _Period, nStartPointBarShift ), // Time at starting point
dtEndPointTime = iTime( _Symbol, _Period, nEndPointBarShift ); // Time at ending pointdouble
dbStartPointPrice = iClose( _Symbol, _Period, nStartPointBarShift ), // Price at starting point
dbEndPointPrice = iClose( _Symbol, _Period, nEndPointBarShift ), // Price at ending point
dbDeltaPrice = dbEndPointPrice - dbStartPointPrice, // Change in price
dbLineSlope = dbDeltaPrice / nDeltaBarShift, // Slope is the value of "m"
dbOffset = dbEndPointPrice - dbLineSlope * nEndPointBarShift; // Offset is the value of "c"// Draw trend-line objectstring sObjectName = "Test-Trendline";
if( ObjectCreate( sObjectName, OBJ_TREND, 0, // Create trend-line object
dtStartPointTime, dbStartPointPrice, // Starting point of trend-line
dtEndPointTime, dbEndPointPrice ) ) // Ending point of trend-line
{
// Get price by both methodsdouble
dbValueByShift = ObjectGetValueByShift( sObjectName, nQueryPointBarShift ),
dbPriceByShift = dbLineSlope * nQueryPointBarShift + dbOffset;
// Print out the values to the logPrintFormat( "Starting Point: %s @ %d", DoubleToString( dbStartPointPrice, _Digits ), nStartPointBarShift );
PrintFormat( "Ending Point: %s @ %d", DoubleToString( dbEndPointPrice, _Digits ), nEndPointBarShift );
PrintFormat( "Query Point: %s @ %d (by ObjectGetValueByShift)", DoubleToString( dbValueByShift, _Digits ), nQueryPointBarShift );
PrintFormat( "Query Point: %s @ %d (by y=mx+c calculation)", DoubleToString( dbPriceByShift, _Digits ), nQueryPointBarShift );
// Delete ObjectObjectDelete( sObjectName ); // Delete trend-line object
};
};
Yes, it does work, but you have to create the intermediate points as well and not just the start and end points. That is why I wrote that you need to apply the equation "y = mx + c" (equation for a line).
And please don't use trend-line graphical objects. That is inefficient.
This method can work, but it has two drawbacks. 1- It is very complex in terms of coding. I feel like the complexity does not worth the outcome. 2- In this Zigzag indicator sometimes I need to connect high of a candle to its low of the same candle, when we use DRAW_LINE each buffer will connect to its own values, that is buffer 1 cannot be connected to buffer 2, therefore I cannot connect high of a candle to its low. The only way for that is using DRAW_ZIGZAG.
This method can work, but it has two drawbacks. 1- It is very complex in terms of coding. I feel like the complexity does not worth the outcome. 2- In this Zigzag indicator sometimes I need to connect high of a candle to its low of the same candle, when we use DRAW_LINE each buffer will connect to its own values, that is buffer 1 cannot be connected to buffer 2, therefore I cannot connect high of a candle to its low. The only way for that is using DRAW_ZIGZAG.
If the objective is draw a colour ZigZag in MQL4 that does not support it natively, then I don't believe the extra steps could be considered "not worth it", but that is a matter of subjective opinion.
That is true, and a partial solution would be to mix both the DRAW_ZIGZAG and the DRAW_LINE method for one of the directions, but it is not foolproof.
If the objective is draw a colour ZigZag in MQL4 that does not support it natively, then I don't believe the extra steps could be considered "not worth it", but that is a matter of subjective opinion.
That is true, and a partial solution would be to mix both the DRAW_ZIGZAG and the DRAW_LINE method for one of the directions, but it is not foolproof.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
As a follow-up, the following posts can be of use to understanding how to use the equation "y = mx + c" for the ZigZag lines.
Even though the posts below are for calculating a Trend-Line, it is the same principle to using DRAW_LINE buffer to draw a line for the ZigZag lines. In other words, it word be the simulating of a TrendLine using DRAW_LINE buffer.
Yes, it does work, but you have to create the intermediate points as well and not just the start and end points. That is why I wrote that you need to apply the equation "y = mx + c" (equation for a line).
And please don't use trend-line graphical objects. That is inefficient.
This method can work, but it has two drawbacks.
1- It is very complex in terms of coding. I feel like the complexity does not worth the outcome.
2- In this Zigzag indicator sometimes I need to connect high of a candle to its low of the same candle, when we use DRAW_LINE each buffer will connect to its own values, that is buffer 1 cannot be connected to buffer 2, therefore I cannot connect high of a candle to its low. The only way for that is using DRAW_ZIGZAG.
This method can work, but it has two drawbacks.
1- It is very complex in terms of coding. I feel like the complexity does not worth the outcome.
2- In this Zigzag indicator sometimes I need to connect high of a candle to its low of the same candle, when we use DRAW_LINE each buffer will connect to its own values, that is buffer 1 cannot be connected to buffer 2, therefore I cannot connect high of a candle to its low. The only way for that is using DRAW_ZIGZAG.
Thank you man for all your help, always the best.