Page 1 of 1

Convert pine script old version to version 5

Posted: Tue Feb 01, 2022 4:07 am
by crypturtle
Hello,

I recently start to learn Pine code few weeks ago (huge thanks to Mathew for the fantastic tutorial !!!).
I am trying to convert an old code I found from #madrid into version 5

ssFilter( price, lowerBand ) =>
angle = sqrt(2)*PI/lowerBand
a1= exp(-angle)
b1 = 2*a1*cos(angle)
c2 = b1
c3 = -a1*a1
c1 = 1 - c2 -c3
filt = c1*(price + nz(price[1]))/2 + c2*nz(filt[1]) + c3*nz(filt[2])

But I get the following error:
Add to Chart operation failed, reason: line 26: Undeclared identifier `filt`

so I have try the following thing:

ssFilter(close, lowerBand) =>
angle = sqrt(2)*PI/lowerBand
a1= exp(-angle)
b1 = 2*a1*cos(angle)
c2 = b1
c3 = -a1*a1
c1 = 1 - c2 -c3
c1*(close + nz(close[1]))/2 + c2*nz(filt[1]) + c3*nz(filt[2])

filt := ssFilter(close, lowerBand)

but I still have the same error:
line 25: Undeclared identifier `filt`

if anyone know how to fix it your help will be greatly appreciated! :zen:

Re: Convert pine script old version to version 5

Posted: Tue Feb 01, 2022 4:54 pm
by processingclouds
Hey crypturtle,

Your last line :
filt = c1*(price + nz(price[1]))/2 + c2*nz(filt[1]) + c3*nz(filt[2])
OR
c1*(close + nz(close[1]))/2 + c2*nz(filt[1]) + c3*nz(filt[2])

It has the variable filt already being used. So technically you are trying to reference filt within filt without it being initialized in anyways.

Re: Convert pine script old version to version 5

Posted: Tue Feb 01, 2022 4:59 pm
by processingclouds
A way to solve this is to add filt = 0 before hand.
Also to convert to version 5 , we add math. to each function making it math.sqrt , math.pi and math.cos

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds

//@version=5
indicator("My script")

ssFilter( price, lowerBand ) =>
    angle = math.sqrt(2) * math.pi/lowerBand
    a1= math.exp(-angle)
    b1 = 2 * a1 * math.cos(angle)
    c2 = b1
    c3 = -a1 * a1
    c1 = 1 - c2 -c3
    filt = 0.0
    filt := c1*(price + nz(price[1]))/2 + c2*nz(filt[1]) + c3*nz(filt[2])

plot(na)
I hope this helps. If you need complete code conversion, i can help you convert it to v5.

Re: Convert pine script old version to version 5

Posted: Tue Feb 01, 2022 8:01 pm
by crypturtle
Thank you very very very much processingclouds! its finally working.
I had try to declare filt=0.0 outside, just before the ssfilter function and it wasn't working.

anyway, thank you again.

Re: Convert pine script old version to version 5

Posted: Thu Feb 03, 2022 11:49 pm
by processingclouds
Welcome , if you have any more questions , just drop a post .