This package provides a JavaScript function to calculate an array of SMA (simple moving average), WMA (weighted moving average), and EMA (exponential moving average) of data points, using either a classic or balanced algorithm. A demo HTML page using this function is provided to try various moving average algorithms.
$ git clone https://github.com/peterthoeny/moving-average-js.git # or clone your own fork
Point your browser to moving-average-js/demo.html, also available online at https://peterthoeny.github.io/moving-average-js/demo.html.
Function usage example:
let dataPoints = [ 2, 2, 3, 3, 4, 4, 5, 8, 9, 7, 5, 4, 4, 5, 4 ]; // y-axis values
let type = 'SMA'; // simple moving average
let size = 11; // sample width
let sma = movingAverage(dataPoints, type, size);
// expected: [ 2, 2, 2.3, 2.5, 2.8, 3, 3.3, 3.9, 4.4, 4.7, 4.7, 4.9, 5.1, 5.3, 5.4 ]
The movingAverage() function in the moving-average.js Javascript file returns a moving average or moving median array from an input array of data points. The function can be used in a browser, as well as in a node.js application on a server.
Three types of classic moving average algorithms are implemented:
A classic moving average algorithm looks only at a moving window (sample width) of data points to the left of the current data point. Therefore, the moving average lags behind the current data point by half the sample width, e.g. the moving average curve is shifted to the right. This method is mainly used in financial applications.
A balanced moving average, also called central moving average, or symmetrical moving average, is computed by looking equally to both sides of the current data point. In other words, the moving average is centered around the current data point. This is primarily used in scientific applications.
The balanced moving average algorithm in this implementation first extends the given data points array by extrapolating the data points on both sides, so that the moving average curve looks more balanced at the beginning and end. The extrapolation assumes that the extended slope of the data points is equal to half of the sample width, e.g. it assumes that the same average rate of change over half the sample width happens also for the extrapolated range. Try the demo to see the extended slope on the left side and right side, and to compare that to one of the balanced moving average curves.
Three types of balanced moving average algorithms are implemented:
BSMA: Balanced simple moving averageBWMA: Balanced weighted moving averageBEMA: Balanced exponential moving average
In contrast to a moving average, a simple moving median is calculated by sorting the values inside the moving window of data points, and finding the value in the middle. The moving median tolerates shocks and outlier values better than the moving mean.
A single type of moving median is currently implemented with classic and balanced algorithms:
SMM: Simple moving medianBSMM: Balanced simple moving median
A slope shows the average rate of change over all data points:
Slope: Linear slope over all data points
/**
* Calculate a moving average array from an array of data points
*
* @param {Array} arr data points (array of y-values)
* @param {String} type 'SMA': simple moving average,
* 'BSMA': balanced simple moving average,
* 'WMA': weighted moving average,
* 'BWMA': balanced weighted moving average,
* 'EMA': exponential moving average,
* 'BEMA': balanced exponential moving average,
* 'SMM': simple moving median,
* 'BSMM': balanced simple moving median,
* 'Slope': linear slope over all data points
* @param {String} size size of moving array slice to calculate average
* @return {Array} maArr moving average array
*/
function movingAverage(arr, type, size) {
}
The demo.html file demonstrates the various moving average algorithms, also available online at https://peterthoeny.github.io/moving-average-js/demo.html. The extended slopes are shown on the left side and right side when the Slope checkbox is checked.
Screenhot of demo HTML page showing the data points (red line), the extended slopes (dashed purple lines), classic SMA curve (dashed blue line), balanced SMA curve (solid blue line), and slope (dashed red line):
LICENSE- MIT license fileREADME.md- documentationpackage.json- package definitionmoving-average.js- JavaScript file withmovingAverage()functiondemo.html- HTML file to demo various moving average algorithmsdemo.js- JavaScript file with charting functions, used by the demo HTML filescreenshot.png- Screenshot of demo
- Repository: https://github.com/peterthoeny/moving-average-js
- Copyright: 2022, Peter Thoeny
