Published
Edited
Jun 8, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
deribit = {
return {
perpetual_instrument: "BTC-PERPETUAL",
fetch: (url => fetch("https://www.deribit.com/api/v2" + url).then(r => r.json()).then(r => r.result)),
}
};
Insert cell
futures_instruments = deribit
.fetch("/public/get_instruments?currency=BTC&kind=future&expired=false")
// Convert the array to an object keyed on the instrument name
.then(is => is.reduce((acc, i) => { acc[i.instrument_name] = i; return acc; }, {}));
Insert cell
tickers = {
const retrieve_tickers = () => {
const url_base = "/public/ticker?instrument_name="
return Promise.all(
Object.values(futures_instruments).map(i => deribit.fetch(url_base + i.instrument_name))
).then(ts => {
return {
perpetual: ts.filter(t => t.instrument_name == deribit.perpetual_instrument)[0],
futures: ts.filter(t => t.instrument_name != deribit.perpetual_instrument),
}
})
}
// Update tickers every update_interval
return Generators.observe(notify => {
notify(retrieve_tickers());
let interval = setInterval(() => notify(retrieve_tickers()), update_interval);
return () => clearInterval(interval);
})
}
Insert cell
calc_basis_apr = (perp, future) => {
const days_in_year = 365.25
const now = new Date(Date.now());
const expiration_timestamp = futures_instruments[future.instrument_name].expiration_timestamp
const expiration = new Date(expiration_timestamp)
const price_delta = future.mark_price - perp.mark_price
const price_percent_change = price_delta / perp.mark_price
const date_days_delta = (expiration.getTime() - now.getTime()) / (24 * 60 * 60 * 1000)
const n = date_days_delta / days_in_year
const apy = ((future.mark_price / perp.mark_price - 1) / n) * 100

return {
instrument_name: future.instrument_name,
days_to_expiry: date_days_delta.toFixed(2),
apy: apy.toFixed(4),
perp_price: perp.mark_price,
future_price: future.mark_price,
percent_delta: (price_percent_change * 100).toFixed(2),
expiration_timestamp: expiration_timestamp,
}
}
Insert cell
table_data = {
return tickers.futures
.map(t => calc_basis_apr(tickers.perpetual, t))
.sort((a, b) => a.expiration_timestamp - b.expiration_timestamp)
}
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