Skip to content

Instantly share code, notes, and snippets.

@shanefontaine
Last active April 3, 2024 16:54
Show Gist options
  • Save shanefontaine/d252f59d4c3180dd5a4623f22c7a495b to your computer and use it in GitHub Desktop.
Save shanefontaine/d252f59d4c3180dd5a4623f22c7a495b to your computer and use it in GitHub Desktop.
Time weighted average price calc
async function main () {
const startTimestamp = 1672560000
const endTimestamp = 1688108400
const prices = await getPrices(startTimestamp, endTimestamp)
let total = 0
for (const price of prices) {
total += price[1]
}
const twap = total / prices.length
console.log(`TWAP: ${twap}`)
}
async function getPrices(startTimestamp: number, endTimestamp: number) {
const baseUrl = 'https://api.coingecko.com/api/v3/coins/hop-protocol/market_chart/range?vs_currency=usd&'
const url = `${baseUrl}from=${startTimestamp}&to=${endTimestamp}`
const res = await fetch(url)
const json = await res.json()
return json.prices
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment