Need Help calculating angle between two lines on MT4

 

Hello everyone,

Am trying to calculate the angle between two lines placed on the MT4 chart and so far I've been getting a value very close to zero. 

My approach is this, get the slope of the two lines then use this formular to get the angle.

tan# = fabs((m2 - m1)/(1+(m2m1))). 

Then I get the angle # by saying, # = arctan(fabs((m2 - m1)/(1+(m2m1)))).

where m2 = second slope, m1 = first slope.

Here's a snippet of the code

  double trendslope, baseslope,angle;
  
  trendslope = (trendend - originpoint) / (Time[bar] - Time[originbar]);
  baseslope  = (baseend - originpoint) / (Time[bar] - Time[originbar]);
  angle = fabs((trendslope - baseslope) / (1 + (trendslope * baseslope)));
  angle = RadToDegrees(atan(angle));
  return(angle);

here m2 is represented by trendslope and m1 is represented by baseslope.

The two lines originate from a common point = (originpoint,Time[originbar]);

so for the trendline I have (originpoint, Time[originbar]) (trendend, Time[bar]);

base line is (originpoint, Time[originbar]) (baseene, Time[bar])


Here's the code for the function RadToDegrees()

double RadToDegrees(double rad)
  {
   double degree=rad*180/M_PI;
   return(degree);
  }

Also worth noting is that the lines are drawn correctly on the MT4 chart, so all values are accurate

The two lines on chart


I need to know, why my formular is returning close to zero values

Any help will be appreciated

 
Hello
 
First thing to note - what are the units you use for the axes? If you want to get angle defined by price in points per second, then you should divide prices by POINT size.
 
holocast:

I need to know, why my formular is returning close to zero values

Any help will be appreciated

Because a slope is not an angle. Whatever the units, even if you finally get an "angle" value that satisfy you, it will be meaning less.

 
Stanislav Korotky:
First thing to note - what are the units you use for the axes? If you want to get angle defined by price in points per second, then you should divide prices by POINT size.

Thanks for the response. I followed your above recommendation and instead of the usual near zero values I used to have, for example 6.3876789E-11, this time around am getting 0.35.

This is good but I believe you'll agree with me that 0.35 is still not the angle between those two lines.

 
Alain Verleyen:

Because a slope is not an angle. Whatever the units, even if you finally get an "angle" value that satisfy you, it will be meaning less.

Am using the formular that relates the slope of two lines to the acute angle they make when in contact. I have used this formular in several real life instances and it always worked out.

My intuition is telling me I have messed up the units somehow and as such am getting baseless values. Don't get me wrong, am not arguing that my approach is the "almighty" method, but I want to believe there must be a method to calculate the angle between two trendlines in mql4, since there's a function to create trendlines based on angle.

So if this approach is not the correct one, then could you kindly tell me what approach I should take?

Thanks

 
holocast:

Am using the formular that relates the slope of two lines to the acute angle they make when in contact. I have used this formular in several real life instances and it always worked out.

My intuition is telling me I have messed up the units somehow and as such am getting baseless values. Don't get me wrong, am not arguing that my approach is the "almighty" method, but I want to believe there must be a method to calculate the angle between two trendlines in mql4, since there's a function to create trendlines based on angle.

So if this approach is not the correct one, then could you kindly tell me what approach I should take?

Thanks

Not sure what is "real life instances" ?

Working with slopes is a lot more simple and universal. Do you really needs angle ?

What I mean is the following : let's take a chart, draw the trenlines like you did and measure the visible angle. You do it on your side, I do it on mine. Supposing we draw the exact same trendlines (prices/times match), the most probable is we will never get the same angle. We have different screen resolution, different zoom, different scale, etc... For this angle to be meaningful we would have to find and agree on all these settings. A computer screen and an electronic chart window is not a paper chart.

If you still want to work with angle, you have to use the same unit for X and Y axis...what is an angle calculated from a slope using time (seconds, minutes, hours...) and price ? You need to use centimeters (probably not a good idea), or maybe pixel...or whatever unit which will make sense for both X and Y axis.

All of this being said, you can work with angle if it fits you needs and preferences, but you have to understand and deal very well with the above points to not mislead yourself.

 
holocast:

Thanks for the response. I followed your above recommendation and instead of the usual near zero values I used to have, for example 6.3876789E-11, this time around am getting 0.35.

This is good but I believe you'll agree with me that 0.35 is still not the angle between those two lines.

Not at all - this _IS_ the angle in your anisotropic space defined by currently selected units.

 
holocast:

Am using the formular that relates the slope of two lines to the acute angle they make when in contact. I have used this formular in several real life instances and it always worked out.

My intuition is telling me I have messed up the units somehow and as such am getting baseless values. Don't get me wrong, am not arguing that my approach is the "almighty" method, but I want to believe there must be a method to calculate the angle between two trendlines in mql4, since there's a function to create trendlines based on angle.

So if this approach is not the correct one, then could you kindly tell me what approach I should take?

Trendlines based on angle uses pixels and make sense only on fixed scale 1:1. There was a discussion on russian forum:

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Общайтесь с разработчиками через Сервисдеск!

Slava, 2017.08.09 10:22

The calculation of the trend line on the corner .

The coordinates of the first point x1, y1 are known. The x2 coordinate of the second point is known. Coordinates are in pixels. Known angle in degrees angle, the angle is converted to radians rangle 

y2 = y1 - (x2 - x1) * tan (rangle) 



 
Stanislav Korotky:

Trendlines based on angle uses pixels and make sense only on fixed scale 1:1. There was a discussion on russian forum:

Thanks for the links.

Even pixels can be different on my computer and on yours, so I am really skeptical about these angles strategies. I am missing something ?

 
Alain Verleyen:

Thanks for the links.

Even pixels can be different on my computer and on yours, so I am really skeptical about these angles strategies. I am missing something ?

Yes, visual representation may differ, but algorithmically X and Y are measured in the same units - pixels, and their "squareness" is out of consideration.

Reason: