-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
chandelierExit.ts
66 lines (58 loc) · 1.53 KB
/
chandelierExit.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
59
60
61
62
63
64
65
66
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { add, multiplyBy, subtract } from '../../helper/numArray';
import { mmax } from '../trend/movingMax';
import { mmin } from '../trend/movingMin';
import { atr } from './averageTrueRange';
/**
* Chandelier exit result object.
*/
export interface CEResult {
long: number[];
short: number[];
}
/**
* Optional configuration of Chandelier exit parameters.
*/
export interface CEConfig {
period?: number;
}
/**
* The default configuration of Chandelier exit.
*/
export const CEDefaultConfig: Required<CEConfig> = {
period: 22,
};
/**
* Chandelier Exit. It sets a trailing stop-loss based on the
* Average True Value (ATR).
*
* Long Exit = 22-Period SMA High - ATR(22) * 3
* Chandelier Exit Short = 22-Period SMA Low + ATR(22) * 3
*
* @param highs high values.
* @param lows low values.
* @param closings closing values.
* @param config configuration.
* @return chandelier exit.
*/
export function ce(
highs: number[],
lows: number[],
closings: number[],
config: CEConfig = {}
): CEResult {
const { period } = { ...CEDefaultConfig, ...config };
const atrResult = atr(highs, lows, closings, { period });
const atrLine3 = multiplyBy(3, atrResult.atrLine);
const highestHigh = mmax(highs, { period });
const lowestLow = mmin(lows, { period });
const long = subtract(highestHigh, atrLine3);
const short = add(lowestLow, atrLine3);
return {
long,
short,
};
}
// Export full name
export { ce as chandelierExit };