Skip to content

Instantly share code, notes, and snippets.

@luan0ap
Last active April 6, 2022 16:45
Show Gist options
  • Save luan0ap/58ad674a8bb734a9d44fecf71dac7c8d to your computer and use it in GitHub Desktop.
Save luan0ap/58ad674a8bb734a9d44fecf71dac7c8d to your computer and use it in GitHub Desktop.
Calculate cryptocurrency average price from Binance API
function cryptoAveragePrice(binanceData) {
return binanceData.reduce((sum, { totalQuota, price }) => {
const totalBought = sum.totalBought + Number(totalQuota)
const totalPrice = sum.totalPrice + Number(totalQuota) * Number(price)
return {
totalBought,
totalPrice,
average: totalPrice / totalBought
}
}, { totalBought: 0, totalPrice: 0, average: 0 })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment