-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathmoneyFlowIndex.ts
More file actions
74 lines (64 loc) · 1.95 KB
/
Copy pathmoneyFlowIndex.ts
File metadata and controls
74 lines (64 loc) · 1.95 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
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2022-2026 The Indicator Authors. All rights reserved.
// https://github.com/cinar/indicatorts
import {
addBy,
changes,
divide,
extractSigns,
multiply,
multiplyBy,
pow,
} from '../../helper/numArray';
import { msum } from '../trend/movingSum';
import { typprice } from '../trend/typicalPrice';
/**
* Optional configuration of MFI parameters.
*/
export interface MFIConfig {
period?: number;
}
/**
* The default configuration of MFI.
*/
export const MFIDefaultConfig: Required<MFIConfig> = {
period: 14,
};
/**
* The Money Flow Index (MFI) analyzes both the closing price and the volume
* to measure to identify overbought and oversold states. It is similar to
* the Relative Strength Index (RSI), but it also uses the volume.
*
* Raw Money Flow = Typical Price * Volume
* Money Ratio = Positive Money Flow / Negative Money Flow
* Money Flow Index = 100 - (100 / (1 + Money Ratio))
*
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @param volumes volume values.
* @param config configuration.
* @return money flow index values.
*/
export function mfi(
highs: number[],
lows: number[],
closings: number[],
volumes: number[],
config: MFIConfig = {}
): number[] {
const { period } = { ...MFIDefaultConfig, ...config };
const typicalPrice = typprice(highs, lows, closings);
const rawMoneyFlow = multiply(typicalPrice, volumes);
const signs = extractSigns(changes(1, typicalPrice));
const moneyFlow = multiply(signs, rawMoneyFlow);
const positiveMoneyFlow = moneyFlow.map((value) => (value >= 0 ? value : 0));
const negativeMoneyFlow = moneyFlow.map((value) => (value < 0 ? value : 0));
const moneyRatio = divide(
msum(positiveMoneyFlow, { period }),
msum(multiplyBy(-1, negativeMoneyFlow), { period })
);
const result = addBy(100, multiplyBy(-100, pow(addBy(1, moneyRatio), -1)));
return result;
}
// Export full name
export { mfi as moneyFlowIndex };