Skip to content

Instantly share code, notes, and snippets.

@brianmcallister
Last active August 25, 2021 16:55
Show Gist options
  • Save brianmcallister/d01ce191a43df4fbeb3caaea7d79cd6c to your computer and use it in GitHub Desktop.
Save brianmcallister/d01ce191a43df4fbeb3caaea7d79cd6c to your computer and use it in GitHub Desktop.
VWAP (Volume Weighted Average Price) function.
/**
* 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;
}
@brianmcallister
Copy link
Author

brianmcallister commented Aug 12, 2016

@fcantelmi
Copy link

This calculation looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment