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,
}
}