series-processing
Version:
Time-series processing for forex, market analysis, including MA, EMA,...
19 lines (16 loc) • 517 B
JavaScript
/**
* Simple Moving Average
* @see https://en.wikipedia.org/wiki/Moving_average
*
* <output series> = sma(<input series>, length)
*/
export const SMA = (outputKey, inputKey, length) => (lastPoint, dataStream) => {
const segment = dataStream.getLastSegment(length);
const segmentLength = segment.length;
if (segmentLength < length) {
// Not enough to calculate
return null;
}
const sma = segment.reduce((n, point) => n + point[inputKey], 0) / segmentLength;
return { [outputKey] : sma };
};