This indicator calculates the simple moving average and standard deviation bands (Bollinger Bands) of fractals.
This indicator only works on PRTv11 onwards.
Set ‘p’ to the period you wish to use in the calculations.
Set ‘Deviations’ to the multiple of standard deviations away from the average you want the bands to be.
The type of fractal used in the calculation can be changed by adjusting the ‘BarsBefore’ and ‘Bars After’ settings. For example if BarsBefore = 2 and BarsAfter = 1 then the swing high fractals would have a high with two bars preceding it with lower highs and one bar after it with a lower high and the swing low fractals would have a low with two bars preceding it with higher lows and one bar after it with a higher low.
The ‘HighandLow’ setting allows you to either display one average line that is an average of the last ‘p’ high and low fractals and standard deviation bands calculated from this or to display two average lines – one for the average of only high fractals and the other the average of only low fractals. The upper band is then calculated off the high fractal average and the lower band off the low fractal average. If ‘HighandLow’ is selected you get the latter high and low separate calculations.
As always I advise downloading and importing to get full functionality.
//Fractal STD bands v2
//By Vonasi
//Date: 20200420
//p = 20 //Period for calculation of SMA
//deviations = 2 //Number of deviations.
//BarsBefore = 2 //Bars before high or low that are have a lower high or higher low for fractal definition
//BarsAfter = 2 //Bars after high or low that are have a lower high or higher low for fractal definition
//HihandLow = 1 //Calculate separate averges and separate upper and lower bands for high fractals and low fractals
once upper = undefined
once lower = undefined
once avg = undefined
once lavg = undefined
once havg = undefined
p = max(p,1)
deviations = max(deviations,0)
BarsBefore = max(BarsBefore,1)
BarsAfter = max(BarsAfter,1)
if barindex >= barsbefore + barsafter then
if not highandlow then
BarLookBack = BarsAfter + 1
if low[BarsAfter] < lowest[BarsBefore](low)[BarLookBack] THEN
if low[BarsAfter] = lowest[BarLookBack](low) THEN
a = a + 1
$price[a] = low[barsafter]
endif
endif
if high[BarsAfter] > highest[BarsBefore](high)[BarLookBack] THEN
if high[BarsAfter] = highest[BarLookBack](high) THEN
a = a + 1
$price[a] = high[barsafter]
endif
endif
if a >= p then
total = 0
for b = a downto a-p+1
total = total + $price[b]
next
avg = total/p
total = 0
for b = a downto a-p+1
total = total + (square($price[b]-avg))
next
stdev = sqrt(total/p)
upper = avg + (stdev*deviations)
lower = avg - (stdev*deviations)
endif
endif
if highandlow then
BarLookBack = BarsAfter + 1
if low[BarsAfter] < lowest[BarsBefore](low)[BarLookBack] THEN
if low[BarsAfter] = lowest[BarLookBack](low) THEN
a = a + 1
$lprice[a] = low[barsafter]
endif
endif
if high[BarsAfter] > highest[BarsBefore](high)[BarLookBack] THEN
if high[BarsAfter] = highest[BarLookBack](high) THEN
c = c + 1
$hprice[c] = high[barsafter]
endif
endif
if a >= p then
total = 0
for b = a downto a-p+1
total = total + $lprice[b]
next
lavg = total/p
total = 0
for b = a downto a-p+1
total = total + (square($lprice[b]-lavg))
next
stdev = sqrt(total/p)
lower = lavg - (stdev*deviations)
endif
if c >= p then
total = 0
for b = c downto c-p+1
total = total + $hprice[b]
next
havg = total/p
total = 0
for b = c downto c-p+1
total = total + (square($hprice[b]-havg))
next
stdev = sqrt(total/p)
upper = havg + (stdev*deviations)
endif
endif
endif
return upper as "Upper Band ", lower as "Lower Band", avg as "average", havg as "High Fractal Average", lavg as "Low Fractal Average"