How to calculate angle of a moving average

 

Hi friends,

I need to know how to get angle of a moving average. after I get the angles, then I want to calculate tangent of the angle and compare the value with a specific number. I want to implement  these method with mql4 to predict an up-trend.

just give me a tip how to get the angle of an up-trend moving average.


up-trend MA

 
Normally what I do is just to deduct the moving average of the previous bar from the moving average of one bar. The steeper the slope, the greater is the difference.
 
Martin Moreno:

Hi friends,

I need to know how to get angle of a moving average. after I get the angles, then I want to calculate tangent of the angle and compare the value with a specific number. I want to implement  these method with mql4 to predict an up-trend.

just give me a tip how to get the angle of an up-trend moving average.



There were many suggestions on this topic before, each for its own use case.

https://www.mql5.com/en/search#!keyword=angle&module=mql5_module_codebase

https://www.mql5.com/en/search#!keyword=angle&module=mql5_module_market

Search - MQL5.community
Search - MQL5.community
  • www.mql5.com
Searching is based on morphology and is insensitive to case. All letters, no matter of their case, will be processed as lowercase. By default, our search engine shows pages, that...
 
Martin Moreno: how to get angle
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 axes scales changes the apparent angle, the slope is constant. Angle is meaningless.

  3. You can create a 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
 
William Roeder:
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 axes scales changes the apparent angle, the slope is constant. Angle is meaningless.

  3. You can create a 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

Thanks a lot William

 
Martin Moreno:

Thanks a lot William

but be careful, to have good results, the best is to use a fixed deltaTime (for instance 1 Bar).


for that you will need to save the prices to a buffer for time and a buffer for price. Because if you use previous bar value, it will not be accurate, because you will be inside the latest bar, but the delta time can be very small (at the beginning of the bar) thus leading to bad values


then at ANY time, you can calculate the correct slope from 1 bar away or so

 
I have already wrote the scripts to detect angles on chart. But it is changing while changing the chart scale
 

There's a lot of comments stating this "can't be done".

Yes, of course, a true angle needs similar units on both sides of the equation. But generally what one wants to know is whether a rise/fall in one part of a chart is greater/lesser than a rise/fall in another part.

This can be hard to judge without a fixed scale and/or when switching between time frames.

But here's a way to do it using high-school math:

First, calculating the angular relationship between price and time:

//--- calculate the angle, (Soh,Cah,Toa) Tan @=opposite/adjacent, the variable "i" is the current bar
double adjacent=sqrt((int)time[i]-(int)time[i-1]);
double opposite=(mabuffer[i]-mabuffer[i-1])/_Point;
double angle=MathArctan((double)opposite/(double)adjacent) * 180/M_PI;

Or, between price and volume:

//--- calculate the angle, (Soh,Cah,Toa) Tan @=opposite/adjacent, the variable "i" is the current bar
double adjacent=sqrt(volumeBuffer[i]);
double opposite=(mabuffer[i]-mabuffer[i-1])/_Point;
double angle=MathArctan((double)opposite/(double)adjacent) * 180/M_PI;


One other quick point - forex prices really don't move much - rarely more than a couple of percent a day, so the "angles" price makes against time aren't that large. I amplify the signal by applying a square root to the y axis, which makes apparent differences between shallow and steep movements more obvious:

sqrt((int)time[i]-(int)time[i-1])

Hope that all helps. Attached image of the "trend angle" derived with the above method for a 13 period simple ma.

Files:
GBPUSDH1.png  40 kb
 
janiwed718 #:

There's a lot of comments stating this "can't be done".

Yes, of course, a true angle needs similar units on both sides of the equation. But generally what one wants to know is whether a rise/fall in one part of a chart is greater/lesser than a rise/fall in another part.

This can be hard to judge without a fixed scale and/or when switching between time frames.

But here's a way to do it using high-school math:

First, calculating the angular relationship between price and time:

Or, between price and volume:


One other quick point - forex prices really don't move much - rarely more than a couple of percent a day, so the "angles" price makes against time aren't that large. I amplify the signal by applying a square root to the y axis, which makes apparent differences between shallow and steep movements more obvious:

Hope that all helps. Attached image of the "trend angle" derived with the above method for a 13 period simple ma.


Dude, this is an old post, but your answer saved my algorithm. I was forced to respond just to thank you.

This calculation is simply perfect. Awesome. Thank you very much for your contribution.

 
@Vampre #: Dude, this is an old post, but your answer saved my algorithm. I was forced to respond just to thank you. This calculation is simply perfect. Awesome. Thank you very much for your contribution.

Don't be fooled. That maths is invalid. You can't calculate an "angle", not even in the way proposed.

You can only calculate a slope. Use that instead. The maths for the slope is much simpler and much more efficient than trying to use the proposed pseudo maths for a false "angle".

 

This is how I find the slope:

iSlope30 = (iMA(_Symbol, timePeriodInMin, 30, 0, MODE_EMA, PRICE_MEDIAN, 0) - iMA(_Symbol, timePeriodInMin, 30, 0, MODE_EMA, PRICE_MEDIAN, 1))/Point;


Works for me, hope it helps someone.