Momentum
Overview
Momentum is an indicator used to specify the direction (and strength) of recent movement of an asset. It is similar
to
moving average indicator in that measures the direction that an asset has
been trading recently. Typically, momentum is just measured as the arithmetic return of the asset over a given
period. Like the moving average, the period is question indicates the time frame over which the momentum has
taken place.
Momentum has a fairly direct relationship to a
moving average. In particular, momentum is just the difference of
a simple moving average calculated with the same period, over two periods.
{% Momentum_i = MovingAverage_i - MovingAverage_{i-1} %}
where the momentum is calculate with the same number of periods, n, as the moving average.
Calculation
The momentum indicator can be fairly easy to calculate. The momentum is just the current price minus the price, n periods ago.
let momentum = data[i].price - data[i-n].price;
NOTE : This calculation treats all days as equivalent, thereby ignoring any effects that holidays and weekends have on
prices.
You can assign a momentum value to each value in an array of prices by using the map function on the
array. (similar to the code using a
list)
let data = prices.map((p, i, prices)=>{
let item = {...p};
if(i>periods) item.momentum = p.price-prices[i-periods].price;
else item.momentum = null;
return item;
})
As an alternative, the momentum function is available through the technical module and can
be used with
$list.merge.
let tc = await import('/lib/finance/technical/v1.0.0/technical.mjs');
let data = [{price:100}, {price:101},{price:102},{price:100},{price:99},{price:100},{price:98},{price:100},{price:102},];
let data2 = $list(data).merge(tc.momentum(3),'momentum').items;
Try it!