Datafeed limping
This commit is contained in:
@@ -2,29 +2,36 @@ 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);
|
||||
|
||||
var fromDate = new Date(from*1000);
|
||||
if (res=="1W") { // for 1W, day must be Sunday
|
||||
const day = fromDate.getUTCDay(); // 0<=day<7
|
||||
fromDate.setDate(fromDate.getDate() + (7-day)%7 );
|
||||
}
|
||||
|
||||
// Set fromDate to be compatible with Tim's datafiles.
|
||||
// This potentially increases number of samples returned.
|
||||
|
||||
if (res.endsWith("W") || res.endsWith("D")) { // Days/Weeks -- set to midnight
|
||||
fromDate.setUTCHours(0, 0, 0);
|
||||
} else {
|
||||
let minutesRes = parseInt(res);
|
||||
if (minutesRes >= 60) { // Hours
|
||||
let hoursRes = Math.floor(minutesRes/60);
|
||||
let fromHours = fromDate.getUTCHours();
|
||||
fromDate.setUTCHours(fromHours - fromHours % hoursRes, 0, 0, 0);
|
||||
} else { // Minutes
|
||||
let fromMinutes = fromDate.getUTCMinutes();
|
||||
fromDate.setUTCMinutes(fromMinutes - fromMinutes % minutesRes, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("fromDate:", fromDate.toUTCString());
|
||||
console.log("toDate: ", toDate.toUTCString());
|
||||
|
||||
const contract = "0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443";
|
||||
|
||||
// check parameters
|
||||
// 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();
|
||||
// }
|
||||
// const contract = "0xC6962004f452bE9203591991D15f6b388e09E8D0";
|
||||
|
||||
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',];
|
||||
@@ -64,13 +71,18 @@ export async function jBars (from, to, res) {
|
||||
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}` : "";
|
||||
const server = "https://alpha.dexorder.trade"
|
||||
|
||||
let url = `/ohlc/42161/${contract}/${fres}${yrdir}/${contract}-${fres}${yrmo}${date}.json`;
|
||||
let url = `${server}/ohlc/42161/${contract}/${fres}${yrdir}/${contract}-${fres}${yrmo}${date}.json`;
|
||||
|
||||
let response = await fetch(url);
|
||||
if (response.ok) {
|
||||
ohlc = await response.json();
|
||||
console.log(`Fetch: ${ohlc.length} samples from ${url}`)
|
||||
console.log(`from: ${new Date(ohlc[0][0]*1000)}`)
|
||||
console.log(`to: ${new Date(ohlc[ohlc.length-1][0]*1000)}`)
|
||||
} else {
|
||||
console.log(`Fetch: file not found: ${url}`)
|
||||
ohlc = []; // no file, then empty data
|
||||
}
|
||||
iFile = new Date(iDate);
|
||||
@@ -80,14 +92,18 @@ export async function jBars (from, to, res) {
|
||||
// Skip samples not for our time
|
||||
|
||||
for(; iohlc < ohlc.length; iohlc++ ) {
|
||||
if ( new Date(ohlc[iohlc][0]+'Z').getTime() >= iDate.getTime() ) break;
|
||||
// if ( new Date(ohlc[iohlc][0]+'Z').getTime() >= iDate.getTime() ) break;
|
||||
if ( ohlc[iohlc][0]*1000 >= iDate.getTime() ) break;
|
||||
}
|
||||
|
||||
let ohlcDate = iohlc >= ohlc.length ? undefined : new Date(ohlc[iohlc][0]+'Z');
|
||||
// console.log(`iohlc: ${iohlc}`);
|
||||
|
||||
// let ohlcDate = iohlc >= ohlc.length ? undefined : new Date(ohlc[iohlc][0]+'Z');
|
||||
let ohlcDate = iohlc >= ohlc.length ? undefined : new Date(ohlc[iohlc][0]*1000);
|
||||
|
||||
// no ohlc sample file, insert missing sample
|
||||
|
||||
const visible_missing_samples = true;
|
||||
const visible_missing_samples = false;
|
||||
if (ohlcDate == undefined) {
|
||||
bar = {
|
||||
time: iDate.getTime(),
|
||||
@@ -102,9 +118,7 @@ export async function jBars (from, to, res) {
|
||||
}
|
||||
|
||||
// file exists, but ohlc sample not for this time, insert missing sample
|
||||
|
||||
else if ( iDate.getTime() != ohlcDate.getTime() ) {
|
||||
|
||||
bar = {
|
||||
time: iDate.getTime(),
|
||||
}
|
||||
@@ -114,10 +128,10 @@ export async function jBars (from, to, res) {
|
||||
low: 0,
|
||||
close: 0,
|
||||
});
|
||||
}
|
||||
|
||||
// Copy ohlc sample
|
||||
|
||||
} else {
|
||||
else {
|
||||
bar = {
|
||||
time: iDate.getTime(),
|
||||
open: ohlc[iohlc][1] ?? ohlc[iohlc][4], // open
|
||||
|
||||
Reference in New Issue
Block a user