Skip to content

Instantly share code, notes, and snippets.

@GraphBear
Last active April 2, 2024 22:22
Show Gist options
  • Save GraphBear/77f8b61d1a0c2f79752c to your computer and use it in GitHub Desktop.
Save GraphBear/77f8b61d1a0c2f79752c to your computer and use it in GitHub Desktop.
weighted moving average
def wma(values, window):
# requires trinum.py
# using definition provided at
# http://www.oanda.com/forex-trading/learn/forex-indicators/weighted-moving-average
# create an array of weights
# use floats when creating array, or the result is integer division below
# and, note that they are reversed. why? read this:
# http://stackoverflow.com/questions/12816011/weighted-moving-average-with-numpy-convolve
weights = np.arange(window, 0, -1.0)
weights /= trinum(window)
# created wma array with NaN values for indexes < window value
weighted_moving_averages = np.empty(window-1)
weighted_moving_averages[:] = np.NAN
# then append the wma's onto the end
weighted_moving_averages = np.append(weighted_moving_averages, np.convolve(values, weights, 'valid'))
return weighted_moving_averages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment