Skip to content

Instantly share code, notes, and snippets.

@antonylu
Created April 5, 2023 07:10
Show Gist options
  • Select an option

  • Save antonylu/b9c3e74041996052562eda65b094d45e to your computer and use it in GitHub Desktop.

Select an option

Save antonylu/b9c3e74041996052562eda65b094d45e to your computer and use it in GitHub Desktop.
Simple Moving Average
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