From 4a076ddde7a7427e28e680aa7396567f7d415de8 Mon Sep 17 00:00:00 2001 From: 7400 <7400> Date: Sat, 2 Mar 2024 10:50:50 -0800 Subject: [PATCH] Fix #84, with one caveat --- src/charts/datafeed.js | 11 +++++------ src/charts/jBars.js | 28 +++++++++++++++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/charts/datafeed.js b/src/charts/datafeed.js index 378bc42..307ea7f 100644 --- a/src/charts/datafeed.js +++ b/src/charts/datafeed.js @@ -185,19 +185,18 @@ export default { const { from, to, firstDataRequest } = periodParams; console.log('[getBars]: Method call', symbolInfo, resolution, from, to); - try { - var bars = await jBars(from, to, resolution); // This is the one that does all the work + try { + let bars, metadata; + [bars, metadata] = await jBars(from, to, resolution); // This is the one that does all the work if (firstDataRequest) { lastBarsCache.set(symbolInfo.full_name, { ...bars[bars.length - 1], }); } - console.log(`[getBars]: returned ${bars.length} bar(s)`); + console.log(`[getBars]: returned ${bars.length} bar(s), and metadata ${metadata}`); console.log(bars); - onHistoryCallback(bars, { - noData: false, - }); + onHistoryCallback(bars, metadata); } catch (error) { console.log('[getBars]: Get error', error); onErrorCallback(error); diff --git a/src/charts/jBars.js b/src/charts/jBars.js index 669f892..1fd1509 100644 --- a/src/charts/jBars.js +++ b/src/charts/jBars.js @@ -44,7 +44,7 @@ export async function jBars (from, to, res) { const is_single_res = single_res.includes(res); const is_monthly_res = !is_single_res && !is_daily_res; - var bars = []; + let bars = []; for ( // Once around for each sample in from-to range let iDate = fromDate, @@ -78,9 +78,9 @@ export async function jBars (from, to, res) { 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)}`) + console.log(`Fetch: ${ohlc.length} resolution ${res} samples from ${url}`) + console.log(`first: ${new Date(ohlc[0][0]*1000).toUTCString()}`) + console.log(`last: ${new Date(ohlc[ohlc.length-1][0]*1000).toUTCString()}`) } else { console.log(`Fetch: file not found: ${url}`) ohlc = []; // no file, then empty data @@ -101,8 +101,9 @@ export async function jBars (from, to, res) { // 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 + // no ohlc sample file, or sample file exists and asking for sample beyond last sample, insert missing sample + const insert_missing_samples = false; const visible_missing_samples = false; if (ohlcDate == undefined) { bar = { @@ -114,7 +115,8 @@ export async function jBars (from, to, res) { high: 50, low: 0, close: 0, - }); + }); + if (!insert_missing_samples) bar = undefined; } // file exists, but ohlc sample not for this time, insert missing sample @@ -128,6 +130,7 @@ export async function jBars (from, to, res) { low: 0, close: 0, }); + if (!insert_missing_samples) bar = undefined; } // Copy ohlc sample @@ -156,5 +159,16 @@ export async function jBars (from, to, res) { } } - return bars; + // noData should be set only if no samples in interval and earlier. + // In our case, we are guaranteed to have contiguous samples. + // So we only return zero bars if: + // 1. interval is before first data available. + // 2. interval is after last data available. + // Returning no samples based on length works assuming that TV never asks for case 2. + // This is probably not a safe assumption. The alternative would be to search + // backward to find beginning of history. How far to search? + + let noData = bars.length == 0; + if (noData) console.log("noData == true!"); + return [bars, {noData}]; }