T-146: M2.3f — QuestDB bars_1m intraday reader for MarketDataStore.bars (timeframe: '1m'): Vault-fenced, parameterized planBars1mQuery + batch iterator over an injected PGWire query port in libs/research/src/market-data-store-questdb.ts (pure, no new deps, no live QuestDB)
This commit is contained in:
parent
adf28209c6
commit
2e5cc2e43d
2 changed files with 86 additions and 7 deletions
|
|
@ -265,10 +265,22 @@ function parseTimestampMicros(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function microsToEpochMs(us: bigint, field: 'ts' | 'known_at', rowIndex: number): number {
|
function microsToEpochMs(
|
||||||
|
us: bigint,
|
||||||
|
field: 'ts' | 'known_at',
|
||||||
|
rowIndex: number,
|
||||||
|
rounding: 'floor' | 'ceil'
|
||||||
|
): number {
|
||||||
const quotient = us / 1000n;
|
const quotient = us / 1000n;
|
||||||
const remainder = us % 1000n;
|
const remainder = us % 1000n;
|
||||||
const msBigInt = remainder < 0n ? quotient - 1n : quotient;
|
const msBigInt =
|
||||||
|
rounding === 'ceil'
|
||||||
|
? remainder > 0n
|
||||||
|
? quotient + 1n
|
||||||
|
: quotient
|
||||||
|
: remainder < 0n
|
||||||
|
? quotient - 1n
|
||||||
|
: quotient;
|
||||||
const ms = Number(msBigInt);
|
const ms = Number(msBigInt);
|
||||||
if (!Number.isSafeInteger(ms) || Math.abs(ms) > MAX_ECMASCRIPT_EPOCH_MS) {
|
if (!Number.isSafeInteger(ms) || Math.abs(ms) > MAX_ECMASCRIPT_EPOCH_MS) {
|
||||||
throw new MarketDataStoreValidationError(
|
throw new MarketDataStoreValidationError(
|
||||||
|
|
@ -350,14 +362,15 @@ function mapBars1mRows(
|
||||||
|
|
||||||
out[i] = {
|
out[i] = {
|
||||||
listingId,
|
listingId,
|
||||||
ts: microsToEpochMs(tsUs, 'ts', i),
|
ts: microsToEpochMs(tsUs, 'ts', i, 'floor'),
|
||||||
open: readRequiredFiniteNumber(raw, 'open', i),
|
open: readRequiredFiniteNumber(raw, 'open', i),
|
||||||
high: readRequiredFiniteNumber(raw, 'high', i),
|
high: readRequiredFiniteNumber(raw, 'high', i),
|
||||||
low: readRequiredFiniteNumber(raw, 'low', i),
|
low: readRequiredFiniteNumber(raw, 'low', i),
|
||||||
close: readRequiredFiniteNumber(raw, 'close', i),
|
close: readRequiredFiniteNumber(raw, 'close', i),
|
||||||
volume: readRequiredFiniteNumber(raw, 'volume', i),
|
volume: readRequiredFiniteNumber(raw, 'volume', i),
|
||||||
vwap: readNullableFiniteNumber(raw, 'vwap', i),
|
vwap: readNullableFiniteNumber(raw, 'vwap', i),
|
||||||
knownAt: microsToEpochMs(knownAtUs, 'known_at', i),
|
// Round sub-millisecond availability up so replay never treats a bar as known before arrival.
|
||||||
|
knownAt: microsToEpochMs(knownAtUs, 'known_at', i, 'ceil'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -404,7 +417,8 @@ export function createQuestDbIntradayBarsReader(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const batchSize = options.batchSize ?? DEFAULT_QUESTDB_BARS_BATCH_SIZE;
|
const batchSize =
|
||||||
|
options.batchSize === undefined ? DEFAULT_QUESTDB_BARS_BATCH_SIZE : options.batchSize;
|
||||||
if (typeof batchSize !== 'number' || !Number.isSafeInteger(batchSize) || batchSize < 1) {
|
if (typeof batchSize !== 'number' || !Number.isSafeInteger(batchSize) || batchSize < 1) {
|
||||||
throw new MarketDataStoreValidationError(
|
throw new MarketDataStoreValidationError(
|
||||||
`Invalid batchSize "${String(options.batchSize)}". Expected a positive safe integer.`,
|
`Invalid batchSize "${String(options.batchSize)}". Expected a positive safe integer.`,
|
||||||
|
|
|
||||||
|
|
@ -415,12 +415,12 @@ describe('Input validation and injection rejection', () => {
|
||||||
})
|
})
|
||||||
).toThrow(MarketDataStoreValidationError);
|
).toThrow(MarketDataStoreValidationError);
|
||||||
|
|
||||||
for (const badBatchSize of [0, -1, 1.5, Number.NaN, Number.POSITIVE_INFINITY]) {
|
for (const badBatchSize of [0, -1, 1.5, Number.NaN, Number.POSITIVE_INFINITY, null]) {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
createQuestDbIntradayBarsReader({
|
createQuestDbIntradayBarsReader({
|
||||||
executor,
|
executor,
|
||||||
vaultCutoffTs: VAULT_CUTOFF_TS,
|
vaultCutoffTs: VAULT_CUTOFF_TS,
|
||||||
batchSize: badBatchSize,
|
batchSize: badBatchSize as unknown as number,
|
||||||
})
|
})
|
||||||
).toThrow(MarketDataStoreValidationError);
|
).toThrow(MarketDataStoreValidationError);
|
||||||
}
|
}
|
||||||
|
|
@ -700,4 +700,69 @@ describe('Malformed and out-of-fence row defence-in-depth', () => {
|
||||||
expect(caught).toBeInstanceOf(MarketDataStoreValidationError);
|
expect(caught).toBeInstanceOf(MarketDataStoreValidationError);
|
||||||
expect(yielded).toHaveLength(0);
|
expect(yielded).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('rounds sub-millisecond known_at up so wider-window replay never exposes a bar before arrival', async () => {
|
||||||
|
// Matches the QuestDB storage fixture:
|
||||||
|
// ts = 2024-01-02T14:30:00.000000Z (1704205800000000 us)
|
||||||
|
// known_at = 2024-01-02T14:31:00.000700Z (1704205860000700 us)
|
||||||
|
const executor = new RecordingQuestDbExecutor();
|
||||||
|
executor.setRows([
|
||||||
|
{
|
||||||
|
listing_id: '1042',
|
||||||
|
ts_us: '1704205800000000',
|
||||||
|
open: 185.1,
|
||||||
|
high: 185.5,
|
||||||
|
low: 184.9,
|
||||||
|
close: 185.4,
|
||||||
|
volume: 12500,
|
||||||
|
vwap: 185.28,
|
||||||
|
known_at_us: '1704205860000700',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
listing_id: '1042',
|
||||||
|
ts_us: '1704205860000000',
|
||||||
|
open: 185.4,
|
||||||
|
high: 185.6,
|
||||||
|
low: 185.3,
|
||||||
|
close: 185.55,
|
||||||
|
volume: 8200,
|
||||||
|
vwap: null,
|
||||||
|
known_at_us: '1704205920000000', // exact millisecond: 2024-01-02T14:32:00.000000Z
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const reader = createQuestDbIntradayBarsReader({
|
||||||
|
executor,
|
||||||
|
vaultCutoffTs: VAULT_CUTOFF_TS,
|
||||||
|
});
|
||||||
|
|
||||||
|
const query: QuestDbBars1mQuery = {
|
||||||
|
listingIds: [1042],
|
||||||
|
timeframe: '1m',
|
||||||
|
from: '2024-01-02T14:30:00.000Z',
|
||||||
|
to: '2024-01-02T21:00:00.000Z',
|
||||||
|
asOf: '2024-01-02T21:00:00.000Z',
|
||||||
|
adjustment: 'raw',
|
||||||
|
};
|
||||||
|
|
||||||
|
const run1 = (await collectBatches(reader.bars(query))).flatMap(b => b.rows);
|
||||||
|
const run2 = (await collectBatches(reader.bars(query))).flatMap(b => b.rows);
|
||||||
|
expect(run1).toEqual(run2);
|
||||||
|
expect(run1).toHaveLength(2);
|
||||||
|
|
||||||
|
const decisionAt143100000 = Date.UTC(2024, 0, 2, 14, 31, 0, 0);
|
||||||
|
const decisionAt143100001 = Date.UTC(2024, 0, 2, 14, 31, 0, 1);
|
||||||
|
const exactKnownAt143200000 = Date.UTC(2024, 0, 2, 14, 32, 0, 0);
|
||||||
|
|
||||||
|
const subMsBar = run1[0]!;
|
||||||
|
expect(subMsBar.knownAt).toBe(decisionAt143100001);
|
||||||
|
// Ineligible at 14:31:00.000 UTC, eligible at 14:31:00.001 UTC.
|
||||||
|
expect(subMsBar.knownAt <= decisionAt143100000).toBe(false);
|
||||||
|
expect(subMsBar.knownAt <= decisionAt143100001).toBe(true);
|
||||||
|
|
||||||
|
// Exact whole-millisecond known_at is not pushed forward.
|
||||||
|
const exactMsBar = run1[1]!;
|
||||||
|
expect(exactMsBar.knownAt).toBe(exactKnownAt143200000);
|
||||||
|
expect(exactMsBar.knownAt <= exactKnownAt143200000).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue