0

Since after re-writting my question there is still no further answer, I try to describe my question in another way.

The expression looks as follow:

y = a*(datas + Prev(datas,1)/2) + b*Prev(y,1) + c*Prev(y,2)

The a, b and c are constants.

The datas is a Series and has n-elements.

The Prev(datas,1) is the privious value of datas.

The initial values of y are 0 and can be defined as:

y = pd.Series(np.zeros_like(datas))

The Prev(y, 1) is the privious of y and the Prev(y,2) is the privious of Prev(y,1)

thomas2013ch
  • 133
  • 2
  • 11
  • you can just return the value without declaring the filt variable using __return( c1*(price + price.shift(1))/2 + c2*filt.shift(1) + c3*filt.shift(2))__ – babygame0ver Dec 18 '17 at 09:21
  • Sorry, I've tried again, simply using return( c1*(price + price.shift(1))/2 + c2*filt.shift(1) + c3*filt.shift(2)) doesn't work. I got error: global name 'filt' is not defined – thomas2013ch Dec 18 '17 at 10:04
  • I do not really get what you are trying to do here since you are using `filt` as a variable. Can you insert your code where you use it as a function – jan-seins Dec 18 '17 at 10:07

2 Answers2

1

if you want to change a global variable in python you need to reference it again in your function

def computeSuperSmootherFilter(price, cutoffLength):
    global filt # Needed to modify global copy of globvar
    a1 = np.exp(-math.pi * math.sqrt(2)/cutoffLength )
    b1 = 2 * a1 * math.cos( math.sqrt(2) * math.pi / cutoffLength )
    c2 = b1
    c3 = -a1*a1
    c1 = 1- c2 - c3
    filt = c1*(price + price.shift(1))/2 + c2*filt.shift(1) + c3*filt.shift(2)

    return filt
jan-seins
  • 1,253
  • 1
  • 18
  • 31
  • 1
    A word of caution - [Why are global variables evil?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – DavidG Dec 18 '17 at 09:27
  • Hi Jan, as I wrote in my post, defining the 'filt' as a global doesn't return the correct values since here the 'filt' is not a variable, but a function. – thomas2013ch Dec 18 '17 at 10:00
0

I think I've found the solution to my question myself. I do as follow:

y=pd.Series(np.zeros_like(datas))

for i in range(len(datas)):    
    try:
        y[i] = c1*(datas[i] + datas[i-1])/2 + c2*y[i-1] + c3*y[i-2]
    except Exception as e:
        print(e)

    y
thomas2013ch
  • 133
  • 2
  • 11