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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
//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" |
Share this
No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.
ProRealTime ITF files and other attachments :PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials
There are actually two ways to calculate standard deviations and I seem to have used the version for calculations on populations rather than on data samples. There is only a tiny difference between them but if you want a modified code then you can find it here; https://www.prorealcode.com/topic/standard-deviation-indicators-minor-fix/