OPEN-SOURCE SCRIPT

Square Root Moving Average

Updated
Abstract
This script computes moving averages which the weighting of the recent quarter takes up about a half weight.
This script also provides their upper bands and lower bands.
You can apply moving average or band strategies with this script.

Introduction
Moving average is a popular indicator which can eliminate market noise and observe trend.
There are several moving average related strategies used by many traders.
The first one is trade when the price is far from moving average.
To measure if the price is far from moving average, traders may need a lower band and an upper band.
Bollinger bands use standard derivation and Keltner channels use average true range.
In up trend, moving average and lower band can be support.
In ranging market, lower band can be support and upper band can be resistance.
In down trend, moving average and upper band can be resistance.
An another group of moving average strategy is comparing short term moving average and long term moving average.
Moving average cross, Awesome oscillators and MACD belong to this group.
The period and weightings of moving averages are also topics.
Period, as known as length, means how many days are computed by moving averages.
Weighting means how much weight the price of a day takes up in moving averages.
For simple moving averages, the weightings of each day are equal.
For most of non-simple moving averages, the weightings of more recent days are higher than the weightings of less recent days.
Many trading courses say the concept of trading strategies is more important than the settings of moving averages.
However, we can observe some characteristics of price movement to design the weightings of moving averages and make them more meaningful.
In this research, we use the observation that when there are no significant events, when the time frame becomes 4 times, the average true range becomes about 2 times.
For example, the average true range in 4-hour chart is about 2 times of the average true range in 1-hour chart; the average true range in 1-hour chart is about 2 times of the average true range in 15-minute chart.
Therefore, the goal of design is making the weighting of the most recent quarter is close to the weighting of the rest recent three quarters.
For example, for the 24-day moving average, the weighting of the most recent 6 days is close to the weighting of the rest 18 days.

Computing the weighting
The formula of moving average is
sum ( price of day n * weighting of day n ) / sum ( weighting of day n )
Day 1 is the most recent day and day k+1 is the day before day k.
For more convenient explanation, we don't expect sum ( weighting of day n ) is equal to 1.
To make the weighting of the most recent quarter is close to the weighting of the rest recent three quarters, we have
sum ( weighting of day 4n ) = 2 * sum ( weighting of day n )
If when weighting of day 1 is 1, we have
sum ( weighting of day n ) = sqrt ( n )
weighting of day n = sqrt ( n ) - sqrt ( n-1 )
weighting of day 2 ≒ 1.414 - 1.000 = 0.414
weighting of day 3 ≒ 1.732 - 1.414 = 0.318
weighting of day 4 ≒ 2.000 - 1.732 = 0.268
If we follow this formula, the weighting of day 1 is too strong and the moving average may be not stable.
To reduce the weighting of day 1 and keep the spirit of the formula, we can add a parameter (we call it as x_1w2b).
The formula becomes
weighting of day n = sqrt ( n+x_1w2b ) - sqrt ( n-1+x_1w2b )
if x_1w2b is 0.25, then we have
weighting of day 1 = sqrt(1.25) - sqrt(0.25) ≒ 1.1 - 0.5 = 0.6
weighting of day 2 = sqrt(2.25) - sqrt(1.25) ≒ 1.5 - 1.1 = 0.4
weighting of day 3 = sqrt(3.25) - sqrt(2.25) ≒ 1.8 - 1.5 = 0.3
weighting of day 4 = sqrt(4.25) - sqrt(3.25) ≒ 2.06 - 1.8 = 0.26
weighting of day 5 = sqrt(5.25) - sqrt(4.25) ≒ 2.3 - 2.06 = 0.24
weighting of day 6 = sqrt(6.25) - sqrt(5.25) ≒ 2.5 - 2.3 = 0.2
weighting of day 7 = sqrt(7.25) - sqrt(6.25) ≒ 2.7 - 2.5 = 0.2

What you see and can adjust in this script
This script plots three moving averages described above.
The short term one is default magenta, 6 days and 1 atr.
The middle term one is default yellow, 24 days and 2 atr.
The long term one is default green, 96 days and 4 atr.
I arrange the short term 6 days to make it close to sma(5).
The other twos are arranged according to 4x length and 2x atr.
There are 9 curves plotted by this script. I made the lower bands and the upper bands less clear than moving averages so it is less possible misrecognizing lower or upper bands as moving averages.
x_src : how to compute the reference price of a day, using 1 to 4 of open, high, low and close.
len : how many days are computed by moving averages
atr : how many days are computed by average true range
multi : the distance from the moving average to the lower band and the distance from the moving average to the lower band are equal to multi * average true range.
x_1w2b : adjust this number to avoid the weighting of day 1 from being too strong.

Conclusion
There are moving averages which the weighting of the most recent quarter is close to the weighting of the rest recent three quarters.
We can apply strategies based on moving averages. Like most of indicators, oversold does not always means it is an opportunity to buy.
If the short term lower band is close to the middle term moving average or the middle term lower band is close to the long term moving average, it may be potential support value.

References
Computing FIR Filters Using Arrays [WMA Example]
How to trade with moving averages : the eight trading signals concluded by Granville
How to trade with Bollinger bands
How to trade with double Bollinger bands
Release Notes
Rename moving average result y1,y2,y3 to yy1,yy2,yy3 .
There are no := on yy1,yy2,yy3 .
I hope security (access different timeframe) can accept it.
Release Notes
In this update, I add an optional parameter , x_is exp.
When we use it , the script compute values according to exponential and logarithm to fit in high volatile price movements.
bandsdynamicresistancedynamicsupportdynamicsupportandresistanceKeltner Channels (KC)Moving Averagessqrtsquarerootupperband

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer