stock-bot/libs/data/questdb
2026-12-02 01:03:29 -05:00
..
src T-154: M2.3l — libs/data/questdb: pure TypeScript ILP v2 batch writer for bars_1m / fx_1m (ilp-v2-writer.ts, binary doubles + microsecond timestamps over an injected sink, byte-parity with tools/curate-etl/src/ilp.rs; no new deps, no live QuestDB) 2026-12-02 00:57:13 -05:00
test T-154: tests 2026-12-02 01:03:29 -05:00
bunfig.toml moved folders around 2025-06-21 18:27:00 -04:00
package.json refactor: pre-M0 cleanup, core library hardening, and isolated dev/QA Docker stack 2026-11-27 00:11:50 -05:00
README.md T-135: M2.3b — libs/data/questdb query layer for QuestDB 10.2.0: LATEST ON + parameterized PGWire queries + contract DDL for bars_1m / fx_1m / bar_revisions (pure, no live QuestDB) 2026-12-01 06:29:20 -05:00
tsconfig.json commit 2 refactor 2026-10-20 14:35:45 -04:00

QuestDB Client Library

A QuestDB 10.2.0 client library for the Stock Bot trading platform, optimized for time-series data, parameterized PGWire queries, and intraday bar storage.

Features

  • Time-Series Optimized: Built for QuestDB 10.2.0 (LATEST ON, SAMPLE BY, ASOF JOIN)
  • Parameterized PGWire Queries: All value filters bind $1..$n placeholders; structured identifiers and intervals are strictly validated
  • Contract DDL: Offline DDL generator for M2 intraday tables (bars_1m, fx_1m, bar_revisions) with SYMBOL CAPACITY ... [NO]CACHE, PARTITION BY MONTH WAL, and DEDUP UPSERT KEYS
  • Typed bars_1m Range Reader: readBars1m() binds listing_id as canonical decimal string, returns INT32 listing_id values, and projects ts and known_at as lossless UTC epoch microseconds (bigint)
  • InfluxDB Line Protocol: Batch ingestion writer
  • Health Monitoring: Connection health monitoring and metrics

Usage

Parameterized Queries & LATEST ON

import { QuestDBClient, QuestDBQueryBuilder } from '@stock-bot/questdb';

const questClient = new QuestDBClient({
  host: 'localhost',
  httpPort: 9000,
  pgPort: 8812,
  influxPort: 9009,
  database: 'qdb',
});
await questClient.connect();

// Parameterized PGWire query via query builder
const { sql, params } = QuestDBQueryBuilder.create(questClient)
  .select('listing_id', 'ts', 'close', 'volume')
  .from('bars_1m')
  .where('listing_id = $1', ['1042'])
  .latestOn('ts', 'listing_id')
  .buildParameterized();
// sql:
// SELECT listing_id, ts, close, volume FROM bars_1m WHERE listing_id = $1 LATEST ON ts PARTITION BY listing_id
// params: ['1042']

// Execute directly (forwards both SQL and bound params)
const latestBars = await questClient.latestBy('bars_1m', ['listing_id', 'ts', 'close'], 'listing_id', {
  where: 'session = $1',
  params: ['RTH'],
});

// Override the designated timestamp column explicitly
const latestRevisions = await questClient.latestBy(
  'bar_revisions',
  ['listing_id', 'revised_at', 'new_close'],
  'listing_id',
  { timestampColumn: 'revised_at' }
);

// Time-series aggregations with bound filters
const hourlyStats = await questClient
  .aggregate('ohlcv_data')
  .selectAgg({ avg_close: 'avg(close)' })
  .whereSymbol('AAPL')
  .whereLastHours(24)
  .groupBy('symbol')
  .sampleBy('1h')
  .execute();

Reading bars_1m Ranges

import { readBars1m } from '@stock-bot/questdb';

// Queries `[from, to)` ordered by `ts ASC`, binding listingId as decimal string SYMBOL
// and converting returned `listing_id` back to signed INT32.
const bars = await readBars1m(
  questClient,
  1042,
  new Date('2024-01-02T14:30:00.000Z'),
  new Date('2024-01-02T21:00:00.000Z')
);

Storage-layer read semantics: readBars1m returns all stored bars in [from, to) with ts and known_at projected via CAST(... AS LONG) as UTC epoch microseconds (bigint, preserving all 6 fractional digits independent of host timezone) without applying replay cutoffs. Research callers (MarketDataStore) remain responsible for as-of availability filtering and Vault fencing.

M2 Intraday Contract DDL (Offline)

import { getM2IntradayDDL } from '@stock-bot/questdb';

const ddl = getM2IntradayDDL();
// ddl.bars_1m       -> CREATE TABLE IF NOT EXISTS bars_1m (...) TIMESTAMP(ts) PARTITION BY MONTH WAL DEDUP UPSERT KEYS(ts, listing_id)
// ddl.fx_1m         -> CREATE TABLE IF NOT EXISTS fx_1m (...) TIMESTAMP(ts) PARTITION BY MONTH WAL DEDUP UPSERT KEYS(ts, pair)
// ddl.bar_revisions -> CREATE TABLE IF NOT EXISTS bar_revisions (...) TIMESTAMP(revised_at) PARTITION BY MONTH WAL

M2 intraday tables are opt-in and are not auto-created during QuestDBClient.connect().

Configuration

Configure using environment variables:

QUESTDB_HOST=localhost
QUESTDB_HTTP_PORT=9000
QUESTDB_PG_PORT=8812
QUESTDB_INFLUX_PORT=9009

Time-Series Features

QuestDB 10.2.0 supports:

  • High-frequency data: Millions of data points per second
  • Time-based partitioning: Monthly WAL partitions for intraday tables
  • ASOF JOINs: Time-series specific joins
  • SAMPLE BY: Time-based aggregations
  • LATEST ON: Get latest values per partition key (LATEST ON <ts> PARTITION BY <keys>)