Published
Edited
Apr 23, 2020
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
getData = (data) => {
const o = data.map(v => +v.open)
const h = data.map(v => +v.high)
const c = data.map(v => +v.close)
const l = data.map(v => +v.low)
const v = data.map(v => +v.quoteAssetVolume)
const t = data.map(v => +v.trades)
return {o, h, c, l, v, t}
}
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
zscore = (data, n=6) => {
n = n - 1
const windows = data.map((v, i) => {
if(i > n){
return data.slice(i - n, i + 1)
}
return 0
})
return windows.map((v, i) => {
if(v){
let mean = d3.mean(v)
let std = d3.deviation(v)
// return data[i]
return (data[i] - mean) / std || 0
}
return 0
})
}
Insert cell
scaleWindows = (arr) => {
const windows = arr.map((v, i) => {
return sigmoid(zscore(arr.slice(i - 5, i + 1), 5).slice(-1)[0]) || 0
})
return windows.slice(6)
}
Insert cell
Insert cell
Insert cell
function average(arr){
return arr.reduce((p, n) => p + n, 0) / arr.length
}
Insert cell
function standardDeviation(values){
var avg = average(values);
var squareDiffs = values.map(function(value){
var diff = value - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
var avgSquareDiff = average(squareDiffs);

var stdDev = Math.sqrt(avgSquareDiff);
return stdDev;
}
Insert cell
test1 = {
const data = getData(btcRawData)
const close = data.c
const high = data.h
const low = data.l
const vol = data.v
const cum = (array) => array.reduce((a, x, i) => [...a, a.length > 0 ? x + a[i-1] : x], [])
const cumulative = array => {
return array.map((n, i, arr) => {
let j = i - 24 + 1
if(j > 0){
return arr.slice(j, i + 1).reduce((a, x, i) => [...a, a.length > 0 ? x + a[i-1] : x], []).slice(-1)[0]
}
return n
})
}
const VP = close.map((_, i) => {
const avgP = (high[i] + low[i] + close[i]) / 3
return (avgP * vol[i])
})
const cumVol = cumulative(vol)
const cumP = cumulative(VP)
const VWAP = cumP.map((c, i) => c / cumVol[i])
const VWAP_O = VWAP.map((n, i, arr) => {
let j = i - 24 + 1
if(j > 0){
let window = arr.slice(j, i + 1)
let mean = average(window)
let std = standardDeviation(window)
return [(VWAP[i] - mean) / std || 0, VWAP[i], close[i]]
}
return 0
})
return VWAP_O
}
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