Calculate the difference between an oscillator's moving average of pivot highs and the moving average of pivot lows

33 Views Asked by At

I have an oscillator (trend). I am calculating the moving averages of the pivot highs and the pivot lows. That works. What doesn't work is when I want to calculate the difference between the two.

One issue is that the moving average isn't known for the pivot low at the time of the last pivot high, and vice versa. In the image below you can see the moving averages of pivot highs (red) and pivot lows (green).

Intended output. Let's say the last known values of the moving averages are +0.8 and -1.0. I want to know what the difference between the two is. In this example = 1.8.

len_high = input.int(12)
len_low = input.int(12)
float pivotHigh = ta.pivothigh(trend, len_high, len_high)
float pivotLow = ta.pivotlow(trend, len_low, len_low)
avg_pvt_high = ta.ema(pivotHigh,10)
avg_pvt_llow = ta.ema(pivotLow,10)
plot(avg_pvt_high, color = color.rgb(208, 25, 62))
plot(avg_pvt_llow, color = color.green)

var float piv_diff = 0
piv_diff := na(avg_pvt_high) or na(avg_pvt_llow) ? 0 : math.abs(avg_pvt_high)+math.abs(avg_pvt_llow)
if barstate.islast
    label.new(bar_index+2, trend, str.tostring(piv_diff, "#.00"), style = label.style_none)
0

There are 0 best solutions below