Extend trendline - ObjectSetInteger problem

 

In one of my indicators, I am using the command

ObjectSetInteger(0,name,OBJPROP_TIME,1,TimeCurrent());

to automatically extend an existing trendline up to the current time. Unfortunately, the slope is changed in the process, rendering the trendline unusable or requiring manual correction. Is this known as a bug, or am I making a mistake? See screenshots, trendline in orange.

Thank you for help.

trendline before extending

extended trendline

 
blackfriday: In one of my indicators, I am using the command.to automatically extend an existing trendline up to the current time. Unfortunately, the slope is changed in the process, rendering the trendline unusable or requiring manual correction. Is this known as a bug, or am I making a mistake? See screenshots, trendline in orange.

If you enable the "ray" object property for a trend line, it will automatically extend endlessly in that direction, without changing the slope.

OBJPROP_RAY_LEFT

Ray goes to the left

bool

OBJPROP_RAY_RIGHT

Ray goes to the right

bool

However, if you want to extend it to a specific bar in time and not beyond, then you have to adjust both the time and price of the end point, if you want to maintain the same slope.

It is simple maths based on the well known "y = m * x + c" equation. Just remember to take into account any missing bars (i.e. use the bar index).

Forum on trading, automated trading systems and testing trading strategies

Experts: Safe Trend Scalp

Fernando Carreiro, 2022.04.24 12:00

y = m * x + c

Let y = price
Let x = bar index

[price] = m * [index] + c

Given:

Price at Index 9 is 105 (10th bar)
Price at Index 1 is 100 (2nd bar)

Therefore:

m = Δy / Δx = (105 - 100) / ( 9 - 1 ) = 5 / 8 = 0.625

c = y - m * x = 105 - m * 9 = 100 - m * 1 = 99.375

Equation:

y = 0.625 * x + 99.375

[price] = 0.625 * [index] + 99.375

Example:

What is trend-line price at bar index 5?

[price @ 5] = 0.625 * 5 + 99.375 = 102.5

Forum on trading, automated trading systems and testing trading strategies

Experts: Safe Trend Scalp

Fernando Carreiro, 2022.04.24 12:31

Yes, obviously! It does not matter the slope as long as you clearly define the values for First and Last point.

Here is an example:

Given:

Price at Index 11 is 100 (12th bar)
Price at Index 1 is 105 (2nd bar)

Therefore:

m = Δy / Δx = (100 - 105) / ( 11 - 1 ) = -5 / 10 = -0.5

c = y - m * x = 100 - m * 11 = 105 - m * 1 = 105.5

Equation:

y = -0.5 * x + 105.5

[price] = -0.5 * [index] + 105.5

Example:

What is trend-line price at bar index 5?

[price @ 5] = -0.5 * 5 + 105.5 = 103

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Properties
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Properties
  • www.mql5.com
Object Properties - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Fernando,

thx for your instant reply. Your method is exactly what I did before - look for yourself. The result is unfortunately the same as above.

                  double price0 = ObjectGetDouble(0,Name,OBJPROP_PRICE,0);
                  double price1 = ObjectGetDouble(0,Name,OBJPROP_PRICE,1);
                  long time0 = ObjectGetInteger(0,Name,OBJPROP_TIME,0);
                  TimeRightIs = ObjectGetInteger(0,Name,OBJPROP_TIME,1); 
                  double dp = price1 - price0 ; 
                  double dt = double(TimeRightIs - time0);
                  double m;
                  if (dt != 0) m = dp / dt; else m = 0;
                  double b = price0 - m * double(time0);
               
                  double price2 = m * double(TimeCurrent()) + b;
               
                  ObjectSetInteger(0,Name,OBJPROP_TIME,1,TimeCurrent());
                  ObjectSetDouble(0,Name,OBJPROP_PRICE,1,price2);

Any idea what is wrong?

 
blackfriday #: Fernando, thx for your instant reply. Your method is exactly what I did before - look for yourself. The result is unfortunately the same as above. Any idea what is wrong?

No, not exactly! I will repeat ... " Just remember to take into account any missing bars (i.e. use the bar index). "

For example, during the weekends and non-trading hours there are no bars, so using time will skew the slope. Use the bar index, not the time.

PS! If you look at my examples above, they all use the bar index, not the time.

 
Fernando Carreiro #:
Just remember to take into account any missing bars (i.e. use the bar index)

Thank you so much Fernando! The bar index did it, you are right, it is the problem with the non-trading hours!

Reason: