First, get time&data of the points of the trend line (here: they are in struct).
Next, find the slope.
Finally, you can compute the trend line value at any point of time.
In the sample example below, all is moved into struct.
Keep in mind, that if you have several such lines on the chart, you have to loop over all of them to find the last one (if you need the last, of course).
struct TrendLine{
datetime m_startDt;
double m_start;
datetime m_endDt;
double m_end;
double m_slope; // slope, price per bar
void Init(const string objName) {
m_startDt=(datetime)ObjectGetInteger(0,objName,OBJPROP_TIME1);
m_start = ObjectGetDouble(0,objName,OBJPROP_PRICE1);
m_endDt =(datetime)ObjectGetInteger(0,objName,OBJPROP_TIME2);
m_end = ObjectGetDouble(0,objName,OBJPROP_PRICE2);
}
void computeSlope() {
const int distanceInBars=iBarShift(_Symbol,0,m_startDt)-iBarShift(_Symbol,0,m_endDt);
m_slope = (m_end-m_start)/distanceInBars;
}
double getTrendLineValueByTime(const datetime time) {
if(m_slope==0.)
computeSlope();
const int distanceInBarsFromEnd=iBarShift(_Symbol,0,time)-iBarShift(_Symbol,0, m_endDt);
const double result=m_end-slope*distanceInBarsFromEnd;
return result;
}
};
so you will use it in the following way:
void OnTick() {
TrendLine trendLine;
string objName;
for(int i=ObjectsTotal()-1;i>=0;i--) {
objName=ObjectName(i);
if(ObjectType(objName)!=OBJ_TREND) continue;
trendLine.Init(objName);
}
datetime t = iTime(_Symbol,0,1);// let's suppose we need TL value for last bar
double tlValue = trendLine.getTrendLineValueByTime(t);
Print(__LINE__," ",__FILE__," trend line at time = ",t," = ",DoubleToString(tlValue,8));
}