/
hma.ts
33 lines (32 loc) · 941 Bytes
/
hma.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {
comp,
drop,
iterator1,
map,
multiplex,
Transducer,
} from "@thi.ng/transducers";
import { wma } from "./wma";
/**
* {@link https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/hull-moving-average}
*
* Note: the number of results will be `period + floor(sqrt(period)) - 2`
* less than the number of processed inputs.
*
* @param weights - period or array of weights
*/
export function hma(period: number): Transducer<number, any>;
export function hma(
period: number,
src: Iterable<number>
): IterableIterator<number>;
export function hma(period: number, src?: Iterable<number>): any {
return src
? iterator1(hma(period), src)
: comp(
multiplex(wma((period / 2) | 0), wma(period)),
drop(period - 1),
map((w) => 2 * w[0] - w[1]),
wma(Math.sqrt(period))
);
}