-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMOM.ts
More file actions
64 lines (49 loc) 路 1.43 KB
/
Copy pathMOM.ts
File metadata and controls
64 lines (49 loc) 路 1.43 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Indicator, IndicatorInput } from '@/indicator';
import { RollingWindow } from '@/utils/RollingWindow';
export interface MOMInput extends IndicatorInput {
values: number[];
period: number;
}
export type MOMOutput = number | undefined;
export type MOMTick = number;
/**
* ### Momentum (MOM)
*
* The Momentum indicator (MOM) is a technical analysis tool used to measure the speed or strength of a security's price movement.
* It essentially reflects the change in price over a specific period.
*
* **Formula:**
*
* MOM = Close(period + 1) - Close(current)
*
* **Sources:**
*
* - http://www.onlinetradingconcepts.com/TechnicalAnalysis/Momentum.html
*/
export class MOM extends Indicator<MOMOutput, MOMTick> {
period: number;
readonly window: RollingWindow;
protected override result: MOMOutput[] = [];
protected override generator;
constructor(input: MOMInput) {
super(input);
this.period = input.period || 14;
this.window = new RollingWindow(this.period + 1);
this.generator = this.momGenerator();
this.generator.next();
input.values.forEach((tick) => {
this.nextValue(tick);
});
}
private *momGenerator(): IterableIterator<MOMOutput, never, MOMTick> {
let tick = yield;
let output;
while (true) {
this.window.push(tick);
if (this.window.filled()) {
output = this.window.at(-1)! - this.window.at(0)!;
}
tick = yield output;
}
}
}