@tradejs/infra 2.0.18 → 2.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/ai.js +3 -54
  2. package/dist/ai.mjs +1 -54
  3. package/dist/chunk-2CZREG43.mjs +112 -0
  4. package/dist/chunk-DFMKDB2R.mjs +1285 -0
  5. package/dist/chunk-I2J6YDBD.mjs +910 -0
  6. package/dist/chunk-NWXFWTWU.mjs +1114 -0
  7. package/dist/chunk-SZQB7ER5.mjs +492 -0
  8. package/dist/chunk-YVIHTUV5.mjs +286 -0
  9. package/dist/internal-2coHaaos.d.mts +26 -0
  10. package/dist/internal-2coHaaos.d.ts +26 -0
  11. package/dist/runtimeDeployments.d.mts +10 -0
  12. package/dist/runtimeDeployments.d.ts +10 -0
  13. package/dist/runtimeDeployments.js +447 -0
  14. package/dist/runtimeDeployments.mjs +81 -0
  15. package/dist/runtimeStrategyConfigs.d.mts +28 -0
  16. package/dist/runtimeStrategyConfigs.d.ts +28 -0
  17. package/dist/runtimeStrategyConfigs.js +425 -0
  18. package/dist/runtimeStrategyConfigs.mjs +89 -0
  19. package/dist/timescale/candles.d.mts +38 -0
  20. package/dist/timescale/candles.d.ts +38 -0
  21. package/dist/timescale/candles.js +408 -0
  22. package/dist/timescale/candles.mjs +21 -0
  23. package/dist/timescale/client.d.mts +4 -0
  24. package/dist/timescale/client.d.ts +4 -0
  25. package/dist/timescale/client.js +109 -0
  26. package/dist/timescale/client.mjs +12 -0
  27. package/dist/timescale/derivatives.d.mts +90 -0
  28. package/dist/timescale/derivatives.d.ts +90 -0
  29. package/dist/timescale/derivatives.js +1270 -0
  30. package/dist/timescale/derivatives.mjs +26 -0
  31. package/dist/timescale/hyperliquidWhales.d.mts +149 -0
  32. package/dist/timescale/hyperliquidWhales.d.ts +149 -0
  33. package/dist/timescale/hyperliquidWhales.js +1893 -0
  34. package/dist/timescale/hyperliquidWhales.mjs +30 -0
  35. package/dist/timescale/marketContext.d.mts +188 -0
  36. package/dist/timescale/marketContext.d.ts +188 -0
  37. package/dist/timescale/marketContext.js +2091 -0
  38. package/dist/timescale/marketContext.mjs +60 -0
  39. package/dist/timescale/spread.d.mts +11 -0
  40. package/dist/timescale/spread.d.ts +11 -0
  41. package/dist/timescale/spread.js +215 -0
  42. package/dist/timescale/spread.mjs +11 -0
  43. package/dist/timescale.d.mts +9 -476
  44. package/dist/timescale.d.ts +9 -476
  45. package/dist/timescale.js +2121 -2112
  46. package/dist/timescale.mjs +73 -4070
  47. package/dist/tradingAccounts.d.mts +2 -8
  48. package/dist/tradingAccounts.d.ts +2 -8
  49. package/dist/tradingAccounts.js +0 -71
  50. package/dist/tradingAccounts.mjs +0 -65
  51. package/dist/values-BrvcmnfM.d.mts +6 -0
  52. package/dist/values-BrvcmnfM.d.ts +6 -0
  53. package/package.json +43 -2
@@ -0,0 +1,286 @@
1
+ import {
2
+ ensureCandlesSchema,
3
+ getPool,
4
+ normalizeCandleProvider,
5
+ normalizeCandleSymbol
6
+ } from "./chunk-I2J6YDBD.mjs";
7
+
8
+ // src/timescale/candles.ts
9
+ var toRows = (provider, symbol, interval, data) => {
10
+ const normalizedProvider = normalizeCandleProvider(provider);
11
+ if (!normalizedProvider) {
12
+ throw new Error("Candle provider is required");
13
+ }
14
+ const normalizedSymbol = normalizeCandleSymbol(symbol);
15
+ return data.map((i) => ({
16
+ provider: normalizedProvider,
17
+ symbol: normalizedSymbol,
18
+ interval,
19
+ ts: new Date(i.timestamp),
20
+ // ms -> Date
21
+ open: i.open,
22
+ high: i.high,
23
+ low: i.low,
24
+ close: i.close,
25
+ volume: i.volume ?? null,
26
+ turnover: i.turnover ?? null,
27
+ takerBuyBaseVolume: i.takerBuyBaseVolume ?? null,
28
+ takerBuyQuoteVolume: i.takerBuyQuoteVolume ?? null,
29
+ takerSellBaseVolume: i.takerSellBaseVolume ?? null,
30
+ takerSellQuoteVolume: i.takerSellQuoteVolume ?? null
31
+ }));
32
+ };
33
+ async function upsertCandles(rows) {
34
+ if (!rows.length) return;
35
+ await ensureCandlesSchema();
36
+ const pool = getPool();
37
+ const cols = [
38
+ "provider",
39
+ "symbol",
40
+ "interval",
41
+ "ts",
42
+ "open",
43
+ "high",
44
+ "low",
45
+ "close",
46
+ "volume",
47
+ "turnover",
48
+ "taker_buy_base_volume",
49
+ "taker_buy_quote_volume",
50
+ "taker_sell_base_volume",
51
+ "taker_sell_quote_volume"
52
+ ];
53
+ const maxRows = Math.floor(65535 / cols.length);
54
+ if (rows.length > maxRows) {
55
+ for (let i = 0; i < rows.length; i += maxRows) {
56
+ await upsertCandles(rows.slice(i, i + maxRows));
57
+ }
58
+ return;
59
+ }
60
+ const valuesSql = rows.map(
61
+ (_, i) => `(${cols.map((__, j) => `$${i * cols.length + j + 1}`).join(",")})`
62
+ ).join(",");
63
+ const flat = rows.flatMap((r) => [
64
+ normalizeCandleProvider(r.provider),
65
+ normalizeCandleSymbol(r.symbol),
66
+ r.interval,
67
+ r.ts,
68
+ r.open,
69
+ r.high,
70
+ r.low,
71
+ r.close,
72
+ r.volume ?? null,
73
+ r.turnover ?? null,
74
+ r.takerBuyBaseVolume ?? null,
75
+ r.takerBuyQuoteVolume ?? null,
76
+ r.takerSellBaseVolume ?? null,
77
+ r.takerSellQuoteVolume ?? null
78
+ ]);
79
+ const sql = `
80
+ INSERT INTO candles (${cols.join(",")})
81
+ VALUES ${valuesSql}
82
+ ON CONFLICT (provider, symbol, interval, ts) DO UPDATE SET
83
+ open = EXCLUDED.open,
84
+ high = EXCLUDED.high,
85
+ low = EXCLUDED.low,
86
+ close = EXCLUDED.close,
87
+ volume = COALESCE(EXCLUDED.volume, candles.volume),
88
+ turnover = COALESCE(EXCLUDED.turnover, candles.turnover),
89
+ taker_buy_base_volume = COALESCE(EXCLUDED.taker_buy_base_volume, candles.taker_buy_base_volume),
90
+ taker_buy_quote_volume = COALESCE(EXCLUDED.taker_buy_quote_volume, candles.taker_buy_quote_volume),
91
+ taker_sell_base_volume = COALESCE(EXCLUDED.taker_sell_base_volume, candles.taker_sell_base_volume),
92
+ taker_sell_quote_volume = COALESCE(EXCLUDED.taker_sell_quote_volume, candles.taker_sell_quote_volume)
93
+ `;
94
+ const client = await pool.connect();
95
+ try {
96
+ await client.query("BEGIN");
97
+ await client.query(sql, flat);
98
+ await client.query("COMMIT");
99
+ } catch (e) {
100
+ await client.query("ROLLBACK");
101
+ throw e;
102
+ } finally {
103
+ client.release();
104
+ }
105
+ }
106
+ async function getCandlesRange(provider, symbol, interval, startMs, endMs) {
107
+ await ensureCandlesSchema();
108
+ const pool = getPool();
109
+ const normalizedProvider = normalizeCandleProvider(provider);
110
+ const normalizedSymbol = normalizeCandleSymbol(symbol);
111
+ const sql = `
112
+ SELECT symbol, interval, ts,
113
+ open, high, low, close, volume, turnover,
114
+ taker_buy_base_volume AS "takerBuyBaseVolume",
115
+ taker_buy_quote_volume AS "takerBuyQuoteVolume",
116
+ taker_sell_base_volume AS "takerSellBaseVolume",
117
+ taker_sell_quote_volume AS "takerSellQuoteVolume"
118
+ FROM candles
119
+ WHERE provider = $1 AND symbol = $2 AND interval = $3
120
+ AND ts >= to_timestamp($4/1000.0)
121
+ AND ts <= to_timestamp($5/1000.0)
122
+ ORDER BY ts ASC
123
+ `;
124
+ const res = await pool.query(sql, [
125
+ normalizedProvider,
126
+ normalizedSymbol,
127
+ interval,
128
+ startMs,
129
+ endMs
130
+ ]);
131
+ return res.rows;
132
+ }
133
+ async function getDataEdges(provider, symbol, interval) {
134
+ await ensureCandlesSchema();
135
+ const pool = getPool();
136
+ const normalizedProvider = normalizeCandleProvider(provider);
137
+ const normalizedSymbol = normalizeCandleSymbol(symbol);
138
+ const sqlMin = `
139
+ SELECT extract(epoch from ts)*1000 AS ms
140
+ FROM candles
141
+ WHERE provider=$1 AND symbol=$2 AND interval=$3
142
+ ORDER BY ts ASC
143
+ LIMIT 1
144
+ `;
145
+ const sqlMax = `
146
+ SELECT extract(epoch from ts)*1000 AS ms
147
+ FROM candles
148
+ WHERE provider=$1 AND symbol=$2 AND interval=$3
149
+ ORDER BY ts DESC
150
+ LIMIT 1
151
+ `;
152
+ const [minQ, maxQ] = await Promise.all([
153
+ pool.query(sqlMin, [normalizedProvider, normalizedSymbol, interval]),
154
+ pool.query(sqlMax, [normalizedProvider, normalizedSymbol, interval])
155
+ ]);
156
+ const minRaw = minQ.rows[0]?.ms;
157
+ const maxRaw = maxQ.rows[0]?.ms;
158
+ const min = Number.isFinite(Number(minRaw)) ? Number(minRaw) : void 0;
159
+ const max = Number.isFinite(Number(maxRaw)) ? Number(maxRaw) : void 0;
160
+ return { min, max };
161
+ }
162
+ async function getDataEdgesForSymbols(provider, symbols, interval) {
163
+ const normalizedSymbols = [
164
+ ...new Set(symbols.map(normalizeCandleSymbol).filter(Boolean))
165
+ ];
166
+ const result = /* @__PURE__ */ new Map();
167
+ for (const symbol of normalizedSymbols) {
168
+ result.set(symbol, {});
169
+ }
170
+ if (!normalizedSymbols.length) {
171
+ return result;
172
+ }
173
+ await ensureCandlesSchema();
174
+ const pool = getPool();
175
+ const normalizedProvider = normalizeCandleProvider(provider);
176
+ const sql = `
177
+ WITH requested(symbol) AS (
178
+ SELECT unnest($2::text[])
179
+ )
180
+ SELECT
181
+ r.symbol,
182
+ (
183
+ SELECT extract(epoch from c.ts)*1000
184
+ FROM candles c
185
+ WHERE c.provider = $1 AND c.symbol = r.symbol AND c.interval = $3
186
+ ORDER BY c.ts ASC
187
+ LIMIT 1
188
+ ) AS min_ms,
189
+ (
190
+ SELECT extract(epoch from c.ts)*1000
191
+ FROM candles c
192
+ WHERE c.provider = $1 AND c.symbol = r.symbol AND c.interval = $3
193
+ ORDER BY c.ts DESC
194
+ LIMIT 1
195
+ ) AS max_ms
196
+ FROM requested r
197
+ `;
198
+ const response = await pool.query(sql, [
199
+ normalizedProvider,
200
+ normalizedSymbols,
201
+ interval
202
+ ]);
203
+ for (const row of response.rows) {
204
+ const symbol = normalizeCandleSymbol(String(row.symbol || ""));
205
+ if (!symbol) continue;
206
+ const min = row.min_ms == null ? NaN : Number(row.min_ms);
207
+ const max = row.max_ms == null ? NaN : Number(row.max_ms);
208
+ result.set(symbol, {
209
+ ...Number.isFinite(min) ? { min } : {},
210
+ ...Number.isFinite(max) ? { max } : {}
211
+ });
212
+ }
213
+ return result;
214
+ }
215
+ async function waitForDbReady(attempts = 20, delayMs = 1e3) {
216
+ const pool = getPool();
217
+ let lastError;
218
+ for (let i = 0; i < attempts; i++) {
219
+ try {
220
+ await pool.query("SELECT 1");
221
+ return;
222
+ } catch (e) {
223
+ lastError = e;
224
+ await new Promise((r) => setTimeout(r, delayMs));
225
+ }
226
+ }
227
+ throw lastError;
228
+ }
229
+ async function deleteCandles(provider, symbol, interval) {
230
+ const pool = getPool();
231
+ const normalizedProvider = normalizeCandleProvider(provider);
232
+ const normalizedSymbol = normalizeCandleSymbol(symbol);
233
+ const sql = `
234
+ DELETE FROM candles
235
+ WHERE provider = $1 AND symbol = $2 AND interval = $3
236
+ `;
237
+ await pool.query(sql, [normalizedProvider, normalizedSymbol, interval]);
238
+ }
239
+ async function findContinuityGap(provider, symbol, interval) {
240
+ const pool = getPool();
241
+ const normalizedProvider = normalizeCandleProvider(provider);
242
+ const normalizedSymbol = normalizeCandleSymbol(symbol);
243
+ const expectedSeconds = interval * 60;
244
+ const sql = `
245
+ WITH ordered AS (
246
+ SELECT
247
+ ts,
248
+ LAG(ts) OVER (ORDER BY ts) AS prev_ts
249
+ FROM candles
250
+ WHERE provider = $1 AND symbol = $2 AND interval = $3
251
+ )
252
+ SELECT
253
+ ts,
254
+ prev_ts,
255
+ EXTRACT(EPOCH FROM (ts - prev_ts))::int AS diff_seconds
256
+ FROM ordered
257
+ WHERE prev_ts IS NOT NULL
258
+ AND EXTRACT(EPOCH FROM (ts - prev_ts))::int <> $4
259
+ ORDER BY ts ASC
260
+ LIMIT 1
261
+ `;
262
+ const res = await pool.query(sql, [
263
+ normalizedProvider,
264
+ normalizedSymbol,
265
+ interval,
266
+ expectedSeconds
267
+ ]);
268
+ const row = res.rows[0];
269
+ if (!row) return null;
270
+ return {
271
+ ts: new Date(row.ts).getTime(),
272
+ prevTs: new Date(row.prev_ts).getTime(),
273
+ diffSeconds: row.diff_seconds
274
+ };
275
+ }
276
+
277
+ export {
278
+ toRows,
279
+ upsertCandles,
280
+ getCandlesRange,
281
+ getDataEdges,
282
+ getDataEdgesForSymbols,
283
+ waitForDbReady,
284
+ deleteCandles,
285
+ findContinuityGap
286
+ };
@@ -0,0 +1,26 @@
1
+ import { Pool } from 'pg';
2
+
3
+ declare global {
4
+ var __pgPool__: Pool | undefined;
5
+ }
6
+
7
+ type TimescaleMarketContextQueryOptions = {
8
+ signal?: AbortSignal;
9
+ timeoutMs?: number;
10
+ };
11
+
12
+ type TimescaleMarketContextSource = 'binance' | 'coinmarketcap' | 'derivatives' | 'hyperliquidWhales';
13
+ declare const configureTimescaleMarketContextSchemaMode: (mode: "ensure" | "verify") => void;
14
+ declare const closeTimescalePool: () => Promise<void>;
15
+ declare const ensureDerivativesSchema: () => Promise<void>;
16
+ declare const ensureBinanceMarketSchema: () => Promise<void>;
17
+ declare const ensureHyperliquidWhaleSchema: () => Promise<void>;
18
+ /**
19
+ * CoinMarketCap tables currently share the historical market-context migration
20
+ * with the Binance tables. Keeping a source-specific entrypoint lets process
21
+ * composition own schema preparation without exposing that storage detail.
22
+ */
23
+ declare const ensureCoinMarketCapContextSchema: () => Promise<void>;
24
+ declare const ensureMarketContextSchemas: (sources: Iterable<TimescaleMarketContextSource>) => Promise<void>;
25
+
26
+ export { type TimescaleMarketContextQueryOptions as T, type TimescaleMarketContextSource as a, configureTimescaleMarketContextSchemaMode as b, closeTimescalePool as c, ensureCoinMarketCapContextSchema as d, ensureBinanceMarketSchema as e, ensureDerivativesSchema as f, ensureHyperliquidWhaleSchema as g, ensureMarketContextSchemas as h };
@@ -0,0 +1,26 @@
1
+ import { Pool } from 'pg';
2
+
3
+ declare global {
4
+ var __pgPool__: Pool | undefined;
5
+ }
6
+
7
+ type TimescaleMarketContextQueryOptions = {
8
+ signal?: AbortSignal;
9
+ timeoutMs?: number;
10
+ };
11
+
12
+ type TimescaleMarketContextSource = 'binance' | 'coinmarketcap' | 'derivatives' | 'hyperliquidWhales';
13
+ declare const configureTimescaleMarketContextSchemaMode: (mode: "ensure" | "verify") => void;
14
+ declare const closeTimescalePool: () => Promise<void>;
15
+ declare const ensureDerivativesSchema: () => Promise<void>;
16
+ declare const ensureBinanceMarketSchema: () => Promise<void>;
17
+ declare const ensureHyperliquidWhaleSchema: () => Promise<void>;
18
+ /**
19
+ * CoinMarketCap tables currently share the historical market-context migration
20
+ * with the Binance tables. Keeping a source-specific entrypoint lets process
21
+ * composition own schema preparation without exposing that storage detail.
22
+ */
23
+ declare const ensureCoinMarketCapContextSchema: () => Promise<void>;
24
+ declare const ensureMarketContextSchemas: (sources: Iterable<TimescaleMarketContextSource>) => Promise<void>;
25
+
26
+ export { type TimescaleMarketContextQueryOptions as T, type TimescaleMarketContextSource as a, configureTimescaleMarketContextSchemaMode as b, closeTimescalePool as c, ensureCoinMarketCapContextSchema as d, ensureBinanceMarketSchema as e, ensureDerivativesSchema as f, ensureHyperliquidWhaleSchema as g, ensureMarketContextSchemas as h };
@@ -0,0 +1,10 @@
1
+ import { RuntimeDeployment, RuntimeDeploymentHeartbeat } from '@tradejs/types';
2
+
3
+ declare const listRuntimeDeployments: (userName: string) => Promise<RuntimeDeployment[]>;
4
+ declare const getRuntimeDeployment: (userName: string, deploymentId: string) => Promise<RuntimeDeployment | null>;
5
+ declare const saveRuntimeDeployment: (userName: string, deployment: RuntimeDeployment) => Promise<RuntimeDeployment>;
6
+ declare const deleteRuntimeDeployment: (userName: string, deploymentId: string) => Promise<void>;
7
+ declare const getRuntimeDeploymentHeartbeat: (userName: string, deploymentId: string) => Promise<RuntimeDeploymentHeartbeat | null>;
8
+ declare const saveRuntimeDeploymentHeartbeat: (userName: string, heartbeat: RuntimeDeploymentHeartbeat) => Promise<RuntimeDeploymentHeartbeat>;
9
+
10
+ export { deleteRuntimeDeployment, getRuntimeDeployment, getRuntimeDeploymentHeartbeat, listRuntimeDeployments, saveRuntimeDeployment, saveRuntimeDeploymentHeartbeat };
@@ -0,0 +1,10 @@
1
+ import { RuntimeDeployment, RuntimeDeploymentHeartbeat } from '@tradejs/types';
2
+
3
+ declare const listRuntimeDeployments: (userName: string) => Promise<RuntimeDeployment[]>;
4
+ declare const getRuntimeDeployment: (userName: string, deploymentId: string) => Promise<RuntimeDeployment | null>;
5
+ declare const saveRuntimeDeployment: (userName: string, deployment: RuntimeDeployment) => Promise<RuntimeDeployment>;
6
+ declare const deleteRuntimeDeployment: (userName: string, deploymentId: string) => Promise<void>;
7
+ declare const getRuntimeDeploymentHeartbeat: (userName: string, deploymentId: string) => Promise<RuntimeDeploymentHeartbeat | null>;
8
+ declare const saveRuntimeDeploymentHeartbeat: (userName: string, heartbeat: RuntimeDeploymentHeartbeat) => Promise<RuntimeDeploymentHeartbeat>;
9
+
10
+ export { deleteRuntimeDeployment, getRuntimeDeployment, getRuntimeDeploymentHeartbeat, listRuntimeDeployments, saveRuntimeDeployment, saveRuntimeDeploymentHeartbeat };