-
-
Save shanefontaine/d252f59d4c3180dd5a4623f22c7a495b to your computer and use it in GitHub Desktop.
Time weighted average price calc
This file contains 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
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