problem about cross of two trend lines

 

Hello all.

it's me again :((

is anybody there to help me how can i calculate time and price of two crossed trend lines?

i have a code for it but it has some worng.


i want it be exact with any pips difference...

thank you

 
#property copyright ""
#property link      ""
//---Header Files-----------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 1

datetime X1 = D'2010.07.15 16:00';
double   Y1 = 1.332603;
datetime X2 = D'2010.09.10 16:00';
double   Y2 = 1.227437;
datetime X3 = D'2010.07.09 08:00';
double   Y3 = 1.22197;
datetime X4 = D'2010.09.08 00:00';
double   Y4 = 1.33196;

//--------------------------------------------------------------------
int init()
{
   return(0);
}
//---------------------------------------------------------
int deinit()
{
   return(0);
}
//---------------------------------------------------------
int start()
{
   Comment( TimeToStr(LinesCrossTime(X1,Y1,X2,Y2,X3,Y3,X4,Y4)) + " , " + DoubleToStr(LinesCrossLevel(X1,Y1,X2,Y2,X3,Y3,X4,Y4),5) );
   
   return(0);
}
//---------------------------------------------------------
datetime LinesCrossTime(datetime X1, double Y1, datetime X2, double Y2, datetime X3, double Y3, datetime X4, double Y4) 
{
   double M1 = (Y2-Y1)/(X2-X1);
   double M3 = (Y4-Y3)/(X4-X3);
   
   return ((M1*X1-M3*X3+Y3-Y1)/(M1-M3));
}
//--------------------------------------------------------
double LinesCrossLevel(datetime X1, double Y1, datetime X2, double Y2, datetime X3, double Y3, datetime X4, double Y4) 
{
   double M1 = (Y2-Y1)/(X2-X1);
   double M3 = (Y4-Y3)/(X4-X3);
   
   double crossPoint = (M1*X1-M3*X3+Y3-Y1)/(M1-M3);
   
   int i =0;
   while (Time[i]>crossPoint)
      i++;
   return(Close[i-1]);   
}  
 

A trend line is a linear function, y = mx + c, so with 2 trend lines you have 2 linear functions so all you need to do is solve the 2 equations when y and x are the same for both equations. First you need the m and c values for each line, c is the offset of each line at the same time coordinate, and m is the gradient/slope. Once you have your 2 equations you use substitution and you substitute for y in one equation by using the 2nd,

e.g. y = 0.8x + 1.2 & y = 1.5x + 2.2 so 0.8x + 1.2 = 1.5x + 2.2 where the lines cross and solving this gives you x coordinate.

Not sure what happens when you have a weekend in the mix . . .

 
RaptorUK:

A trend line is a linear function, y = mx + c, so with 2 trend lines you have 2 linear functions so all you need to do is solve the 2 equations when y and x are the same for both equations. First you need the m and c values for each line, c is the offset of each line at the same time coordinate, and m is the gradient/slope. Once you have your 2 equations you use substitution and you substitute for y in one equation by using the 2nd,

e.g. y = 0.8x + 1.2 & y = 1.5x + 2.2 so 0.8x + 1.2 = 1.5x + 2.2 where the lines cross and solving this gives you x coordinate.

Not sure what happens when you have a weekend in the mix . . .


at first excuse for my english.

thanks dear RaptorUK for reply.

i know that and my problem is something else.

when i change timeframe to other timeframe or zoom in or zoom out the value also changes.

and my main problem is price no time.

i need my func returns the price of crossing without any wrong.

can you help me too about this:

if i have the corner of lines and one of price from each line can i do that?

and if yes how?

thanks

best regards

 

maniac1984:

when i change timeframe to other timeframe or zoom in or zoom out the value also changes.

and my main problem is price no time.

  1. Solve the two equations like Raptor said. The result is one x and one y. Y is price.
  2. if you change timeframes and your code generates different trendlines, of course the value will change.
  3. if you zoom in or our and your code generates different trendlines, your code is broken.
 
WHRoeder:
  1. Solve the two equations like Raptor said. The result is one x and one y. Y is price.
  2. if you change timeframes and your code generates different trendlines, of course the value will change.
  3. if you zoom in or our and your code generates different trendlines, your code is broken.

thanks dear WHRoeder

i will check it.

Reason: