How to get price at trend line already drawn as price moves?

To add comments, please log in or register
Kaone01
43
Kaone01  

Hi Everyone,


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 

Stanislav Korotky
25218
Stanislav Korotky  

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);
      return 0;
    }
    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);
        
    return b;
  }
  //...
}

The line should have direction (ray) into the future.

Kaone01
43
Kaone01  
Stanislav Korotky:

A slightly MT4-ish code is as follows (it's taken from my article which is not yet translated into English):

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[0]

Any idea why I get that?

Kaone01
43
Kaone01  

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.

Stanislav Korotky
25218
Stanislav Korotky  
Kaone01:

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[0]

Any idea why I get that?


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);

To add comments, please log in or register