Skip to content

Instantly share code, notes, and snippets.

@nrm176
Created June 24, 2017 04:54
Show Gist options
  • Save nrm176/b4a3a0b1bb9f6d97c955e1e24cfd2912 to your computer and use it in GitHub Desktop.
Save nrm176/b4a3a0b1bb9f6d97c955e1e24cfd2912 to your computer and use it in GitHub Desktop.
Simple Moving Average
import numpy as np
#mock data
data = '''853 1,005
992 876
589 1,330
1,532 389
1,414 508
1,237 652
'''
def simpleMovingAverage(values, window):
weights = np.repeat(1.0, window)/window
smas = np.convolve(values, weights, 'valid')
return smas
ups = []
downs = []
for d in data.split('\n'):
up, down = d.split('\t')
ups.append(int(up.replace(',', '')))
downs.append(int(down.replace(',', '')))
movingAverageUps = simpleMovingAverage(ups,6)
movingAverageDowns = simpleMovingAverage(downs,6)
for u,d in zip(movingAverageUps,movingAverageDowns):
print(np.round(u/d*100,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment