Published
Edited
Sep 19, 2021
1 fork
1 star
Insert cell
md`# bitcoin historical data`
Insert cell
md`## y: ${percentOnYear} 6m: ${percentOnSixMonth}
## 3m: ${percentOnThreeMonth} 1m: ${percentOnMonth}
## w: ${percentOnWeek} w/7: ${(percentOnWeek / 7).toFixed(2)}
## d: ${percentOnDay} %`
Insert cell
hist = fetch('https://api.coindesk.com/v1/bpi/historical/close.json').then(resp => resp.json())
Insert cell
currentPrice = fetch('https://api.coindesk.com/v1/bpi/currentprice.json').then(resp => resp.json())
Insert cell
current = currentPrice.bpi.USD.rate_float
Insert cell
Object.keys(hist.bpi)[0]
Insert cell
today = new Date()
Insert cell
dateFns = await require('https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js');
Insert cell
yesterday = dateFns.subDays(today, 1)
Insert cell
weekAgo = dateFns.subDays(today, 7);
Insert cell
monthAgo = dateFns.subMonths(today, 1);
Insert cell
threeMonthAgo = dateFns.format(dateFns.subMonths(today, 3), 'YYYY-MM-DD')
Insert cell
sixMonthAgo = dateFns.format(dateFns.subMonths(today, 6), 'YYYY-MM-DD')
Insert cell
lastYear = dateFns.format(dateFns.subYears(today, 1), 'YYYY-MM-DD')
Insert cell
lastYearBpi = fetch(`https://api.coindesk.com/v1/bpi/historical/close.json?start=${lastYear}&end=${lastYear}`).then(resp => resp.json())
Insert cell
sixMonthBpi = fetch(`https://api.coindesk.com/v1/bpi/historical/close.json?start=${sixMonthAgo}&end=${sixMonthAgo}`).then(resp => resp.json())
Insert cell
threeMonthBpi = fetch(`https://api.coindesk.com/v1/bpi/historical/close.json?start=${threeMonthAgo}&end=${threeMonthAgo}`).then(resp => resp.json())
Insert cell
priceLastYear = lastYearBpi.bpi[lastYear]
Insert cell
priceSixMonthAgo = sixMonthBpi.bpi[sixMonthAgo]
Insert cell
priceThreeMonthAgo = threeMonthBpi.bpi[threeMonthAgo]
Insert cell
priceMonthAgo = hist.bpi[dateFns.format(monthAgo, 'YYYY-MM-DD')]
Insert cell
priceWeekAgo = hist.bpi[dateFns.format(weekAgo, 'YYYY-MM-DD')]
Insert cell
priceYesterday = hist.bpi[dateFns.format(yesterday, 'YYYY-MM-DD')]
Insert cell
md`percents`
Insert cell
percentOnYear = ((current - priceLastYear) / priceLastYear * 100).toFixed(2)
Insert cell
percentOnSixMonth = ((current - priceSixMonthAgo) / priceSixMonthAgo * 100).toFixed(2)
Insert cell
percentOnThreeMonth = ((current - priceThreeMonthAgo) / priceThreeMonthAgo * 100).toFixed(2)
Insert cell
percentOnDay = ((current - priceYesterday) / priceYesterday * 100).toFixed(2)
Insert cell
percentOnWeek = ((current - priceWeekAgo) / priceWeekAgo * 100).toFixed(2)
Insert cell
percentOnMonth = ((current - priceMonthAgo) / priceMonthAgo * 100).toFixed(2)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more