-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
volumeWeightedAveragePrice.ts
47 lines (41 loc) · 1.04 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
47
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { divide, multiply } from '../../helper/numArray';
import { msum } from '../trend/movingSum';
/**
* Optional configuration of VWAP parameters.
*/
export interface VWAPConfig {
period?: number;
}
/**
* The default configuration of VWAP.
*/
export const VWAPDefaultConfig: Required<VWAPConfig> = {
period: 14,
};
/**
* The Volume Weighted Average Price (VWAP) provides the average price
* the asset has traded.
*
* VWAP = Sum(Closing * Volume) / Sum(Volume)
*
* @param closings closing values.
* @param volumes volume values.
* @param config configuration.
* @returns vwap values.
*/
export function vwap(
closings: number[],
volumes: number[],
config: VWAPConfig = {}
): number[] {
const { period } = { ...VWAPDefaultConfig, ...config };
const result = divide(
msum(multiply(closings, volumes), { period }),
msum(volumes, { period })
);
return result;
}
// Export full name
export { vwap as volumeWeightedAveragePrice };