All examples By author By category About

ColinEberhardt

d3fc - example with multiple moving averages

Multiple moving averages

A quick example to show how multiple moving averages can be applied to a chart. By default, the d3fc algorithms write the results back onto the datapoint within the supplied series data. Therefore, if you apply the the same algorithm, the results will be overwritten.

This behaviour can be changed by supplying merge function that allows you to change where the results are stored:

// compute a 20 point ema
var ema20 = fc.indicator.algorithm.exponentialMovingAverage()
    .value(function(d) { return d.close; })
    // the merge function describes how the ema is 'stored' in each datapoint
    .merge(function(datum, ema) { datum.ema20 = ema; })
    .windowSize(20);