-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
ulcerIndex.ts
58 lines (52 loc) · 1.48 KB
/
ulcerIndex.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
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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import {
divide,
multiply,
multiplyBy,
sqrt,
subtract,
} from '../../helper/numArray';
import { mmax } from '../trend/movingMax';
import { sma } from '../trend/simpleMovingAverage';
/**
* Optional configuration of UI parameters.
*/
export interface UIConfig {
period?: number;
}
/**
* The default configuration of UI.
*/
export const UIDefaultConfig: Required<UIConfig> = {
period: 14,
};
/**
* The Ulcer Index (UI) measures downside risk. The index increases in value
* as the price moves farther away from a recent high and falls as the price
* rises to new highs.
*
* High Closings = Max(period, Closings)
* Percentage Drawdown = 100 * ((Closings - High Closings) / High Closings)
* Squared Average = Sma(period, Percent Drawdown * Percent Drawdown)
* Ulcer Index = Sqrt(Squared Average)
*
* @param closings closing values.
* @param config configuration.
* @returns ui values.
*/
export function ui(closings: number[], config: UIConfig = {}): number[] {
const { period } = { ...UIDefaultConfig, ...config };
const highClosings = mmax(closings, { period });
const percentageDrawdown = multiplyBy(
100,
divide(subtract(closings, highClosings), highClosings)
);
const squaredAverage = sma(multiply(percentageDrawdown, percentageDrawdown), {
period,
});
const result = sqrt(squaredAverage);
return result;
}
// Export full name
export { ui as ulcerIndex };