Created
April 5, 2023 07:10
-
-
Save antonylu/b9c3e74041996052562eda65b094d45e to your computer and use it in GitHub Desktop.
Simple Moving Average
This file contains hidden or 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
| BUFFER_SIZE = 900 | |
| buffer = [0] * BUFFER_SIZE | |
| index = 0 | |
| total = 0 | |
| count = 0 | |
| def moving_average(new_value): | |
| global index, total, count | |
| if count < BUFFER_SIZE: | |
| count += 1 | |
| index = count - 1 | |
| else: | |
| total -= buffer[index] | |
| total += new_value | |
| buffer[index] = new_value | |
| index = (index + 1) % BUFFER_SIZE | |
| if count < BUFFER_SIZE: | |
| return -1 | |
| else: | |
| return total / BUFFER_SIZE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment