historical ohlc support, edit tvWidget to enable.

This commit is contained in:
7400
2024-02-08 12:32:44 -08:00
parent 5465efbd86
commit 4ab0cc9659
4 changed files with 277 additions and 0 deletions

107
src/charts/jBars.js Normal file
View File

@@ -0,0 +1,107 @@
export async function jBars (from, to, res) {
console.log('[jBars]: Method call', res, from, to);
var fromDate = new Date(from*1000);
var toDate = new Date(to*1000);
console.log("fromDate:", fromDate.toUTCString());
console.log("toDate: ", toDate.toUTCString());
const contract = "0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443";
// check parameters
if (res != "1D") throw Error("Only 1D resolution currently supported");
console.assert(fromDate.getUTCHours() == 0, "hours should be zero");
console.assert(fromDate.getUTCMinutes() == 0, "minutes should be zero");
console.assert(fromDate.getUTCSeconds() == 0, "seconds should be zero");
console.assert(fromDate.getUTCMilliseconds() == 0, "milliseconds should be zero");
// Spoof data
var spoof;
{
const yr = "2022";
const mo = "01";
const url = `/ohlc/42161/${contract}/${res}/${yr}/${contract}-${res}-${yr}${mo}.json`
const response = await fetch(url);
spoof = await response.json();
}
var bars = [];
for ( // Once around for each sample in from-to range
let iDate = fromDate,
// loop state
iMonth = -1,
iolhc = 0,
ohlc;
iDate < toDate;
iDate.setUTCDate(iDate.getUTCDate()+1)
) {
let bar = undefined;
// Fetch one sample file as needed
if (iMonth != iDate.getUTCMonth()) {
const yr = iDate.getUTCFullYear();
const mo = String(iDate.getUTCMonth()+1).padStart(2, '0');
const url = `/ohlc/42161/${contract}/${res}/${yr}/${contract}-${res}-${yr}${mo}.json`
let response = await fetch(url);
if (response.ok) {
ohlc = await response.json();
} else {
ohlc = []; // no file, then empty data
}
iMonth = iDate.getUTCMonth();
iolhc = 0;
}
let ohlcDate = iolhc >= ohlc.length ? undefined : new Date(ohlc[iolhc][0]+'Z');
// no ohlc sample, insert a visible sample
if (ohlcDate == undefined) {
bar = {
time: iDate.getTime(),
open: 50,
high: 50,
low: 0,
close: 0,
}
}
// ohlc sample not for this time, insert invisible sample
else if ( iDate.getTime() != ohlcDate.getTime() ) {
bar = {
time: iDate.getTime(),
// open: 100,
// high: 100,
// low: 0,
// close: 0,
}
// Copy ohlc sample
} else {
bar = {
time: iDate.getTime(),
open: ohlc[iolhc][1],
high: ohlc[iolhc][2],
low: ohlc[iolhc][3],
close: ohlc[iolhc][4],
}
iolhc++;
}
if (bar==undefined) throw "bar==undefined";
bars.push(bar);
}
return bars;
}