-1
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

halfer
  • 19,824
  • 17
  • 99
  • 186
user9161038
  • 13
  • 1
  • 1
  • 3
  • `=` is used for assigning a value to a variable. It has to be `variable = value`. It makes no sense to assign to a function call. What do you intend that to mean? – Barmar Jun 29 '18 at 02:27
  • https://startpythonml.wordpress.com/2016/03/13/smoothed-moving-average-and-variations/ – Barmar Jun 29 '18 at 02:29
  • Related: https://stackoverflow.com/questions/13728392/moving-average-or-running-mean – Barmar Jun 29 '18 at 02:30

3 Answers3

0

SMMA(i) is written as if calling a function. A variable can be assigned to the output of a function but it does not make sense to call a function as a variable and set it equal to a value. For example moving_average = SMMA(i) would assign a variable, moving_average, to the output of the function SMMA(i) but SMMA(i) = moving_average does not make sense. Hope I helped. `

SMMA(i) = 4
Traceback (most recent call last):
Python Shell, prompt 5, line 1
Syntax Error: can't assign to function call: <string>, line 1, pos 0

`

Jack
  • 11
  • 2
0

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.)

gilch
  • 10,813
  • 1
  • 23
  • 28
  • Dude, thanks a lot, It works.I should assign to a subscript 'result[i]' first, and use the built in func 'enumerate'! Very appreciate! – user9161038 Jul 03 '18 at 03:13
0

I recommend to use simple EMA calculation to achieve this.
SMMA essentially is EMA but just with different length.

For a SMMA(x) , I will do EMA(x*2-1)

SMMA EMA
1 1
2 3
3 5
4 7
5 9
6 11
7 13
8 15
9 17
10 19
11 21
12 23
13 25
14 27
15 29
16 31
17 33
Yunnosch
  • 26,130
  • 9
  • 42
  • 54