The FinTech utility collections of simple, cumulative, and exponential moving averages.
npm install finmath

This module is lack of maintenance.
If you are familiar with python programming maybe you could check stock-pandas which provides powerful statistic indicators support, and is backed by numpy and pandas.
The performance of stock-pandas is many times higher than JavaScript libraries, and can be directly used by machine learning programs.
**
The complete collection of mathematical utility methods for FinTech , including:
- Moving averages
- MACD
- Bollinger bands
- Standard deviations
- Highest high values / Lowest low values
And all finmath methods also handle empty values.
- simple Moving Average (MA)
- Dynamic weighted Moving Average (DMA)
- Exponential Moving Average (EMA)
- Smoothed Moving Average (SMA)
- Weighted Moving Average (WMA)
- MACD
- BOLLinger bands (BOLL)
- Standard Deviations (SD)
- Highest High Values (HHV)
- Lowest Low Values (LLV)
``sh`
$ npm i finmath
`js
import {
ma, dma, ema, sma, wma,
macd,
boll,
sd,
hhv, llv,
add, sub, mul, div
} from 'finmath'
ma([1, 2, 3, 4, 5], 2)
// [<1 empty item>, 1.5, 2.5, 3.5, 4.5]
`
`ts`
type Data = EmptyableArray
- data Data the collection of data inside which empty values are allowed. Empty values are useful if a stock is suspended.number
- size the size of the periods.
Returns Data
Type Array represents an array of numbers or empty items. And every method of finmath does NOT accepts items that are not numbers.
`js
[1,, 2, 3] // OK ✅
[1, undefined, 2, 3] // NOT OK ❌
[1, null, 2, 3] // NOT OK ❌
`
#### Special Cases
`js1
// If the size is less than
ma([1, 2, 3], 0.5) // [1, 2, 3]
// If the size is larger than data length
ma([1, 2, 3], 5) // [<3 empty items>]
ma([, 1,, 3, 4, 5], 2)
// [<2 empty items>, 0.5, 1.5, 3.5, 4.5]
`
And all of the other moving average methods have similar mechanism.
- data
- alpha Data the coefficient or list of coefficients alpha represents the degree of weighting decrease for each datum.alpha
- If is a number, then the weighting decrease for each datum is the same.alpha
- If larger than 1 is invalid, then the return value will be an empty array of the same length of the original data.alpha
- If is an array, then it could provide different decreasing degree for each datum.Boolean=
- noHead whether we should abandon the first DMA.
Returns Data
`js
dma([1, 2, 3], 2) // [<3 empty items>]
dma([1, 2, 3], 0.5) // [1, 1.5, 2.25]
dma([1, 2, 3, 4, 5], [0.1, 0.2, 0.1])
// [1, 1.2, 1.38]
`
Calulates the most frequent used exponential average which covers about 86% of the total weight (when alpha = 2 / (N + 1)).
- data
- size Number the size of the periods.
Returns Data
Also known as the modified moving average or running moving average, with alpha = times / size.
- data
- size
- times? Number=1
Returns Data
Calculates convolution of the datum points with a fixed weighting function.
Returns Data
MACD, short for Moving Average Convergence / Divergence, is a trading indicator used in technical analysis of stock prices, created by Gerald Appel in the late 1970s.
- data Data the collection of pricesnumber=26
- slowPeriods? the size of slow periods. Defaults to 26number=12
- fastPeriods? the size of fast periods. Defaults to 12number=9
- signalPeriods? the size of periods to calculate the MACD signal line.
Returns MACDGraph
`js
macd(data)
// which returns:
// {
// MACD:
// signal:
// histogram:
// }
`
- MACD Data the difference between EMAs of the fast periods and EMAs of the slow periods.Data
- signal the EMAs of the MACDData
- histogram MACD minus signal
In some countries, such as China, the three series above are commonly known as:
`sh`
MACD -> DIF
signal -> DEA
histogram -> MACD
`js`
boll([1, 2, 4, 8], 2, 2)
// {
// upper: [, 2.5, 5, 10],
// mid : [, 1.5, 3, 6],
// lower: [, 0.5, 1, 2]
// }
- data Data the collection of dataNumber=20
- size? the period size, defaults to 20Number=2
- times? the times of standard deviation between the upper band and the moving average.Object=
- options? optional optionsData=
- ma? the moving averages of the provided datum and period size. This option is used to prevent duplicate calculation of moving average.Data=
- sd? the standard average of the provided datum and period size
Returns Array the array of the Band object.
`ts`
interface Band {
// the value of the upper band
upper: number
// the value middle band (simple moving average)
mid: number
// the value of the lower band
lower: number
}
- data Data the collection of datanumber
- size the sample size of
Returns Data the array of standard deviations.
`js
sd([1, 2, 4, 8], 2) // [<1 empty item>, 0.5, 1, 2]
sd([1, 2, 3, 4, 5, 6], 4)
// [
// <3 empty items>,
// 1.118033988749895,
// 1.118033988749895,
// 1.118033988749895
// ]
`
- data Data the array of closing prices.number
- periods the size of periods
Returns Data the highest high values of closing prices over the preceding periods periods (periods includes the current time).
`js
const array = [1, 2, 4, 1]
hhv(array, 2) // [, 2, 4, 4]
hhv(array) // 4
hhv(array, 5) // [<4 empty items>]
hhv(array, 1) // [1, 2, 4, 1]
hhv(array, 2) // [, 1, 2, 2]
`
Instead, returns Data` the lowest low values.