Published
Edited
Nov 2, 2021
3 forks
8 stars
Insert cell
md`# Options Strategy Evaluation`
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
callPremiumReceived = chainByCallDelta.map(opt => Math.floor(callsSold * opt.callPrice))
Insert cell
netFuturePutValue = futurePutValue.map((value, i) => value - putPremiumSpent)
Insert cell
puts = ['PUTS', ..._.uniq(futureNetValueIndexed.flat().map(x => x.y))]
Insert cell
byPuts = {
const flattened = futureNetValueIndexed.flat()
const grouping = []
flattened.forEach((pair,i) => {
const idx = puts.findIndex(x => x == pair.y)
if (!grouping[idx]) {
grouping[idx] = [pair.y]
}
grouping[idx].push(pair.value.toLocaleString(undefined, { style: 'currency', currency: 'USD'}))
})
grouping[0] = calls
return grouping
}
Insert cell
calls = ['CALLS', ..._.uniq(futureNetValueIndexed.flat().map(x => x.x))]
Insert cell
futureNetValueIndexed = futureNetValue.map((callSeries, callIdx) => {
return callSeries.map((value, putIdx) => {
const call = chainByCallDelta[callIdx]
const put = chainByPutDelta[putIdx]
return {
x: `${call.strike}-STRIKE-${call.expiration}DTE-CALL`,
y: `${put.strike}-STRIKE-${put.expiration}DTE-PUT`,
value: `${value}`
}
})
})
Insert cell
putRows = futureNetValue.map((callSeries, callIdx) => {
return callSeries.map((value, putIdx) => {
const call = chainByCallDelta[callIdx]
const put = chainByPutDelta[putIdx]
return {
x: `${call.strike}-STRIKE-${call.expiration}DTE-CALL`,
y: `${put.strike}-STRIKE-${put.expiration}DTE-PUT`,
value: `${value}`
}
})
})
Insert cell
Insert cell
callsSold = {
const allocation = amountToHedge * (100-percentAllocation)/100
const ethAmount = Math.floor(allocation/Spot)
return ethAmount
}
Insert cell
Insert cell
putsBought = {
const allocation = amountToHedge * percentAllocation/100
const premium = Math.floor(allocation)
return chainByPutDelta.map(opt => Math.floor(premium / opt.put))
}
Insert cell
Insert cell
Insert cell
Insert cell
chainByOnlyPut = chainByPutDelta.map((c, i) => {
const { put, putDelta, strike, expiration } = c
const amountBought = putsBought[i]
const premiumAmt = amountBought * put
const premium = Math.round(premiumAmt).toLocaleString()
const futureAmt = futurePutValue[i]
const futureValue = futureAmt.toLocaleString()
const net = Math.round(futureAmt - premiumAmt).toLocaleString()
return {
putPrice: put,
putDelta,
strike,
expiration,
premium,
futureValue,
net
}
})
Insert cell
Insert cell
putDeltaIndex = {
return chains.map((e,i) => {
const optionToBuy = e.findIndex(x => x.putDelta <= -buyDelta/100)
return [i, optionToBuy]
})
}
Insert cell
chains = {
return EXPIRATIONS.map(e => {
return strikes.map(s => {
const time = e/365
const volatility = Volatility / 100
const callArgs = [Spot, s, time, volatility, RFR, 'call']
const putArgs = [Spot, s, time, volatility, RFR, 'put']
const callPrice = blackScholes.blackScholes(...callArgs)
const callDelta = greeks.getDelta(...callArgs)
const put = blackScholes.blackScholes(...putArgs)
const putDelta = greeks.getDelta(...putArgs)
return { callPrice, callDelta, put, putDelta, strike: s, expiration: e }
})
})
}
Insert cell
futureCallValue = futureChainCalls.map(({call}) => Math.floor(callsSold * call))
Insert cell
futurePutValue = futureChainPuts.map(({ put }, i) => Math.floor(putsBought[i] * put))
Insert cell
futureChainCalls = callDeltaIndex.map(([e,o]) => futureChains[e][o])
Insert cell
futureChainPuts = putDeltaIndex.map(([e,o]) => futureChains[e][o])
Insert cell
futureChains = {
return EXPIRATIONS.map(e => {
return strikes.map(s => {
const timeRemaining = e - daysIntoFuture
const time = timeRemaining <= 0 ? 0 : e/365
const volatility = futureVolatility / 100
const callArgs = [futureSpot, s, time, volatility, RFR, 'call']
const putArgs = [futureSpot, s, time, volatility, RFR, 'put']
const call = blackScholes.blackScholes(...callArgs)
const put = blackScholes.blackScholes(...putArgs)
return { call, put, strike: s, expiration: e }
})
})
}
Insert cell
Insert cell
dcc = 365
Insert cell
RFR = 0.01
Insert cell
Insert cell
Insert cell
greeks = require('https://bundle.run/greeks@1.0.0')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
treemap = data => d3.treemap()
.round(true)
.tile(d3.treemapSliceDice)
.size([
width - margin.left - margin.right,
height - margin.top - margin.bottom
])
(d3.hierarchy(
{
values: d3.nest()
.key(d => d.x)
.key(d => d.y)
.entries(data)
},
d => d.values
).sum(d => d.value))
.each(d => {
d.x0 += margin.left;
d.x1 += margin.left;
d.y0 += margin.top;
d.y1 += margin.top;
})
Insert cell
margin = ({top: 30, right: -1, bottom: -1, left: 1})
Insert cell
format = d => d.toLocaleString()
Insert cell
height = 954
Insert cell
color = d3.scaleLinear().domain(d3.extent(futureNetValue.flat())).range(['#d3e4ff','#2275f9'])
Insert cell
width = height
Insert cell
Insert cell
import { text, slider, radio, number } from '@jashkenas/inputs'
Insert cell
import { tile } from '@d3/treemap'
Insert cell
_ = require("lodash");
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