Public
Edited
Feb 5
Insert cell
Insert cell
Insert cell
stocks = (await Promise.all([
FileAttachment("aapl.csv").csv({typed: true}).then((values) => ["AAPL", values]),
FileAttachment("amzn.csv").csv({typed: true}).then((values) => ["AMZN", values]),
FileAttachment("goog.csv").csv({typed: true}).then((values) => ["GOOG", values]),
FileAttachment("ibm.csv").csv({typed: true}).then((values) => ["IBM", values]),
])).flatMap(([Symbol, values]) => values.map(d => ({Symbol, ...d})))
Insert cell
Insert cell
function ewma(alpha, index1=0) {
return (x) => {
let p = x[0]/x[index1];
return x.map((c) => (p = p * (1 - alpha) + (c / x[index1]) * alpha));
};
}
Insert cell
Insert cell
Insert cell
Plot.plot({
color: { legend: true },
y: { type: "log" },
marks: [
Plot.lineY(
stocks,
Plot.map({ y: ewma(alpha, anchor) }, { x: "Date", y: "Close", stroke: "Symbol" })
)
]
})
Insert cell
Insert cell
ewma(0.5)([2,2,3,2,1])
Insert cell
//pivot(stocks, ['Symbol', 'Close'], 'Symbol', 'Close')
Insert cell
Insert cell
pivot = d3.flatRollup(stocks, v => Object.fromEntries(v.map(d => d.Symbol).map((d,i) => [d, v.map(d => d.Close)[i]])), d => d.Date).map(d => ({ Date: d[0], ...d[1] }))
Insert cell
Plot.plot({
color: { legend: true },
y: { type: "log" },
marks:
["AAPL", "GOOG", "AMZN", "IBM"].map(d =>
Plot.lineY(pivot,
Plot.normalizeY( "last", { x: "Date", y: d, stroke: e => e.stroke=d })))
})
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