PineScript: Calculate the slope intensity (angle) of a moving average

167 Views Asked by At

I am fairly new to PineScript and am playing around with some indicators. For this task I am building out a 9/30 MA indicator. What I am trying to achieve is calculating the validity of a trend by calculating the slope intensity of the slower WMA (same calculation should work for the faster EMA as well). A steeper slope could more accurately identify a trend than a flat MA.

What I have so far:

EMAValue = input(9, "EMA")
WMAValue = input(30, "WMA")

ema = ta.ema(close, EMAValue)
wma = ta.wma(close, WMAValue)

EMACrossOver = ta.crossover(ema, wma)
EMACrossUnder = ta.crossunder(ema, wma)

// Plot Crosover and Crossunder
plotshape(EMACrossOver, style = shape.triangleup, location = location.belowbar, color = color.green)
plotshape(EMACrossUnder, style = shape.triangledown, location = location.abovebar, color = color.red)

// Plot Moving Averages
EMAPlot = plot(ema, color = color.lime, linewidth = 1)
WMAPlot = plot(wma, color = color.blue, linewidth = 2)
bgColour = ema > wma ? color.new(color.green, 70) : color.new(color.red, 70)
fill(plot1=EMAPlot, plot2=WMAPlot, color=bgColour)

The above code does the following:

  • Plots the 9 EMA and the 30 WMA
  • Plots the crossover and crossunder
  • Add background color of spread between moving averages.

In the images below we can focus on the blue WMA (slower MA)

Here is an example a strong trend with a steep slope. enter image description here

Here is an example of a flat slope. enter image description here

Looking at the images above how can I indicate that the slope is for example at an angle of n-degrees?

0

There are 0 best solutions below