# Function Savitzky Golay Filter with 7 Vectors V0

737 views
737
EXPERIMENTAL:
update with extras.
the 3rd function(multipass) doesnt work, dont know why :|
```//@version=2
study("Function Savitzky Golay Filter with 7 Vectors V0", overlay=true)
//reference: http://www.statistics4u.info/fundstat_eng/cc_filter_savgolay.html
f_savitzky_golay_filter_w_7_vectors(_source)=>
_polynomial = (-2 * _source[6] + 3 * _source[5] + 6 * _source[4] + 7 * _source[3] + 6 * _source[2] + 3 * _source[1] - 2 * _source[0]) / 21

f_savitzky_golay_filter_w_scaling_7_vectors(_source, _window)=>
_polynomial = (-2 * _source[6 * _window] + 3 * _source[5 * _window] + 6 * _source[4 * _window] + 7 * _source[3 * _window] + 6 * _source[2 * _window] + 3 * _source[1 * _window] - 2 * _source[0]) / 21

f_savitzky_golay_filter_w_7_vectors_multipass(_source, _total_passes)=>
_v0 = _source[0]
_v1 = _source[1]
_v2 = _source[2]
_v3 = _source[3]
_v4 = _source[4]
_v5 = _source[5]
_v6 = _source[6]
for _i = 0 to _total_passes
_v0 := ((-2 * _v6[06] + 3 * _v5[05] + 6 * _v4[04] + 7 * _v3[3] + 6 * _v2[2] + 3 * _v1[1] - 2 * _v0[0]) / 21)
_v1 := ((-2 * _v6[07] + 3 * _v5[06] + 6 * _v4[05] + 7 * _v3[4] + 6 * _v2[3] + 3 * _v1[2] - 2 * _v0[1]) / 21)
_v2 := ((-2 * _v6[08] + 3 * _v5[07] + 6 * _v4[06] + 7 * _v3[5] + 6 * _v2[4] + 3 * _v1[3] - 2 * _v0[2]) / 21)
_v3 := ((-2 * _v6[09] + 3 * _v5[08] + 6 * _v4[07] + 7 * _v3[6] + 6 * _v2[5] + 3 * _v1[4] - 2 * _v0[3]) / 21)
_v4 := ((-2 * _v6[10] + 3 * _v5[09] + 6 * _v4[08] + 7 * _v3[7] + 6 * _v2[6] + 3 * _v1[5] - 2 * _v0[4]) / 21)
_v5 := ((-2 * _v6[11] + 3 * _v5[10] + 6 * _v4[09] + 7 * _v3[8] + 6 * _v2[7] + 3 * _v1[6] - 2 * _v0[5]) / 21)
_v6 := ((-2 * _v6[12] + 3 * _v5[11] + 6 * _v4[10] + 7 * _v3[9] + 6 * _v2[8] + 3 * _v1[7] - 2 * _v0[6]) / 21)
_return = _v0

plot(f_savitzky_golay_filter_w_7_vectors(close), title='regular', color=blue)

window = input(10)

plot(f_savitzky_golay_filter_w_scaling_7_vectors(close, window), title='scaling', color=red)
plot(f_savitzky_golay_filter_w_7_vectors_multipass(close, window), title='multipass', color=maroon)

```
It worked for me when i changed it to:

fSavitzkyGolayMultipass(msrcRAW,total_passes) =>
tpc= (total_passes + 0)
msrc= fixnan(msrcRAW)
v0= nz(msrc)
v1= nz(msrc)
v2= nz(msrc)
v3= nz(msrc)
v4= nz(msrc)
v5= nz(msrc)
v6= nz(msrc)
// theV=1
theV= for _i=0 to tpc
tmpr=v0
v0:=((-2.0 * nz(_v6,v6)) + (3.0 * nz(_v5,v5)) + (6.0 * nz(_v4,v4)) + (7.0 * nz(_v3,v3)) + (6.0 * nz(_v2,v2)) + (3.0 * nz(_v1,v1)) - (2.0 * tmpr)) / 21.0
_v1=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
_v2=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
_v3=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
_v4=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
_v5=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
_v6=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
v0
theV
MasterDisaster
@MasterDisaster
well.. this looks shitty here... all the code missbehavied:
//@version=2
...
..
..

fSavitzkyGolayMultipass(msrcRAW,total_passes) =>
// tpc= (total_passes + 0)
// msrc= fixnan(msrcRAW)
// v0= nz(msrc)
// v1= nz(msrc)
// v2= nz(msrc)
// v3= nz(msrc)
// v4= nz(msrc)
// v5= nz(msrc)
// v6= nz(msrc)
// // theV=1
// theV= for _i=0 to tpc
// tmpr=v0
// v0:=((-2.0 * nz(_v6,v6)) + (3.0 * nz(_v5,v5)) + (6.0 * nz(_v4,v4)) + (7.0 * nz(_v3,v3)) + (6.0 * nz(_v2,v2)) + (3.0 * nz(_v1,v1)) - (2.0 * tmpr)) / 21.0
// _v1=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
// _v2=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
// _v3=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
// _v4=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
// _v5=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
// _v6=((-2.0 * v6) + (3.0 * v5) + (6.0 * v4) + (7.0 * v3) + (6.0 * v2) + (3.0 * v1) - (2.0 * v0)) / 21.0
// v0
// theV

the case is probably in the first line with the last multiply with _v0 where i just changed it to use tmpr variable. I hope that this pasted code will not hide the altered syntax.
MasterDisaster
@MasterDisaster, please review, let me know if this works for you.
Hello Ricardo
If you have time can you make a scirpt on Polynomial Regression in Tradingview

Sample of CALGO below

Indicators cBots API Forum Jobs Consultants VPS MarketWatch

Search
HomeAlgorithmsIndicatorsPolynomial Regression Channel (PRC)
Polynomial Regression Channel (PRC)
by qualitiedx2 in category Volatility at 25/05/2012
Description
The PRC indicator applies a polynomial function to the linear regression function to adapt itself to the flow of market prices. Since they are regression bands that self adjust for volatility.

Polynomial Regression Channel
How to install
×Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Copy code

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{

public class PRC : Indicator
{

public int degree { get; set; }

public int period { get; set; }

public double strdDev { get; set; }

public double strdDev2 { get; set; }

public IndicatorDataSeries prc { get; set; }

public IndicatorDataSeries sqh { get; set; }

public IndicatorDataSeries sql { get; set; }

public IndicatorDataSeries sql2 { get; set; }

public IndicatorDataSeries sqh2 { get; set; }

private double ai = new double;
private double b = new double;
private double x = new double;
private double sx = new double;
private double sum;
private int ip;
private int p;
private int n;
private int f;
private double qq;
private double mm;
private double tt;
private int ii;
private int jj;
private int kk;
private int ll;
private int nn;
private double sq;
private double sq2;
private int i0 = 0;
private int mi;

public override void Calculate(int index)
{
ip = period;
p = ip;
sx = p + 1;
nn = degree + 1;
//----------------------sx-------------------------------------------------------------------
for(mi=1;mi<=nn*2-2;mi++) //
{
sum=0;
for(n=i0;n<=i0+p;n++)
{
sum+=Math.Pow(n,mi);
}
sx=sum;
}
//----------------------syx-----------
for(mi=1;mi<=nn;mi++)
{
sum=0.00000;
for(n=i0;n<=i0+p;n++)
{
if(mi==1) sum+=MarketSeries.Close;
else sum+=MarketSeries.Close*Math.Pow(n,mi-1);
}
b=sum;
}
//===============Matrix=======================================================================================================
for(jj=1;jj<=nn;jj++)
{
for(ii=1; ii<=nn; ii++)
{
kk=ii+jj-1;
ai=sx;
}
}
//===============Gauss========================================================================================================
for(kk=1; kk<=nn-1; kk++)
{
ll=0;
mm=0;
for(ii=kk; ii<=nn; ii++)
{
if(Math.Abs(ai)>mm)
{
mm=Math.Abs(ai);
ll=ii;
}
}
if(ll==0) return;
if (ll!=kk)
{
for(jj=1; jj<=nn; jj++)
{
tt=ai;
ai=ai;
ai=tt;
}
tt=b;
b=b;
b=tt;
}
for(ii=kk+1;ii<=nn;ii++)
{
qq=ai/ai;
for(jj=1;jj<=nn;jj++)
{
if(jj==kk) ai=0;
else ai=ai-qq*ai;
}
b=b-qq*b;
}
}
x=b/ai;
for(ii=nn-1;ii>=1;ii--)
{
tt=0;
for(jj=1;jj<=nn-ii;jj++)
{
tt=tt+ai*x;
x=(1/ai)*(b-tt);
}
}
sq=0.0;
sq2=0.0;
for(n=i0;n<=i0+p;n++)
{
sum=0;
for(kk=1;kk<=degree;kk++)
{
sum+=x*Math.Pow(n,kk);
}
prc=(x+sum);
sq+=Math.Pow(MarketSeries.Close-prc,2);
sq2+=Math.Pow(MarketSeries.Close-prc,2);
}
sq=Math.Sqrt(sq/(p+1))*strdDev;
sq2=Math.Sqrt(sq2/(p+1))*strdDev2;
for(n=i0;n<=i0+p;n++)
{
sqh=(prc+sq);
sql=(prc-sq);
sqh2=(prc+sq2);
sql2=(prc-sq2);
}
}
}
}
C