forked from cinar/indicatorts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvwmaStrategy.ts
43 lines (37 loc) · 1.28 KB
/
vwmaStrategy.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { Asset } from '../asset';
import { Action } from '../action';
import { sma } from '../../indicator/trend/simpleMovingAverage';
import {
VWMAConfig,
VWMADefaultConfig,
vwma,
} from '../../indicator/trend/volumeWeightedMovingAverage';
/**
* The vwmaStrategy function uses SMA and VWMA indicators to provide
* a BUY action when VWMA is above SMA, and a SELL signal when VWMA
* is below SMA, a HOLD signal otherwse.
*
* @param asset asset object.
* @param config configuration.
* @returns strategy actions.
*/
export function vwmaStrategy(asset: Asset, config: VWMAConfig = {}): Action[] {
const strategyConfig = { ...VWMADefaultConfig, ...config };
const smaValues = sma(asset.closings, strategyConfig);
const vwmaValues = vwma(asset.closings, asset.volumes, strategyConfig);
const result = new Array<Action>(vwmaValues.length);
for (let i = 0; i < result.length; i++) {
if (vwmaValues[i] > smaValues[i]) {
result[i] = Action.BUY;
} else if (vwmaValues[i] < smaValues[i]) {
result[i] = Action.SELL;
} else {
result[i] = Action.HOLD;
}
}
return result;
}
// Export full name
export { vwmaStrategy as volumeWeightedMovingAverageStrategy };