Published
Edited
May 16, 2019
Insert cell
Insert cell
Insert cell
{
const button = DOM.download(serialize(dailyPrices[selectedIdx]), null, "Download Price Data as CSV");
button.style.position = 'relative';
return button;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mkrRollingBeta = {
return dailyCloses[selectedIdx].map((close, idx) => {
let cleaned = dailyPercentageChanges[selectedIdx]
let btc = btcPercentChange.slice(-cleaned.length)
if (idx < betaLookback) return 0
let startIdx = idx - betaLookback
return calculateBeta(cleaned.slice(startIdx, idx), btc.slice(startIdx, idx))
})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mkrForwardChange = getForwardChange(dailyPercentageChanges[selectedIdx], dailyCloses[selectedIdx])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mkrForwardChangeClean = betaChart.map(data => [data.beta, data.forwardChange]).filter(x => !isNaN(x[0]))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {batExpectedReturn} from '@bgits/crypto-protocol-expected-returns'
Insert cell
Insert cell
Insert cell
Insert cell
calculatePortfolios = (syms = []) => {
const coorelations = coorelationMatrix(syms)
const weights = combinationSum(syms.length, 100).map(x => x.map(x => x/100))
const returns = weights.map(comb => simpleStatistics.sum(comb.map((weight,i) => weight * forwardExpectedReturn[syms[i]])))
//TODO look into use PMPT - sortino/ downside volatility only
const stds = syms.map(ticker => {
const pctChanges = getSymbolPercentageChanges(ticker).slice(-sharpeLookback)
const std = simpleStatistics.standardDeviation(pctChanges)
return std
})
const weightedStds = weights.map(comb => simpleStatistics.sum(comb.map((weight, i) => (weight * stds[i])**2)))
const weightedCors = weights.map(weights => {
let cors = []
for(let i=0;i<syms.length;i++){
for(let j=i+1;j<syms.length;j++){
const first = syms[i]
const second = syms[j]
const key = `${first}_${second}`
const cor = coorelations[key]
const result = 2*weights[i]*weights[j]*stds[i]*stds[j]*cor
cors.push(result)
}
}
return simpleStatistics.sum(cors)
})
const portfolioStds = weightedStds.map((std, i) => Math.sqrt(std + weightedCors[i]))
const details = weights
.map(w => w.map((w, i) => `${syms[i]}: ${w}`))
.map(w => w.reduce((pv,cv) => `${pv} ${cv}`))
.map((w,i) => `${w} sharpe: ${(returns[i] - riskFreeRate) / portfolioStds[i]}`)
const riskReturns = portfolioStds.map((p,i) => ({ portfolioStd: p, returns: returns[i], details: details[i]}))
return { weights,returns, portfolioStds, stds, weightedStds, coorelations, weightedCors, riskReturns, details }
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {serialize} from "@palewire/saving-csv"
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