enivid: We cannot use time as x in f(x) = kx + b calculation because charts are non-linear in respect to time. For example, daily charts have weekends and holidays omitted, so the neighboring bars could be 1 day apart or 3 days apart.
I think if you consider time as linear to the right of bar 0 and use bar number to the left of bar 0 you can calculate what you need. Anything drawn to the right of bar 0 may well change position when the weekend arrives if they have been drawn at a time that occurs when the markets are closed . . .
Works miracles :-). If someone needs, here's the code, which calculates vertical difference between main and auxiliary lines of the OBJ_CHANNEL:
string BorderChannel = "MyChannel";
datetime T1 = ObjectGet(BorderChannel, OBJPROP_TIME1);
datetime T2 = ObjectGet(BorderChannel, OBJPROP_TIME2);
datetime T3 = ObjectGet(BorderChannel, OBJPROP_TIME3);
double P1 = ObjectGet(BorderChannel, OBJPROP_PRICE1);
double P2 = ObjectGet(BorderChannel, OBJPROP_PRICE2);
double P3 = ObjectGet(BorderChannel, OBJPROP_PRICE3);
double Diff = 0;
// Simple casesif (T3 == T1) // Auxiliary point with the same time as the first point of the main line
{
Diff = P1 - P3;
}
elseif (T3 == T2) // Auxiliary point with the same time as the second point of the main line
{
Diff = P2 - P3;
}
// Difficult caseelse
{
// Use linear function to calculate auxiliary price at T1// Find bar numbers of T1 and T2// double because will be used in divisiondouble B1 = ObjectGetShiftByValue(BorderChannel, P1);
double B2 = ObjectGetShiftByValue(BorderChannel, P2);
// Normalize T3 by removing excess time values. For example, hours and minutes on D1 timeframe.
T3 = T3 - T3 % (Period() * 60);
if (T3 > Time[0]) // If T3 is placed in the future, need to calculate its negative shift
{
double B3 = (Time[0] - T3) / (Period() * 60);
}
else// Otherwise shift can be found by scrolling through bars
{
for (int i = 0; i < Bars; i++)
if (Time[i] == T3)
{
B3 = i;
break;
}
}
// Slopedouble k = (P2 - P1) / (B2 - B1);
// Y-intercept is calculated for auxiliary line (it has the same Slope)double b = P3 - k * B3;
// Find auxiliary line's price at point T1double AuxP = k * B1 + b;
Diff = P1 - AuxP;
}
We cannot use time as x in f(x) = kx + b calculation because charts are non-linear in respect to time. For example, daily charts have weekends and holidays omitted, so the neighboring bars could be 1 day apart or 3 days apart.
Works miracles :-). If someone needs, here's the code, which calculates vertical difference between main and auxiliary lines of the OBJ_CHANNEL:
Nicely done - a calculation I had on my to-do list!
Thanks for sharing the code!
Helped me a lot!
Thanks!
Hi,
You're broked your neck with this code. Just use the "ObjectGetValueByTime" command.
Ex:
//For the first line
Alert(ObjectGetValueByTime(0,BorderChannel,Time[0],0));
//For the second line
Alert(ObjectGetValueByTime(0,BorderChannel,Time[0],1));
Chris
Thanks, Chris!
I guess this function had not yet been added to MetaTrader 4 when I was creating this workaround.