Add equity print quote joins
This commit is contained in:
parent
9908c431f0
commit
3164167bee
15 changed files with 1033 additions and 7 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { SP500_SYMBOLS, type EquityPrint } from "@islandflow/types";
|
||||
import { SP500_SYMBOLS, type EquityPrint, type EquityQuote } from "@islandflow/types";
|
||||
import type { EquityIngestAdapter, EquityIngestHandlers } from "./types";
|
||||
|
||||
type SyntheticEquitiesAdapterConfig = {
|
||||
|
|
@ -38,6 +38,25 @@ const buildSyntheticPrint = (
|
|||
};
|
||||
};
|
||||
|
||||
const buildSyntheticQuote = (
|
||||
seq: number,
|
||||
now: number,
|
||||
symbol: string,
|
||||
bid: number,
|
||||
ask: number
|
||||
): EquityQuote => {
|
||||
return {
|
||||
source_ts: now,
|
||||
ingest_ts: now,
|
||||
seq,
|
||||
trace_id: `synthetic-equity-quote-${seq}`,
|
||||
ts: now,
|
||||
underlying_id: symbol,
|
||||
bid,
|
||||
ask
|
||||
};
|
||||
};
|
||||
|
||||
export const createSyntheticEquitiesAdapter = (
|
||||
config: SyntheticEquitiesAdapterConfig
|
||||
): EquityIngestAdapter => {
|
||||
|
|
@ -45,6 +64,7 @@ export const createSyntheticEquitiesAdapter = (
|
|||
name: "synthetic",
|
||||
start: (handlers: EquityIngestHandlers) => {
|
||||
let seq = 0;
|
||||
let quoteSeq = 0;
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
let stopped = false;
|
||||
|
||||
|
|
@ -65,8 +85,18 @@ export const createSyntheticEquitiesAdapter = (
|
|||
const size = 10 + (seq % 600);
|
||||
const exchange = EXCHANGES[(seq + symbolHash) % EXCHANGES.length];
|
||||
const offExchangeFlag = (seq + i) % 6 === 0;
|
||||
const print = buildSyntheticPrint(seq, now + i * 4, symbol, price, size, exchange, offExchangeFlag);
|
||||
const eventTs = now + i * 4;
|
||||
const print = buildSyntheticPrint(seq, eventTs, symbol, price, size, exchange, offExchangeFlag);
|
||||
void handlers.onTrade(print);
|
||||
|
||||
if (handlers.onQuote) {
|
||||
quoteSeq += 1;
|
||||
const spread = Math.max(0.02, Number((price * 0.002).toFixed(2)));
|
||||
const bid = Math.max(0.01, Number((price - spread / 2).toFixed(2)));
|
||||
const ask = Math.max(bid + 0.01, Number((price + spread / 2).toFixed(2)));
|
||||
const quote = buildSyntheticQuote(quoteSeq, eventTs, symbol, bid, ask);
|
||||
void handlers.onQuote(quote);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ import { readEnv } from "@islandflow/config";
|
|||
import { createLogger } from "@islandflow/observability";
|
||||
import {
|
||||
SUBJECT_EQUITY_PRINTS,
|
||||
SUBJECT_EQUITY_QUOTES,
|
||||
STREAM_EQUITY_PRINTS,
|
||||
STREAM_EQUITY_QUOTES,
|
||||
connectJetStreamWithRetry,
|
||||
ensureStream,
|
||||
publishJson
|
||||
|
|
@ -10,9 +12,16 @@ import {
|
|||
import {
|
||||
createClickHouseClient,
|
||||
ensureEquityPrintsTable,
|
||||
insertEquityPrint
|
||||
ensureEquityQuotesTable,
|
||||
insertEquityPrint,
|
||||
insertEquityQuote
|
||||
} from "@islandflow/storage";
|
||||
import { EquityPrintSchema, type EquityPrint } from "@islandflow/types";
|
||||
import {
|
||||
EquityPrintSchema,
|
||||
EquityQuoteSchema,
|
||||
type EquityPrint,
|
||||
type EquityQuote
|
||||
} from "@islandflow/types";
|
||||
import { createSyntheticEquitiesAdapter } from "./adapters/synthetic";
|
||||
import type { EquityIngestAdapter, StopHandler } from "./adapters/types";
|
||||
import { z } from "zod";
|
||||
|
|
@ -136,6 +145,19 @@ const run = async () => {
|
|||
num_replicas: 1
|
||||
});
|
||||
|
||||
await ensureStream(jsm, {
|
||||
name: STREAM_EQUITY_QUOTES,
|
||||
subjects: [SUBJECT_EQUITY_QUOTES],
|
||||
retention: "limits",
|
||||
storage: "file",
|
||||
discard: "old",
|
||||
max_msgs_per_subject: -1,
|
||||
max_msgs: -1,
|
||||
max_bytes: -1,
|
||||
max_age: 0,
|
||||
num_replicas: 1
|
||||
});
|
||||
|
||||
const clickhouse = createClickHouseClient({
|
||||
url: env.CLICKHOUSE_URL,
|
||||
database: env.CLICKHOUSE_DATABASE
|
||||
|
|
@ -143,11 +165,13 @@ const run = async () => {
|
|||
|
||||
await retry("clickhouse table init", 20, 500, async () => {
|
||||
await ensureEquityPrintsTable(clickhouse);
|
||||
await ensureEquityQuotesTable(clickhouse);
|
||||
});
|
||||
|
||||
const adapter = selectAdapter(env.EQUITIES_INGEST_ADAPTER);
|
||||
logger.info("ingest adapter selected", { adapter: adapter.name });
|
||||
const allowPublish = buildThrottle(env.TESTING_MODE, env.TESTING_THROTTLE_MS);
|
||||
const allowQuotePublish = buildThrottle(env.TESTING_MODE, env.TESTING_THROTTLE_MS);
|
||||
|
||||
const stopAdapter: StopHandler = await adapter.start({
|
||||
onTrade: async (candidate: EquityPrint) => {
|
||||
|
|
@ -176,6 +200,28 @@ const run = async () => {
|
|||
trace_id: print.trace_id
|
||||
});
|
||||
}
|
||||
},
|
||||
onQuote: async (candidate: EquityQuote) => {
|
||||
if (state.shuttingDown) {
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
if (!allowQuotePublish(now)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const quote = EquityQuoteSchema.parse(candidate);
|
||||
|
||||
try {
|
||||
await insertEquityQuote(clickhouse, quote);
|
||||
await publishJson(js, SUBJECT_EQUITY_QUOTES, quote);
|
||||
} catch (error) {
|
||||
logger.error("failed to publish equity quote", {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
trace_id: quote.trace_id
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue