Calculating the Slope of a Moving Average in Degrees

17 Nov 2018
0 comment
0 attachment

This code snippet demonstrates how to calculate the slope of an Exponential Moving Average (EMA) in terms of angles, which are approximated by price/time ratios. The result is visualized using a histogram where the color changes based on the angle’s value.


EMA=average[20](close)
a=30
b=-30
HH = EMA-EMA[p]
X = HH / p
Ang = ATAN(X)
if ang>a then
  r=0
  g=255
endif
if angb then
  r=0
  g=0
endif
RETURN ang coloured(r,g,0) style(histogram) as "Angle",0 AS "0",a as "ligne",b as"ligne2"

Explanation of the Code:

  • EMA Calculation: The code starts by calculating the Exponential Moving Average (EMA) of the closing prices over the last 20 periods.
  • Angle Calculation: It then computes the difference between the current EMA and the previous period’s EMA (EMA[p]). This difference is divided by the period ‘p’ to normalize the slope over time, resulting in ‘X’.
  • ATAN Function: The ATAN (arctangent) function is used to convert the slope ‘X’ into an angle ‘Ang’. This angle is in radians.
  • Conditional Coloring: The angle is then evaluated against two thresholds ‘a’ and ‘b’. If ‘Ang’ is greater than ‘a’ (30 degrees), the histogram bar is colored green. If ‘Ang’ is less than ‘b’ (-30 degrees), the bar is colored red. If ‘Ang’ is between ‘a’ and ‘b’, the bar remains uncolored (black).
  • Output: Finally, the angle ‘Ang’ is returned and displayed as a histogram. The histogram is styled with colors defined by the conditions, and additional lines are drawn at ‘a’ and ‘b’ to indicate these thresholds.

This approach provides a visual method to quickly assess the direction and steepness of the moving average’s change, which can be particularly useful for identifying trends in data series.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/calcul-de-pente/#post-165837

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
hitman045 Average
Operating in the shadows, I hack problems one by one. My bio is currently encrypted by a complex algorithm. Decryption underway...
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...