All time selectors work for OHLC
This commit is contained in:
@@ -10,97 +10,136 @@ export async function jBars (from, to, res) {
|
||||
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");
|
||||
// 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 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();
|
||||
// }
|
||||
|
||||
const file_res = ['1m', '3m', '5m', '10m', '15m', '30m', '1H', '2H', '4H', '8H', '12H', '1D', '2D', '3D', '1W',];
|
||||
const supported_res = ['1', '3', '5', '10', '15', '30', '60', '120', '240', '480', '720', '1D', '2D', '3D', '1W',];
|
||||
const daily_res = ['1', '3', '5', '10', '15', '30'];
|
||||
const single_res = ['1W'];
|
||||
|
||||
if (!supported_res.includes(res)) throw Error(`resolution ${res} not supported.`);
|
||||
|
||||
const is_daily_res = daily_res.includes(res);
|
||||
const is_single_res = single_res.includes(res);
|
||||
const is_monthly_res = !is_single_res && !is_daily_res;
|
||||
|
||||
var bars = [];
|
||||
|
||||
for ( // Once around for each sample in from-to range
|
||||
let iDate = fromDate,
|
||||
// loop state
|
||||
iMonth = -1,
|
||||
iolhc = 0,
|
||||
iFile = undefined,
|
||||
iohlc = 0,
|
||||
ohlc;
|
||||
iDate < toDate;
|
||||
iDate.setUTCDate(iDate.getUTCDate()+1)
|
||||
) {
|
||||
|
||||
let bar = undefined;
|
||||
|
||||
// Fetch one sample file as needed
|
||||
|
||||
if (iFile == undefined ? true :
|
||||
is_monthly_res ? iFile.getUTCMonth() != iDate.getUTCMonth() :
|
||||
is_daily_res ? iFile.getUTCDate() != iDate.getUTCDate() :
|
||||
false // is_single_res
|
||||
) {
|
||||
|
||||
if (iMonth != iDate.getUTCMonth()) {
|
||||
const fres = file_res[supported_res.indexOf(res)]
|
||||
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`
|
||||
const yrdir = is_single_res ? "" : `/${yr}`;
|
||||
const mo = String(iDate.getUTCMonth()+1).padStart(2, '0'); // January is month 0 in Date object
|
||||
const date = is_daily_res ? String(iDate.getUTCDate()).padStart(2, '0') : "";
|
||||
const yrmo = !is_single_res ? `-${yr}${mo}` : "";
|
||||
|
||||
let url = `/ohlc/42161/${contract}/${fres}${yrdir}/${contract}-${fres}${yrmo}${date}.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;
|
||||
iFile = new Date(iDate);
|
||||
iohlc = 0;
|
||||
}
|
||||
|
||||
let ohlcDate = iolhc >= ohlc.length ? undefined : new Date(ohlc[iolhc][0]+'Z');
|
||||
// Skip samples not for our time
|
||||
|
||||
// no ohlc sample, insert a visible sample
|
||||
for(; iohlc < ohlc.length; iohlc++ ) {
|
||||
if ( new Date(ohlc[iohlc][0]+'Z').getTime() >= iDate.getTime() ) break;
|
||||
}
|
||||
|
||||
let ohlcDate = iohlc >= ohlc.length ? undefined : new Date(ohlc[iohlc][0]+'Z');
|
||||
|
||||
// no ohlc sample file, insert missing sample
|
||||
|
||||
const visible_missing_samples = true;
|
||||
if (ohlcDate == undefined) {
|
||||
bar = {
|
||||
time: iDate.getTime(),
|
||||
open: 50,
|
||||
high: 50,
|
||||
low: 0,
|
||||
close: 0,
|
||||
}
|
||||
if (visible_missing_samples) bar = Object.assign(bar, {
|
||||
// Comment these to make invisible
|
||||
open: 50,
|
||||
high: 50,
|
||||
low: 0,
|
||||
close: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// ohlc sample not for this time, insert invisible sample
|
||||
// file exists, but ohlc sample not for this time, insert missing sample
|
||||
|
||||
else if ( iDate.getTime() != ohlcDate.getTime() ) {
|
||||
|
||||
bar = {
|
||||
time: iDate.getTime(),
|
||||
// open: 100,
|
||||
// high: 100,
|
||||
// low: 0,
|
||||
// close: 0,
|
||||
}
|
||||
if (visible_missing_samples) bar = Object.assign(bar, {
|
||||
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],
|
||||
open: ohlc[iohlc][1] ?? ohlc[iohlc][4], // open
|
||||
high: ohlc[iohlc][2] ?? ohlc[iohlc][4], // high
|
||||
low: ohlc[iohlc][3] ?? ohlc[iohlc][4], // low
|
||||
close: ohlc[iohlc][4], // close
|
||||
}
|
||||
iolhc++;
|
||||
iohlc++;
|
||||
}
|
||||
|
||||
if (bar==undefined) throw "bar==undefined";
|
||||
bars.push(bar);
|
||||
if (bar != undefined) bars.push(bar);
|
||||
|
||||
// Increment loop variable
|
||||
|
||||
if (supported_res.indexOf(res)<supported_res.indexOf('1D')) { // <1day
|
||||
const mins = parseInt(res);
|
||||
iDate.setUTCMinutes(iDate.getUTCMinutes()+mins)
|
||||
} else if (res=='1W') { // 1W
|
||||
iDate.setUTCDate(iDate.getUTCDate()+7);
|
||||
} else { // <1W >1day
|
||||
iDate.setUTCDate(iDate.getUTCDate()+1);
|
||||
}
|
||||
}
|
||||
|
||||
return bars;
|
||||
|
||||
Reference in New Issue
Block a user