Moving Average Script

Overview

After the data has been added to the desktop, it can be retrieved from a script by using the $val service and passing in the name of the dataset.

$val('Fixed Timeseries')



Next, we utilize the map method of Javascript array to create a new dataset set with the moving average calculated and appended to each record. For each point in the time series, we do the following calculation.

let data = $val('Fixed Timeseries').map((p,i,data)=>{ let item = {...p, ma:null}; let movingAverage = 20; let list = $list(data).first(i+1).last(movingAverage); if(list.length === movingAverage) item.ma = list.map(p=>p.price).average(); return item; });

the calculation creates a new object, and then utilizes the $list utility to retrieve the items from the data array that are needed to calculate the average.

Moving Average Library

Moving Average API

Next Steps

In the last step, we tie the moving average used to the value of slider control.

next