Slope calculation and determined angle

 

Greetings,

  I am using the following code attempting to calculate the slope of a drawn trendline...however it seems flawed as the values I am getting seem ridiculously small.  Can somebody please verify if my calculations are correct (probably not) and...assuming they are incorrect provide a suggestion on how to correct?


  bool _line_1 = ObjectCreate(_Symbol, "TopOpen_" + _LINE_NUMBERS, OBJ_TREND, 0, priceInformation[_drawBar].time, priceInformation[_drawBar].open, priceInformation[_lookup].time, priceInformation[_lookup].open);

  ObjectSetInteger(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_COLOR, Blue);  //object color
  ObjectSetInteger(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_STYLE, STYLE_SOLID);  //object style
  ObjectSetInteger(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_WIDTH, 1);  //object width
  ObjectSetInteger(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_RAY_RIGHT, true);  //object ray   
   
  bool _line_2 = ObjectCreate(_Symbol, "TopClose_" + _LINE_NUMBERS, OBJ_TREND, 0, priceInformation[_drawBar].time, priceInformation[_drawBar].open, priceInformation[_lookup].time, priceInformation[_lookup].close);

  ObjectSetInteger(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_COLOR, Blue);  //object color
  ObjectSetInteger(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_STYLE, STYLE_SOLID);  //object style
  ObjectSetInteger(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_WIDTH, 1);  //object width
  ObjectSetInteger(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_RAY_RIGHT, true);  //object ray      

    //***GET SLOPES OF THE 2 LINES EXTENDING FROM THE 'TOP' ORIGIN

     // Line 1, Point 1
     double xA1 = (double)ObjectGetInteger(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_TIME, 0);
     double yA1 = ObjectGetDouble(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_PRICE, 0);

     // Line 1, Point 2
     double xA2 = (double)ObjectGetInteger(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_TIME, 1);
     double yA2 = ObjectGetDouble(0, "TopOpen_" + _LINE_NUMBERS, OBJPROP_PRICE, 1);

     // Line 2, Point 1
     double xB1 = (double)ObjectGetInteger(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_TIME, 0);
     double yB1 = ObjectGetDouble(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_PRICE, 0);

     // Line 2, Point 2
     double xB2 = (double)ObjectGetInteger(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_TIME, 1);
     double yB2 = ObjectGetDouble(0, "TopClose_" + _LINE_NUMBERS, OBJPROP_PRICE, 1);

     // Line slope
     double mA = (yA1 - yA2) / (xA1 - xA2);
     double mB = (yB1 - yB2) / (xB1 - xB2);

Print("SLOPE of TopOpen: ", mA);
Print("SLOPE of TopClose: ", mB);

   Basically I figure I should be able to calculate the angle of the drawn lines from the slope...however as I mentioned I do not believe the code posted above is correctly calculating the line slopes.

  For what it is worth I have also tried determining the degree angle of the lines using the following code, however the value I receive seems incorrect.  It seems from reading some other posts this method is discredited...?

     //*****ANGLE OF THE LINES...
      int X2, Y2, X1, Y1;
      ChartTimePriceToXY(0, 0, xA2, yA2, X2, Y2);
      ChartTimePriceToXY(0, 0, xA1, yA1, X1, Y1);
      double angle_in_degrees = MathArctan( (double)(Y2-Y1)/(double)(X1-X2) )*180/M_PI;
      Print("THE ANGLE OF (TopOpen_): ", angle_in_degrees);
     //*****

   Any suggestions appreciated.  I thank you in advance.

 
pangit68: I have also tried determining the degree angle of the lines
Can't be done.
  1. There is no angle from 2 anchor points. An angle requires distance divided by distance; a unit-less number. A chart has price and time. What is the angle of going 30 miles in 45 minutes? Meaningless!

  2. You can get the slope from a trendline: m=ΔPrice÷ΔTime. Changing the price scale, bar width, or window size, changes the apparent angle, the slope is constant. Angle is meaningless.

  3. You can create an angled line using one point and setting the angle (Object Properties - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference.) The line is drawn that angle in pixels. Changing the axes scales does NOT change the angle (and thus is meaningless.)

  4. If you insist, take the two price/time coordinates, convert them to pixel coordinates and then to apparent angle with arctan.
              How to get the angle of a trendline and place a text object to it? - Trend Indicators - MQL4 programming forum - Page 2

 

Don't use time and price to calculate the slope. Find out how many bars are between point 1 and point 2. If point 1 is on bar index X and point 2 is on bar index (X + 20), then SLOPE = (price 2 - price 1) / 20 and it tells you by how much the price changes for every new bar after the 1st point. You can't use time because the market isn't open 24/7. To calculate the price value of any point "X" on the trend line, you can then use (price 1) + SLOPE * (point X index - point 1 index). Like William said, you can't calculate the angle, only the slope.

*edit: I know there's a tool in MT5 called Trendline by angle, but there's no practical use for it that I'm aware of since the angle isn't dependant on your scale. You could use it to draw shapes on your chart, but not to track the price.
 

I thank each of you for your replies.

Mr. Roeder is not my 'second' code block in my original post not equal to your '#4 point' in your message?  After some additional testing it does seem to be giving me at least a fairly accurate angle measurement.

Thank you for your explanation Jeepack...makes sense to me.  However I do believe it is possible to use the slope to calculate the angle...my (limited) testing seems to be accurate.  After obtaining the slope using your suggested method (by taking a difference in price (pips) from the first bar and the second bar divided by # of bars) I am using a process as described here "https://sciencing.com/convert-percentage-slope-degrees-8093249.html" to obtain a value for the angle which seems at least fairly accurate...

Again thanks to both of you for your earlier responses...appreciated.   

How to Convert a Percentage Slope to Degrees
How to Convert a Percentage Slope to Degrees
  • sciencing.com
When you're driving in hilly country, you'll come across road signs alerting you to steep grades ahead, and these are always expressed as percentages. For truckers, a 4 percent downgrade is enough to switch into low gear, and if you're driving an older RV with an underpowered engine, you might think twice about trying to climb a grade steeper...
 
pangit68 #:

I thank each of you for your replies.

Mr. Roeder is not my 'second' code block in my original post not equal to your '#4 point' in your message?  After some additional testing it does seem to be giving me at least a fairly accurate angle measurement.

Thank you for your explanation Jeepack...makes sense to me.  However I do believe it is possible to use the slope to calculate the angle...my (limited) testing seems to be accurate.  After obtaining the slope using your suggested method (by taking a difference in price (pips) from the first bar and the second bar divided by # of bars) I am using a process as described here "https://sciencing.com/convert-percentage-slope-degrees-8093249.html" to obtain a value for the angle which seems at least fairly accurate...

Again thanks to both of you for your earlier responses...appreciated.   

This is incorrect. See highlights.

When I said take the difference in pips between the first point and the second point and divide by the number of bars with [(index of second point) - (index of first point)], the second point is the second point on the trendline. You are extending it with a ray to the right but your trendline still has a second point from which the ray starts. It can be anywhere since you are using a ray, it could be on the next bar, or 20 bars later. But this is the point that you use to calculate the difference in pips. Another way to say this is that if you don't extend a ray to the right, the first point is the start of the trend line and the second point is the end.

You can't use that method and apply it to something where the x and y scales aren't using the same unit of measurement. Mr Roeder is right. This technique works for roads because the unit of the slope on the x and y scale is the same (meters, feet, yards, etc). You can prove this yourself by drawing a trendline with the "trendline by angle" tool in MT5, then draw a regular trendline over it, then simply adjust the scale of your chart and you'll see the two trendlines will now have different angles. That's because the scale on your chart uses price on the y axis and time on the x axis.

Yes, it seems to be related to the same technique. I'm not sure if the code is right but I hope you understand that this tells you abosolutely nothing about how your trendline is constructed because the x and y coordinates in pixels will depend on how you adjust your price scale on the right and how zoomed in you are on the time scale. In other words, simply resizing your chart's window will change the angle.

You could convert the slope to a percentage but if you do that then you're basically drawing a logarithmic trendline and MT5 doesn't have an option for log scales or log trendlines so if you're using the trendline tool to draw a trendline don't use percentages. This is true except if you don't extend a ray to the right. If you only draw a trendline with start point and an end point, you could technically define it with a percentage, but I would still advise against this because if you used that percentage to calculate values of the trendline between the two points, you would get incorrect results (a log trendline would show up as a curve on a non-log price scale). But maybe what you want to know is the "Rate Of Change" between two points, and don't care about the points between those two points or to the right of the second point. If so, then you could simply use an ROC or ROC% indicator, or calculate it yourself.

Maybe if you explained why you wanted to get the angle, it would be easier for us to help you find the correct solution, if one exists. Although you already said you wanted to calculate the slope of a drawn trendline. And what that means is you want to be able to know the formula that allows you to calculate the value of any points on that trendline, including points that either extend to the right of it or to the left of it. And if that's what you really want, the method I first described is the only one that works on a chart where the x axis is "bars" and y axis is "price" and the value you should be looking for would be expressed as "price per bar". There is nothing else you can do in this case, you have to respect the scale's respective units of measurements. But if you have a different goal in mind, feel free to explain.

 

Greetings Jeepack, thank you for the reply.

  I apologize my previous reply was not adequately explained.  I am in fact using the difference in price as you had suggested to create a slope.  In my case the two bars are consecutive, in other words the second bar is immediately following the first.  Therefore, for example:

EURUSD: 

CLOSE PRICES ARE = Abs(1.05933 - 1.05892) = .00041 (41 pip difference in price between the initial bar and the second bar immediately following)

                              Therefore, since there is only 1 bar separation between the two the slope is: .00041 / 1 = .00041   correct?

  If that is the slope I'm not sure how that is of use in determining the angle...however I can verify the code I posted in the 'second' block above in my initial posting does seem to provide an accurate measure of the angle of the line, as verified by taking a screenshot of the chart and measuring using an angle measurement tool in a graphics program.

   I realize that using differing scales on the chart will change the angle, that makes sense.  It should be possible however, to display a chart using only a fixed number of bars (say 80 bars total) and using only the 'default' display (100% scale without zooming in/out) to measure angles...ignoring any differences at other resolutions.  Is there an easy way to 'force' a chart to display a fixed number of bars?

Thanks again for your input.

Reason: