Calculating Indicators

Overview



When performing a trade backtest, it is not necessary to pre-calculate all the indicators and to append them as new columns to the price data. Rather, one can perform a stateful running calculation that calculates each indicator on the fly when needed.

Moving Average Example



The following example uses the moving average library to calculate a 200 day moving average each time the trade function is called.


let ma = await import('/lib/time-series/moving-average/v1.0.0/moving-average.mjs');

let nma = ma.runningMoivngAverage(200);

let trade = function(price, nav){
  let movingAverage200 = nma(price.close);
  let result = {};
  if(ma !== null && ma < price.close) result.close = nav/price.close;
  return result;
}
					


Contents