Last active
April 2, 2024 22:22
-
-
Save GraphBear/77f8b61d1a0c2f79752c to your computer and use it in GitHub Desktop.
weighted moving average
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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