Skip to main content
deleted 60 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

How can i clean up this code python code Moving average of a data series

How cleancan I rework the code up so usage is

ema(x, 5)that ema(x, 5) returns the data now in emaout. Imemaout? I'm trying to enclose everything in one def.

ex:

def ema(series, period):
   return ema

Right now its in a for loop and a def. and would work with numpy array.

x = [32.47, 32.70, 32.77, 33.11, 33.25, 33.23, 33.23, 33.0, 33.04, 33.21]

def ema(series, period, prevma):
    smoothing = 2.0 / (period + 1.0)                
    return prevma + smoothing * (series[bar] - prevma)

prevema = x[0] 
emaout =[]

for bar, close in enumerate(x):
    curema = ema(x, 5, prevema) 
    prevema = curema
    emaout.append(curema)

print 
print emaout  

x could be a NumPy array.

How can i clean up this code python code

How clean the code up so usage is

ema(x, 5) returns the data now in emaout. Im trying to enclose everything in one def.

ex:

def ema(series, period):
   return ema

Right now its in a for loop and a def. and would work with numpy array.

x = [32.47, 32.70, 32.77, 33.11, 33.25, 33.23, 33.23, 33.0, 33.04, 33.21]

def ema(series, period, prevma):
    smoothing = 2.0 / (period + 1.0)                
    return prevma + smoothing * (series[bar] - prevma)

prevema = x[0] 
emaout =[]

for bar, close in enumerate(x):
    curema = ema(x, 5, prevema) 
    prevema = curema
    emaout.append(curema)

print 
print emaout  

Moving average of a data series

How can I rework the code up that ema(x, 5) returns the data now in emaout? I'm trying to enclose everything in one def.

Right now its in a for loop and a def.

x = [32.47, 32.70, 32.77, 33.11, 33.25, 33.23, 33.23, 33.0, 33.04, 33.21]

def ema(series, period, prevma):
    smoothing = 2.0 / (period + 1.0)                
    return prevma + smoothing * (series[bar] - prevma)

prevema = x[0] 
emaout =[]

for bar, close in enumerate(x):
    curema = ema(x, 5, prevema) 
    prevema = curema
    emaout.append(curema)

print 
print emaout

x could be a NumPy array.

Source Link
Merlin
  • 149
  • 1
  • 7

How can i clean up this code python code

How clean the code up so usage is

ema(x, 5) returns the data now in emaout. Im trying to enclose everything in one def.

ex:

def ema(series, period):
   return ema

Right now its in a for loop and a def. and would work with numpy array.

x = [32.47, 32.70, 32.77, 33.11, 33.25, 33.23, 33.23, 33.0, 33.04, 33.21]

def ema(series, period, prevma):
    smoothing = 2.0 / (period + 1.0)                
    return prevma + smoothing * (series[bar] - prevma)

prevema = x[0] 
emaout =[]

for bar, close in enumerate(x):
    curema = ema(x, 5, prevema) 
    prevema = curema
    emaout.append(curema)

print 
print emaout