Get Values of Channel's Second Line - page 2

To add comments, please log in or register
Simon Gniadkowski
17591
Simon Gniadkowski  
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 . . . 
Andriy Moraru
1065
Andriy Moraru  
Hmmm... Calculating negative bar shift for Time3 could be possible if time is linear in negative shift area. Thanks for the idea!
Andriy Moraru
1065
Andriy Moraru  

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 cases
if (T3 == T1) // Auxiliary point with the same time as the first point of the main line
{
   Diff = P1 - P3;
}
else if (T3 == T2)  // Auxiliary point with the same time as the second point of the main line
{
   Diff = P2 - P3;
}
// Difficult case
else
{
   // Use linear function to calculate auxiliary price at T1
   
   // Find bar numbers of T1 and T2
   // double because will be used in division
   double 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;
         }
   }
   
   // Slope
   double 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 T1
   double AuxP = k * B1 + b;
   
   Diff = P1 - AuxP;
}
noonehastherighttojudgeanother
235
noonehastherighttojudgeanother  

Nicely done - a calculation I had on my to-do list!

einhorn_kiel
13
einhorn_kiel  

Thanks for sharing the code!

Helped me a lot!

Thanks! 

Christian Ricard
137
Christian Ricard  

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

Andriy Moraru
1065
Andriy Moraru  

Thanks, Chris!

I guess this function had not yet been added to MetaTrader 4 when I was creating this workaround.

12
To add comments, please log in or register