Last active
August 25, 2021 16:55
-
-
Save brianmcallister/d01ce191a43df4fbeb3caaea7d79cd6c to your computer and use it in GitHub Desktop.
VWAP (Volume Weighted Average Price) function.
This file contains hidden or 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
/** | |
* Calculate VWAP (volume weighted average price) for a list of bids. | |
* | |
* @param {number} total - Total shares bought. | |
* @param {Array<number, number>} positions - Tuples of position data. The first | |
* index is the amount of shares, the | |
* second index is the price of the | |
* shares. | |
* | |
* @example | |
* | |
* vwap(10, [2.5, 268], [7.5, 269]); | |
* #=> 268.75 | |
* | |
* @returns {number} Weighted VWAP amount. | |
*/ | |
const vwap = (total: number, ...positions: Array<Array<number>>): number => { | |
return positions.reduce((acc, next) => acc + (next[0] * next[1]), 0) / total; | |
} |
This calculation looks good to me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage