+# Use this constant as a flag
+WINDOW_SIZE = 3
+USE_MOVING_AVERAGE = False
+
+
+def moving_average(array, window=3):
+ """ Calculate moving average
+ Args:
+ array: array of numbers
+ window: window of moving average (default = 3)
+ """
+ # Check if window > len(array)
+ if window > len(array):
+ window = len(array)
+ # Calculate cumulative sum of each array element
+ retarr = np.cumsum(array, dtype=float)
+ # Adjust cumulative sum of each array element
+ # based on window size
+ retarr[window:] = retarr[window:] - retarr[:-window]
+ # Pad the first array elements with zeroes
+ retarr[:window - 1] = np.zeros(window - 1)
+ # Calculate moving average starting from the element
+ # at window size, e.g. element 4 for window=5
+ retarr[window - 1:] = retarr[window - 1:] / window
+ return retarr
+