I have written the function below in order to find the SMA of a csv file according to the desired SMA formula, However, something is wrong with my formula which I can't figure it out.
def SMA_calculation(t, w):
s = np.size(t)
g = np.zeros(s)
for i in range(0, s):
if i < w-1:
g[i] = np.NaN
else:
g[i] = np.mean(t[i-w:i])
return g
The first time the
elseblock is executed, is wheni == w - 1. This means the argument passed tomeanist[-1:w-1]. This is wrong. The first slice you want to get the mean of ist[:w]. So you need to add one to both start and end indices of the slice: