 # Polynomial LSMA Estimation - Estimating An LSMA Of Any Degree 2704 views
Introduction

It was one of my most requested post, so here you have it, today i present a way to estimate an LSMA of any degree by using a kernel based on a sine wave series, note that this is originally a paper that i posted that you can find here https://figshare.com/articles/A_NEW_LOW-... , in the paper you will be able to find the frequency response of the filter as well as both python and pinescript code.

The least squares moving average or LSMA is a filter that best fit a polynomial function through the price by using the method of least squares, by default the LSMA best fit a line through the input by using the following formula : ax + b where x is often a linear series 1,2,3...etc and a/b are parameters, the LSMA is made by finding a and b such that their values minimize the sum of squares between the lsma and the input.

Now a LSMA of 2nd degree (quadratic) is in the form of ax^2 + bx + c, although the first order LSMA is not hard to make the 2nd order one is way more heavy in term of codes since we must find optimal values for a,b and c, therefore we may want to find alternatives if the goal is simply data smoothing.

Estimation By Convolution

The LSMA is a FIR filter which posses various characteristics, the impulse response of an LSMA of degree n is a polynomial of the same degree, and its step response is a polynomial of degree n+1, estimating those step response is done by the described sine wave series :

f(x) =>
sum = 0.
b = 0.
pi = atan(1)*4
a = x*x
for i = 1 to d
b := 1/i * sin(x*i*pi)
sum := sum + b
pol = a + iff(d == 0,0,sum)

which is simple the sum of multiple sine waves of different frequency and amplitude + the square of a linear function. We then differentiate this result and apply convolution.

The Indicator

length control the filter period while degree control the degree of the filter, higher degree's create better fit with the input as seen below :

Now lets compare our estimate with actual LSMA's, below a lsma in blue and our estimate in orange of both degree 1 and period 100 :

Below a LSMA of degree 2 (quadratic) and our estimate with degree 2 with both period 100 :

It can be seen that the estimate is doing a pretty decent job.

Now we can't make comparisons with higher degrees of lsma's but thats not a real necessity.

Conclusion

This indicator wasn't intended as a direct estimate of the lsma but it was originally based on the estimation of polynomials using sine wave series, which led to the proposed filter showcased in the article. So i think we can agree that this is not a bad estimate although i could have showcased more statistics but thats to many work, but its not that interesting to use higher degree's anyways so sticking with degree 1, 2 and 3 might be for the best.

Hope you like and thanks for reading !
Protected script
This script is published closed-source and you may use it freely. You can favorite it to use it on a chart. You cannot view or modify its source code.
Want to use this script on a chart?
Patreon: https://www.patreon.com/alexgrover

You can also check out some of the indicators I made for luxalgo : https://www.tradingview.com/u/LuxAlgo/#published-scripts alexgrover
@alexgrover, Alex, the script from the article won't compile, what's the problem?

kernel(x, order) =>
sum = 0.
b = 0.
pi = atan(1)*4
a = x*x
for i = 1 to order
b := 1/i * sin(x*i*pi)
sum := sum + b
pol = a + sum
//----
F(src, M, order) =>
sum = 0.
for i = 1 to M
w = kernel(i/M,order) - kernel((i-1)/M,order)
sum := sum + src * w
sum navyreal7
@navyreal7, Thats just a declared function, you would need to copy the full code and past it, also the script was made using the v2, so using the v4 won't work, make sure to replace "red" in the plot function by "color.red". alexgrover
@alexgrover, I realized that this is not the code itself, but a function? but in the article there seems to be nothing else, where is the indicator code itself?
Or do I need to add this function to the version 2 script? navyreal7
@navyreal7,
```//@version=2
study("Polynomial LSMA Estimation",overlay=true)
length = input(100),d = input(2,title="Degree")
//----
f(x) =>
sum = 0.
b = 0.
pi = atan(1)*4
a = x*x
for i = 1 to d
b := 1/i * sin(x*i*pi)
sum := sum + b
pol = a + iff(d == 0,0,sum)
//----
F(x) =>
sum = 0.
for i = 1 to length
w = f(i/length) - f((i-1)/length)
sum := sum + x[i-1] * w
sum
//----
src = input(close)
pka = F(src)
//----
plot(pka,color=red,linewidth=2,transp=0)``` 