Published
Edited
Apr 15, 2020
Insert cell
Insert cell
//the data took forever for me to get in the right form. But I'm sure there is a simpler way of doing it.
Insert cell
Insert cell
SYMBOL
Insert cell
DT
Insert cell
Insert cell
SalesLog
Insert cell
optionGroup = d3
.groups(SalesLog, d => d.option_code)
.sort((a, b) => d3.ascending(a[1][0].expiry, b[1][0].expiry))
Insert cell
OptionTrades = [...optionGroup].map((x, i) => {
let name = x[0];

let aggregateMinute = d3.groups(x[1], d => +d.DT);

return aggregateMinute.map((m, j) => {
let date = new Date(m[0]);
let category = m[1][0].option_type;
let expiry = m[1][0].expiry;
///let value = d3.sum(m[1], minuteAggregation);
let value = Separately_Summed[i][j];
return { name, date, category, expiry, value };
});

//value, QTY, PRICE, OPTION_CODE.label, OPTION_CODE.expiry,
})
Insert cell
marketMinutes = {
let start = +new Date(DT + " 9:30"),
end = +new Date(DT + " 16:00");
let time = start;
let marketMinutes = [];
while (time <= end) {
marketMinutes.push(time);
time += 60000 * 30;
}
return marketMinutes.map(x => new Date(x));
}
Insert cell
{
let option = OptionTrades[0];
let trades = option;
let firstTrade = trades[0];
let atOpen = { ...firstTrade, value: 0, date: marketMinutes[0] };
// return atOpen;
// name: ".IBM191025C110"
// date: 2019-10-25T11:53
// category: "C"
// expiry: 2019-10-25
// value: 258
// let minutes = marketMinutes;
return marketMinutes.map(m => {
//are there trades for this minute?
let it = { ...atOpen, date: m };
let match = dateBisector.left(trades, m);
if (match > 0) return { ...trades[match - 1], date: m };
else return it;
// if(it.date===firstTrade)
});
}
Insert cell
OptionMinutes = OptionTrades.map(option => {
let trades = option;
let firstTrade = trades[0];
let atOpen = { ...firstTrade, value: 0, date: marketMinutes[0] };
// return atOpen;
// name: ".IBM191025C110"
// date: 2019-10-25T11:53
// category: "C"
// expiry: 2019-10-25
// value: 258
// let minutes = marketMinutes;
return marketMinutes.map(m => {
//are there trades for this minute?
let it = { ...atOpen, date: m };
let match = dateBisector.left(trades, m);
if (match > 0) return { ...trades[match - 1], date: m };
else return it;
// if(it.date===firstTrade)
});
})
Insert cell
OptionMinutes.map(x => {
let withoutNames = x.find(x => !x.name);
if (withoutNames) return withoutNames;
else return false;
})
Insert cell
data = d3.merge(OptionMinutes)
Insert cell
// data = d3
// .merge(
// [...optionGroup].map((x, i) => {
// let name = x[0];

// let aggregateMinute = d3.groups(x[1], d => +d.DT);

// return aggregateMinute.map((m, j) => {
// let date = new Date(m[0]);
// let category = m[1][0].option_type;
// let expiry = m[1][0].expiry;
// ///let value = d3.sum(m[1], minuteAggregation);
// let value = Separately_Summed[i][j];
// return { name, date, category, expiry, value };
// });

// //value, QTY, PRICE, OPTION_CODE.label, OPTION_CODE.expiry,
// })
// )
// .sort((a, b) => d3.ascending(a.DT, b.DT))
Insert cell
//Maybe this is a good use case for cumsum. maybe it's not the best. I just felt pleased to try it
Insert cell
Insert cell
// dateBisector2 = d3.bisector(d => d.DT)
Insert cell
dateBisector.left(
SalesLog,
SalesLog[Math.floor(SalesLog.length * Math.random())].date
)
Insert cell
// optionGroup.map((x, i) => {
// let name = x[0];

// let aggregateMinute = d3.groups(x[1], d => +d.DT);

// return aggregateMinute.map((m, j) => {
// let date = new Date(m[0]);
// let category = m[1][0].option_type;
// let expiry = m[1][0].expiry;
// ///let value = d3.sum(m[1], minuteAggregation);
// let value = Separately_Summed[i][j];
// return { name, date, category, expiry, value };
// });

// //value, QTY, PRICE, OPTION_CODE.label, OPTION_CODE.expiry,
// })
Insert cell
minuteAggregation = d => +d.qty * +d.price
Insert cell
Insert cell
Insert cell
Insert cell
dateBisector = d3.bisector(d => d.date)
Insert cell
Insert cell
// dateBisector.left(fullData, +data[0].DT)
Insert cell
// d3.merge(
// fullData.map(f => {
// let tradeInfo = data.filter(x => x.name === f[1] && +f[0] === +x.date);
// if (!tradeInfo) return null;
// return tradeInfo;
// })
// )
Insert cell
// d3.bisect(fullData, d => +d[0] === +data[0].date)
Insert cell
// d3.bisect(fullData, d => d[1] === data[0].name)
Insert cell
// fullData = d3.cross(datevalues.map(x => x[0]), names)
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
keyframes = {
const _keyframes = [];
let ka, a, kb, b;
for ([[ka, a], [kb, b]] of d3.pairs(datevalues)) {
for (let i = 0; i < k; ++i) {
const t = i / k;
_keyframes.push([
new Date(ka * (1 - t) + kb * t),
rank(name => a.get(name) * (1 - t) + b.get(name) * t)
]);
}
}
_keyframes.push([new Date(kb), rank(name => b.get(name))]);
return _keyframes;
}
Insert cell
Insert cell
Insert cell
dateFrames = d3.groups(keyframes.flatMap(([, data]) => data), d => +d.date)
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 { SalesLog, SYMBOL, DT } from '@stroked/ibm'
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