Fix #84, with one caveat

This commit is contained in:
7400
2024-03-02 10:50:50 -08:00
parent 6cecac77db
commit 4a076ddde7
2 changed files with 26 additions and 13 deletions

View File

@@ -185,19 +185,18 @@ export default {
const { from, to, firstDataRequest } = periodParams; const { from, to, firstDataRequest } = periodParams;
console.log('[getBars]: Method call', symbolInfo, resolution, from, to); 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) { if (firstDataRequest) {
lastBarsCache.set(symbolInfo.full_name, { lastBarsCache.set(symbolInfo.full_name, {
...bars[bars.length - 1], ...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); console.log(bars);
onHistoryCallback(bars, { onHistoryCallback(bars, metadata);
noData: false,
});
} catch (error) { } catch (error) {
console.log('[getBars]: Get error', error); console.log('[getBars]: Get error', error);
onErrorCallback(error); onErrorCallback(error);

View File

@@ -44,7 +44,7 @@ export async function jBars (from, to, res) {
const is_single_res = single_res.includes(res); const is_single_res = single_res.includes(res);
const is_monthly_res = !is_single_res && !is_daily_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 for ( // Once around for each sample in from-to range
let iDate = fromDate, let iDate = fromDate,
@@ -78,9 +78,9 @@ export async function jBars (from, to, res) {
let response = await fetch(url); let response = await fetch(url);
if (response.ok) { if (response.ok) {
ohlc = await response.json(); ohlc = await response.json();
console.log(`Fetch: ${ohlc.length} samples from ${url}`) console.log(`Fetch: ${ohlc.length} resolution ${res} samples from ${url}`)
console.log(`from: ${new Date(ohlc[0][0]*1000)}`) console.log(`first: ${new Date(ohlc[0][0]*1000).toUTCString()}`)
console.log(`to: ${new Date(ohlc[ohlc.length-1][0]*1000)}`) console.log(`last: ${new Date(ohlc[ohlc.length-1][0]*1000).toUTCString()}`)
} else { } else {
console.log(`Fetch: file not found: ${url}`) console.log(`Fetch: file not found: ${url}`)
ohlc = []; // no file, then empty data 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]+'Z');
let ohlcDate = iohlc >= ohlc.length ? undefined : new Date(ohlc[iohlc][0]*1000); 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; const visible_missing_samples = false;
if (ohlcDate == undefined) { if (ohlcDate == undefined) {
bar = { bar = {
@@ -115,6 +116,7 @@ export async function jBars (from, to, res) {
low: 0, low: 0,
close: 0, close: 0,
}); });
if (!insert_missing_samples) bar = undefined;
} }
// file exists, but ohlc sample not for this time, insert missing sample // 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, low: 0,
close: 0, close: 0,
}); });
if (!insert_missing_samples) bar = undefined;
} }
// Copy ohlc sample // 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}];
} }