I already estimated the least-squares moving average numerous times, one of the most elegant ways was by rescaling a linear function to the price by using the z-score, today i will propose a new smoother (FLSMA) based on the line rescaling approach and the inverse of a scaled moving average error with the goal to provide an alternative least-squares smoother, the indicator won't use the and will try to adresses problems such as overshoots and lag reduction.
Line Rescaling Method
For those who did not see my estimation using the line rescaling method here is a resume, we want to fit a polynomial function of degree 1 to the price by reducing the sum of squares between the price and the filter, squares is a term meaning the squared difference between the price and its estimation. The line rescaling technique work as follow :
- 1 - get the z-score of a line.
- 2 - multiply this z-score with the correlation between the price and a line.
- 3 - multiply the precedent result with the standard deviation of the price, then sum that to a .
This process is shorter than the classical least-squares moving average method.
Z-Score Derivation And The Inverse Fisher Transform
The FLSMA will use a similar approach to the line rescaling technique but instead of using the correlation during step 2 we will use an alternative calculated from the error between the estimate and the price.
In order to do so we must use the inverse , the inverse can take a z-score and scale it in a range of (1,-1), it is possible to estimate the correlation with it. First lets create our modified z-score in the form of : Z = ma((y - Y)/e) where y is the price, Y our output estimate and e the moving average absolute error between the price and Y and lets call it scaled smoothed error, then apply the inverse : r = IFT (Z) = tanh (Z), we then multiply the z-score of the line with it.
The FLSMA greatly reduce the overshoots, this mean that the maximas of abs(r) are lower than the maxima's of the absolute correlation, such case is not "bad" but we can see that the filter is not closer to the price than the during trending periods, we can assume the filter don't reduce least-squares as well as the .
The image above is the running mean of the absolute error of each the FLSMA (in red) and the (in blue), we could fix this problem by multiplying the smooth scaled error by p where p can be any number, for example :
z = ( src - nz (b, src ),length)/e * p where p = 2
In red the FLSMA and in blue the FLSMA with p = 2, the greater p is the less lag the FLSMA will have.
It could be possible to get better results than the with such design, the presented indicator use its own correlation replacement but it is possible to use anything in a range of (1,-1) to multiply the line z-score. Although the proposed filter only reduce overshoots without keeping the accuracy of the i believe the code can be useful for others.
Thanks for reading.
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 a publication is governed by House Rules. You can favorite it to use it on a chart.
Become a Patreon and get access to exclusive technical indicators!
You can also check out some of the indicators I made for luxalgo : https://www.tradingview.com/u/LuxAlgo/#published-scripts