def SMMA(column,N):
for i in range(len(column)):
if i <= N:
SMMA(i) = np.nan()
elif i == N + 1:
SMMA(i) = column[:N].mean()
else:
SMMA(i) = (SMMA(i-1)*N + column[i])/ N
Smoothed Moving Average (SMMA) is one of my favorite financial analysis tool.It is different from the well-know Simply moving Average tool. below is the definition and above is my code, but the IDE is kept telling me syntaxError:
File "<ipython-input-13-fdcc1fd914c0>", line 6
SMMA(i) = column[:N].mean()
^SyntaxError: can't assign to function call
Definition of SMMA:
The first value of this smoothed moving average is calculated as the simple moving average (SMA):
SUM1 = SUM (CLOSE (i), N)
SMMA1 = SUM1 / N
The second moving average is calculated according to this formula:
SMMA (i) = (SMMA1*(N-1) + CLOSE (i)) / N
Succeeding moving averages are calculated according to the below formula:
PREVSUM = SMMA (i - 1) * N
SMMA (i) = (PREVSUM - SMMA (i - 1) + CLOSE (i)) / N
Is something like this what you had in mind?
def SMMA(column,N):
result = np.empty(len(column))
for i, e in enumerate(column):
if i <= N:
result[i] = np.nan()
elif i == N + 1:
result[i] = column[:N].mean()
else:
result[i] = (result[i-1]*N + e) / N
return result
You can assign to a subscript like result[i]
in Python.
The function isn't going to do anything unless you return or yield a value or mutate an argument or something.
The above code generates and returns a NumPy float array, which may or may not be what you want. (If this is insufficient, please edit your question to clarify the intended use.)