-
-
Save easa/2d77437d059dca65e4ec485091fb745e 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment