-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathstochasticOscillatorStrategy.ts
42 lines (36 loc) · 1.06 KB
/
stochasticOscillatorStrategy.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { Asset } from '../asset';
import { Action } from '../action';
import {
StochConfig,
StochDefaultConfig,
stoch,
} from '../../indicator/momentum/stochasticOscillator';
/**
* Stochastic oscillator strategy function.
*
* @param asset asset object.
* @param config configuration.
* @return strategy actions.
*/
export function stochStrategy(
asset: Asset,
config: StochConfig = {}
): Action[] {
const strategyConfig = { ...StochDefaultConfig, ...config };
const result = stoch(asset.highs, asset.lows, asset.closings, strategyConfig);
const actions = new Array<Action>(result.k.length);
for (let i = 0; i < actions.length; i++) {
if (result.k[i] >= 80 && result.d[i] >= 80) {
actions[i] = Action.SELL;
} else if (result.k[i] <= 20 && result.d[i] <= 20) {
actions[i] = Action.BUY;
} else {
actions[i] = Action.HOLD;
}
}
return actions;
}
// Export full name
export { stochStrategy as stochasticOscillatorStrategy };