Moving Average

Overview


To calculate the moving average, we use the Moving Average Library. There are several different methods for calculating a moving average, in this case, we use the runningMovingAverage method.

You create an instance of a running moving average function by calling the method and passing in the size of desired moving average, in this case, we will use a 200 period (day) moving average.
Each time the running moving average function is called with a new number, it updates the value of the moving average and returns that value. In this case, the first 199 calls to the method will return null.

Full Script


The following code will create a new dataset which is a copy of the prices dataset but with the 200 day moving average appended to each record, and a field which calculates the difference between the price and the moving average as a proportion of price. (This will be used to determine if the size of the difference between the price and the moving average has an effect on the expected return)

Next Steps


After computing a moving average, we create a dynamic visualization.