Published
Edited
Mar 2, 2020
1 star
Insert cell
md`# Portfolio`
Insert cell
Insert cell
Insert cell
portfolio = ({
stocks: [
{tick: 'ADBE', invested: 0, units: 0},
{tick: 'ADSK', invested: 0, units: 0},
{tick: 'SHOP', invested: 0, units: 0},
{tick: 'WDAY', invested: 0, units: 0},
{tick: 'AMD', invested: 0, units: 0},
{tick: 'V', invested: 0, units: 0},
{tick: 'FTNT', invested: 0, units: 0},
{tick: 'LULU', invested: 0, units: 0},
{tick: 'SPLK', invested: 0, units: 0},
{tick: 'TTD', invested: 0, units: 0},
// {tick: 'NVDA', invested: 1000, units: 4.09},
// {tick: 'PAYC', invested: 800, units: 2.67},
// {tick: 'UNH', invested: 1250, units: 4.44},
// {tick: 'VRTX', invested: 1250, units: 5.42},
// {tick: 'WORK', invested: 600, units: 21.62},
],
size: 25000,
risk: 0.0035
})
Insert cell
Insert cell
z.print(z.filter(r => r.diffCash > 50 || r.diffCash < -50, df))
Insert cell
Insert cell
Insert cell
df = {
let res = []

for(let symbol in prices) {
prices[symbol] = z.addCol('atr', calculateATR(prices[symbol], 21), prices[symbol])
const row = {
symbol,
...(z.tail(1, prices[symbol])[0]),
}

row.shares = ((portfolio.size * portfolio.risk) / row.atr).toFixed(2)
res.push(row)
}
res = z.addCol('sharedInvested', z.getCol('units', portfolio.stocks), res)
res = z.addCol('cashInvested', z.getCol('invested', portfolio.stocks), res)
res = z.addCol('diff', z.deriveCol((r) => (r.shares - r.sharedInvested).toFixed(2), res), res)
res = z.addCol('diffCash', z.deriveCol((r) => Math.round((r.diff * r.close) / 50) * 50, res), res)
res = z.pickCols(
['symbol', 'date', 'close', 'atr', 'shares', 'sharedInvested', 'diff', 'diffCash', 'cashInvested'],
res
)
res = z.sortByCol('diffCash', 'asc', res)

return res;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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