Trendling Angle Calculation

 

I am trying to calculate the angle from a previous bars High/Low to the open of the next bar. Does this code look right?

double getAngleByType(int orderType) {
double changeInY;
double changeInX = 60;
int buyNormalizer = 90;
if (orderType == OP_BUY)
{
changeInY = MathAbs(Low[1] - Open[0]);
}
else if (orderType == OP_SELL)
{
changeInY = MathAbs(High[1] - Open[0]);
buyNormalizer = 0;
}

return (MathAbs( //MathArctan(
buyNormalizer - (MathTan(
changeInY / changeInX)) * (180 / 3.14159) * angleMultiplier));

It seems to me that visually the angle calculated isn't what I was expecting.

Thanks

 

jyett,

Is above me - maths side. But look at this thread which may well benifit you Help to calculate the slope of a line that is Perpendicular to a known line

Also phy gives good coverage in this thread Angled Trendlines

How to get the angle of a moving average?

Longshot maybe Elementary Linear Algebra; Solutions to Elementary Linear Algebra

afaik, Tovan has posted somewhere... (man! I cannot find it now) with link to good maths site). Will look some more but I definitely remember him doing this...

good luck

.

more:

Angle of 2 candles

 
fbj wrote >>

jyett,

Is above me - maths side. But look at this thread which may well benifit you Help to calculate the slope of a line that is Perpendicular to a known line

Also phy gives good coverage in this thread Angled Trendlines

How to get the angle of a moving average?

Longshot maybe Elementary Linear Algebra; Solutions to Elementary Linear Algebra

afaik, Tovan has posted somewhere... (man! I cannot find it now) with link to good maths site). Will look some more but I definitely remember him doing this...

good luck

.

more:

Angle of 2 candles

I will look into it, thanks!

 
jyett77:

I am trying to calculate the angle from a previous bars High/Low to the open of the next bar. Does this code look right?

double getAngleByType(int orderType) {
double changeInY;
double changeInX = 60;
int buyNormalizer = 90;
if (orderType == OP_BUY)
{
changeInY = MathAbs(Low[1] - Open[0]);
}
else if (orderType == OP_SELL)
{
changeInY = MathAbs(High[1] - Open[0]);
buyNormalizer = 0;
}

return (MathAbs( //MathArctan(
buyNormalizer - (MathTan(
changeInY / changeInX)) * (180 / 3.14159) * angleMultiplier));

It seems to me that visually the angle calculated isn't what I was expecting.

Thanks

jyett, I have devoted considerable time in developing a means for calculating angles. First, let me say this --- the 45 degree angle you see on your chart today will not be a 45 degree angle on your chart in a few days. The reason is all computer generated charts use variable scaling of the vertical scale where the price scale is located. They do this so the data takes up the entire screen. Thus, let's say for example that mathmatically, 1 PIP/Bar = 45 degrees. Sometimes that may visually look like a 45 degree angle on your screen, and sometimes it may visually look like a 60 degree angle --- it all depends on the pricing scale for the chart on your screen at that given moment. Now having said that, as you can now see, it is a complicated topic. What really does constitute a 45 degree angle? Well you're going to have to pick a number, and of course in doing so you do not want to be overly arbitrary. Here's what I did:


1) Downloaded the M15 data for EURUSD and opened the file in Excel - that's a quarter-million bars worth of data.

2) Calc'd a 100SMA, and manipulated the data to find how many trends exceeded 100 bars (ie 25 hrs)

3) Calc'd the average rate of change for the middle third of the trend (the part that tends to be the straightest)

4) Sorted those trends exceeding 100+ bars (approximately 1000 trends) by the newly calc'd rate of change for the middle-third, and decided to use the 500th rate of change (ie the one midway thru the sorted data) as the rate of change I would use for 45 degrees. Thus, for the M15 chart, I use 0.742 PIPs per Bar as the standard for a 45 degree angle.


It appears that you are trying to calc the angle from one bar to the next. I would avoid doing that. I am currently using a 5-bar average for calculating the angle, and even that is a bit jumpy (and that's a 5-bar average on the 100SMA line). I will most likely go to a 10-bar average as it produces a more consistent result.


Mathematically, the calculation is a follows:


If the current rate of change for the 5-bar average is 0.876 PIPs per bar, then the angle is:


Arctan(0.876/0.742)=49.7 degrees


An additional cautionary note: In periods of high volatility such as we have experienced in the last 6-9 months, you will get a lot of big angles, if you go to a period low volatility say 2006, the angles will not be as large. Therefore, volatility has a large impact on the angles that get calculated. So what works this year if you set some minimum threshold angle in your EA may not work next year.


Best regards,

 
FXtrader2008 wrote >>

jyett, I have devoted considerable time in developing a means for calculating angles. First, let me say this --- the 45 degree angle you see on your chart today will not be a 45 degree angle on your chart in a few days. The reason is all computer generated charts use variable scaling of the vertical scale where the price scale is located. They do this so the data takes up the entire screen. Thus, let's say for example that mathmatically, 1 PIP/Bar = 45 degrees. Sometimes that may visually look like a 45 degree angle on your screen, and sometimes it may visually look like a 60 degree angle --- it all depends on the pricing scale for the chart on your screen at that given moment. Now having said that, as you can now see, it is a complicated topic. What really does constitute a 45 degree angle? Well you're going to have to pick a number, and of course in doing so you do not want to be overly arbitrary. Here's what I did:

1) Downloaded the M15 data for EURUSD and opened the file in Excel - that's a quarter-million bars worth of data.

2) Calc'd a 100SMA, and manipulated the data to find how many trends exceeded 100 bars (ie 25 hrs)

3) Calc'd the average rate of change for the middle third of the trend (the part that tends to be the straightest)

4) Sorted those trends exceeding 100+ bars (approximately 1000 trends) by the newly calc'd rate of change for the middle-third, and decided to use the 500th rate of change (ie the one midway thru the sorted data) as the rate of change I would use for 45 degrees. Thus, for the M15 chart, I use 0.742 PIPs per Bar as the standard for a 45 degree angle.

It appears that you are trying to calc the angle from one bar to the next. I would avoid doing that. I am currently using a 5-bar average for calculating the angle, and even that is a bit jumpy (and that's a 5-bar average on the 100SMA line). I will most likely go to a 10-bar average as it produces a more consistent result.

Mathematically, the calculation is a follows:

If the current rate of change for the 5-bar average is 0.876 PIPs per bar, then the angle is:

Arctan(0.876/0.742)=49.7 degrees

An additional cautionary note: In periods of high volatility such as we have experienced in the last 6-9 months, you will get a lot of big angles, if you go to a period low volatility say 2006, the angles will not be as large. Therefore, volatility has a large impact on the angles that get calculated. So what works this year if you set some minimum threshold angle in your EA may not work next year.

Best regards,

Thanks FXtrader2008! This at least gives me a base line to look into. I completely agree that the angle on the screen DOES NOT LOOK like what we were calculating. I am going to try your method and see how I might be able to fit it into my stategy. The reason I am using it from previous bar to current bar is because it is designed as a filter rather then a signal generator. It really was suppose to be able help filter out bad positions. Once again thanks, I will try to report results if i find it works for my program.

 

After reading the entries here I am wondering if there is a way to calculate the absolute angle (the actual Line in line charts in my case) and use it in further calculations regardless of the scaling of the chart, the monitor aspect ratio etc?

 
Huck Finn #:

jyett, I have devoted considerable time in developing a means for calculating angles. First, let me say this --- the 45 degree angle you see on your chart today will not be a 45 degree angle on your chart in a few days. The reason is all computer generated charts use variable scaling of the vertical scale where the price scale is located. They do this so the data takes up the entire screen. Thus, let's say for example that mathmatically, 1 PIP/Bar = 45 degrees. Sometimes that may visually look like a 45 degree angle on your screen, and sometimes it may visually look like a 60 degree angle --- it all depends on the pricing scale for the chart on your screen at that given moment. Now having said that, as you can now see, it is a complicated topic. What really does constitute a 45 degree angle? Well you're going to have to pick a number, and of course in doing so you do not want to be overly arbitrary. Here's what I did:


1) Downloaded the M15 data for EURUSD and opened the file in Excel - that's a quarter-million bars worth of data.

2) Calc'd a 100SMA, and manipulated the data to find how many trends exceeded 100 bars (ie 25 hrs)

3) Calc'd the average rate of change for the middle third of the trend (the part that tends to be the straightest)

4) Sorted those trends exceeding 100+ bars (approximately 1000 trends) by the newly calc'd rate of change for the middle-third, and decided to use the 500th rate of change (ie the one midway thru the sorted data) as the rate of change I would use for 45 degrees. Thus, for the M15 chart, I use 0.742 PIPs per Bar as the standard for a 45 degree angle.


It appears that you are trying to calc the angle from one bar to the next. I would avoid doing that. I am currently using a 5-bar average for calculating the angle, and even that is a bit jumpy (and that's a 5-bar average on the 100SMA line). I will most likely go to a 10-bar average as it produces a more consistent result.


Mathematically, the calculation is a follows:


If the current rate of change for the 5-bar average is 0.876 PIPs per bar, then the angle is:


Arctan(0.876/0.742)=49.7 degrees


An additional cautionary note: In periods of high volatility such as we have experienced in the last 6-9 months, you will get a lot of big angles, if you go to a period low volatility say 2006, the angles will not be as large. Therefore, volatility has a large impact on the angles that get calculated. So what works this year if you set some minimum threshold angle in your EA may not work next year.


Best regards,

double read_angle(double price1,long time1,double price2,long time2)
  {
   double result=0;

   int x1=0,x2=0;
   int y1=0,y2=0;
   long chart = ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);

   ChartTimePriceToXY(0,0,time1,price1,x1,y1);
   ChartTimePriceToXY(0,0,time2,price2,x2,y2);
   int size = (int)chart;
   y1=size-y1;
   y2=size-y2;
   double angle =NormalizeDouble((MathArctan(((double)y2-(double)y1)/((double)x2-(double)x1))*180)/M_PI,1);
   result=angle;
   return(result);
  }
 
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