forked from cinar/indicatorts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolumeWeightedAveragePrice.ts
46 lines (42 loc) · 1.09 KB
/
volumeWeightedAveragePrice.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
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { divide, multiply } from '../../helper/numArray';
import { msum } from '../trend/msum';
/**
* Default period for VWAP.
*/
export const VWAP_DEFAULT_PERIOD = 14;
/**
* The Volume Weighted Average Price (VWAP) provides the average price
* the asset has traded.
*
* VWAP = Sum(Closing * Volume) / Sum(Volume)
*
* @param period window period.
* @param closings closing values.
* @param volumes volume values.
* @returns vwap values.
*/
export function volumeWeightedAveragePrice(
period: number,
closings: number[],
volumes: number[]
): number[] {
return divide(
msum(period, multiply(closings, volumes)),
msum(period, volumes)
);
}
/**
* Default volume weighted average price with period of 14.
*
* @param closings closing values.
* @param volumes volume values.
* @returns vwap values.
*/
export function defaultVolumeWeightedAveragePrice(
closings: number[],
volumes: number[]
): number[] {
return volumeWeightedAveragePrice(VWAP_DEFAULT_PERIOD, closings, volumes);
}