-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from cinar:cinar/issue111
Volume Weighted Moving Average (VWMA)
- Loading branch information
Showing
11 changed files
with
184 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2022 Onur Cinar. All Rights Reserved. | ||
// https://github.com/cinar/indicatorts | ||
|
||
import { deepStrictEqual } from 'assert'; | ||
import { roundDigitsAll } from '../../helper/numArray'; | ||
import { defaultVwma, vwma } from './vwma'; | ||
|
||
describe('Volume Weighted Moving Average', () => { | ||
it('should be able to compute VWMA', () => { | ||
const closings = [20, 21, 21, 19, 16]; | ||
const volumes = [100, 50, 40, 50, 100]; | ||
const expected = [20, 20.33, 20.47, 20.29, 17.84]; | ||
const period = 3; | ||
|
||
const actual = vwma(period, closings, volumes); | ||
deepStrictEqual(roundDigitsAll(2, actual), expected); | ||
}); | ||
|
||
it('should be able to compute default', () => { | ||
const closings = [20, 21, 21, 19, 16]; | ||
const volumes = [100, 50, 40, 50, 100]; | ||
const expected = [20, 20.33, 20.47, 20.17, 18.94]; | ||
|
||
const actual = defaultVwma(closings, volumes); | ||
deepStrictEqual(roundDigitsAll(2, actual), expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2022 Onur Cinar. All Rights Reserved. | ||
// https://github.com/cinar/indicatorts | ||
|
||
import { divide, multiply } from '../../helper/numArray'; | ||
import { msum } from './msum'; | ||
|
||
/** Default VWMA period value. */ | ||
export const DEFAULT_VWMA_PERIOD = 20; | ||
|
||
/** | ||
* The vwma function calculates the Volume Weighted Moving Average (VWMA) | ||
* averaging the price data with an emphasis on volume, meaning areas | ||
* with higher volume will have a greater weight. | ||
* | ||
* VWMA = Sum(Price * Volume) / Sum(Volume) for a given Period. | ||
* | ||
* @param period period value. | ||
* @param closings asset closings. | ||
* @param volumes asset volumes. | ||
* @returns vwma values. | ||
*/ | ||
export function vwma( | ||
period: number, | ||
closings: number[], | ||
volumes: number[] | ||
): number[] { | ||
return divide( | ||
msum(period, multiply(closings, volumes)), | ||
msum(period, volumes) | ||
); | ||
} | ||
|
||
/** | ||
* The defaultVwma function calculates VWMA with a period of 20. | ||
* | ||
* @param closings asset closings. | ||
* @param volumes asset volumes. | ||
* @returns vwma values. | ||
*/ | ||
export function defaultVwma(closings: number[], volumes: number[]): number[] { | ||
return vwma(DEFAULT_VWMA_PERIOD, closings, volumes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2022 Onur Cinar. All Rights Reserved. | ||
// https://github.com/cinar/indicatorts | ||
|
||
import { deepStrictEqual } from 'assert'; | ||
import { Asset } from '../asset'; | ||
import { Action } from '../action'; | ||
import { wvmaStrategy } from './vwmaStrategy'; | ||
|
||
describe('VWMA stategy', () => { | ||
it('should be able to compute strategy', () => { | ||
const asset: Asset = { | ||
dates: [], | ||
openings: [], | ||
highs: [], | ||
lows: [], | ||
closings: [20, 21, 21, 19, 16], | ||
volumes: [100, 50, 40, 50, 100], | ||
}; | ||
const expected = [ | ||
Action.HOLD, | ||
Action.SELL, | ||
Action.SELL, | ||
Action.SELL, | ||
Action.SELL, | ||
]; | ||
const period = 3; | ||
|
||
const actual = wvmaStrategy(period, asset); | ||
deepStrictEqual(actual, expected); | ||
}); | ||
}); |
Oops, something went wrong.