I have a trendline that is already drawn moving upward on chart. I need to get the price value for this trendline for the current candle time as candles get formed going forward.
How can I programmatically get the value on the trend line for my current candle time?
ObjectGetValueByShift didn't work as it gave the same result as below:
The mq4 code below seems to only give me price for when the trendline was created, but it keeps on giving me the same value on every candle going forward. I expect the value to increase because my trendline is upward, I need just the value price on that trendline for the latest candle time as price moves.
double dTrendlinePrice = ObjectGetValueByShift("TrendlineName", 0);
Thanks in advance
A slightly MT4-ish code is as follows (it's taken from my article which is not yet translated into English):
double getCurrentPrice(const string name)
int type = (int)ObjectGetInteger(0, name, OBJPROP_TYPE);
if(type == OBJ_TREND)
datetime dt1 = (datetime)ObjectGetInteger(0, name, OBJPROP_TIME, 0);
datetime dt2 = (datetime)ObjectGetInteger(0, name, OBJPROP_TIME, 1);
int i1 = iBarShift(NULL, 0, dt1, true);
int i2 = iBarShift(NULL, 0, dt2, true);
if(i1 <= i2 || i1 == -1 || i2 == -1)
Print("Incorrect line: ", name);
double p1 = ObjectGetDouble(0, name, OBJPROP_PRICE, 0);
double p2 = ObjectGetDouble(0, name, OBJPROP_PRICE, 1);
double k = -(p1 - p2)/(i2 - i1);
double b = -(i1 * p2 - i2 * p1)/(i2 - i1);
The line should have direction (ray) into the future.
Thanks Stanislav, the line does have Ray checked into the future, but the function getCurrentPrice(...) returns 0 and the error seems to be "Incorrect line: "
I tried to print more before that condition and below are the values for i1 and i2:
getCurrentPrice i1[-1] i2
Any idea why I get that?
Thanks a lot Stanislav,
I have changed the exact to give the nearest value if the bar is invalid, thus set parameters as follows iBarShift(NULL, 0, dt1, false)and that worked perfect.
A line can be treated as incorrect only in 2 cases:
- if it's drawn in wrong direction from future to past (from the right to the left), whereas it should go from the left to the right;
- the end point of the line is in future (non existing yet bar);