-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
kdjStrategy.ts
39 lines (35 loc) · 1000 Bytes
/
kdjStrategy.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { Asset } from '../asset';
import { Action } from '../action';
import {
KDJConfig,
KDJDefaultConfig,
kdj,
} from '../../indicator/trend/randomIndex';
/**
* KDJ strategy.
* @param asset asset object.
* @param config configuration.
* @return strategy actions.
*/
export function kdjStrategy(asset: Asset, config: KDJConfig = {}): Action[] {
const strategyConfig = { ...KDJDefaultConfig, ...config };
const kdjResult = kdj(
asset.highs,
asset.lows,
asset.closings,
strategyConfig
);
const actions = new Array<Action>(kdjResult.k.length);
for (let i = 0; i < actions.length; i++) {
if (kdjResult.k[i] > kdjResult.d[i] && kdjResult.k[i] <= 20) {
actions[i] = Action.BUY;
} else if (kdjResult.k[i] < kdjResult.d[i] && kdjResult.k[i] >= 80) {
actions[i] = Action.SELL;
} else {
actions[i] = Action.HOLD;
}
}
return actions;
}