@catalyst-team/poly-sdk 0.4.6 → 0.4.7
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.
- package/README.md +18 -9
- package/README.zh-CN.md +18 -9
- package/dist/src/clients/data-api.d.ts +25 -0
- package/dist/src/clients/data-api.d.ts.map +1 -1
- package/dist/src/clients/data-api.js +57 -0
- package/dist/src/clients/data-api.js.map +1 -1
- package/dist/src/core/types.d.ts +55 -0
- package/dist/src/core/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +6 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +4 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/realtime/index.d.ts +18 -0
- package/dist/src/realtime/index.d.ts.map +1 -0
- package/dist/src/realtime/index.js +14 -0
- package/dist/src/realtime/index.js.map +1 -0
- package/dist/src/realtime/realtime-data-client.d.ts +274 -0
- package/dist/src/realtime/realtime-data-client.d.ts.map +1 -0
- package/dist/src/realtime/realtime-data-client.js +771 -0
- package/dist/src/realtime/realtime-data-client.js.map +1 -0
- package/dist/src/realtime/types.d.ts +485 -0
- package/dist/src/realtime/types.d.ts.map +1 -0
- package/dist/src/realtime/types.js +36 -0
- package/dist/src/realtime/types.js.map +1 -0
- package/dist/src/services/arbitrage-service.d.ts.map +1 -1
- package/dist/src/services/arbitrage-service.js +2 -1
- package/dist/src/services/arbitrage-service.js.map +1 -1
- package/dist/src/services/dip-arb-service.d.ts.map +1 -1
- package/dist/src/services/dip-arb-service.js +3 -19
- package/dist/src/services/dip-arb-service.js.map +1 -1
- package/dist/src/services/market-service.d.ts +93 -11
- package/dist/src/services/market-service.d.ts.map +1 -1
- package/dist/src/services/market-service.js +189 -22
- package/dist/src/services/market-service.js.map +1 -1
- package/dist/src/services/order-handle.test.d.ts +15 -0
- package/dist/src/services/order-handle.test.d.ts.map +1 -0
- package/dist/src/services/order-handle.test.js +333 -0
- package/dist/src/services/order-handle.test.js.map +1 -0
- package/dist/src/services/order-manager.d.ts +162 -6
- package/dist/src/services/order-manager.d.ts.map +1 -1
- package/dist/src/services/order-manager.js +419 -30
- package/dist/src/services/order-manager.js.map +1 -1
- package/dist/src/services/realtime-service-v2.d.ts +122 -6
- package/dist/src/services/realtime-service-v2.d.ts.map +1 -1
- package/dist/src/services/realtime-service-v2.js +475 -70
- package/dist/src/services/realtime-service-v2.js.map +1 -1
- package/dist/src/services/trading-service.d.ts +129 -1
- package/dist/src/services/trading-service.d.ts.map +1 -1
- package/dist/src/services/trading-service.js +198 -5
- package/dist/src/services/trading-service.js.map +1 -1
- package/package.json +1 -2
- package/dist/src/services/ctf-detector.d.ts +0 -215
- package/dist/src/services/ctf-detector.d.ts.map +0 -1
- package/dist/src/services/ctf-detector.js +0 -420
- package/dist/src/services/ctf-detector.js.map +0 -1
|
@@ -12,7 +12,7 @@ import { DataApiClient } from '../clients/data-api.js';
|
|
|
12
12
|
import { GammaApiClient, GammaMarket } from '../clients/gamma-api.js';
|
|
13
13
|
import type { UnifiedCache } from '../core/unified-cache.js';
|
|
14
14
|
import { RateLimiter } from '../core/rate-limiter.js';
|
|
15
|
-
import type { UnifiedMarket, ProcessedOrderbook, ArbitrageOpportunity, KLineInterval, KLineCandle, DualKLineData, RealtimeSpreadAnalysis, Side, Orderbook, UnderlyingAsset, TokenUnderlyingCorrelation } from '../core/types.js';
|
|
15
|
+
import type { UnifiedMarket, ProcessedOrderbook, ArbitrageOpportunity, KLineInterval, KLineCandle, DualKLineData, RealtimeSpreadAnalysis, Side, Orderbook, UnderlyingAsset, TokenUnderlyingCorrelation, PricePoint, DualPriceLineData } from '../core/types.js';
|
|
16
16
|
import type { BinanceService } from './binance-service.js';
|
|
17
17
|
export declare const POLYGON_MAINNET = 137;
|
|
18
18
|
export type { Side, Orderbook } from '../core/types.js';
|
|
@@ -24,10 +24,7 @@ export interface PriceHistoryParams {
|
|
|
24
24
|
endTs?: number;
|
|
25
25
|
fidelity?: number;
|
|
26
26
|
}
|
|
27
|
-
export
|
|
28
|
-
timestamp: number;
|
|
29
|
-
price: number;
|
|
30
|
-
}
|
|
27
|
+
export type { PricePoint } from '../core/types.js';
|
|
31
28
|
export interface MarketServiceConfig {
|
|
32
29
|
/** Private key for CLOB client auth (optional, for authenticated endpoints) */
|
|
33
30
|
privateKey?: string;
|
|
@@ -186,7 +183,67 @@ export declare class MarketService {
|
|
|
186
183
|
private getMarketBySlug;
|
|
187
184
|
private getMarketByConditionId;
|
|
188
185
|
/**
|
|
189
|
-
* Get
|
|
186
|
+
* Get price line for a market outcome from /prices-history API.
|
|
187
|
+
*
|
|
188
|
+
* This returns pre-computed price points from the CLOB API,
|
|
189
|
+
* suitable for lightweight price charts. For OHLCV candles
|
|
190
|
+
* aggregated from trade data, use `getKLinesOHLCV()` instead.
|
|
191
|
+
*
|
|
192
|
+
* @param conditionId - Market condition ID
|
|
193
|
+
* @param interval - Price history interval (1h, 6h, 1d, 1w, max)
|
|
194
|
+
* @param options - Query options
|
|
195
|
+
* @param options.fidelity - Number of price points to return
|
|
196
|
+
* @param options.startTs - Start timestamp (Unix seconds)
|
|
197
|
+
* @param options.endTs - End timestamp (Unix seconds)
|
|
198
|
+
* @param options.outcomeIndex - Outcome index (0 = primary, 1 = secondary; default: 0)
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* // Get 1-day price line for primary outcome
|
|
203
|
+
* const prices = await sdk.markets.getKLines(conditionId, '1d');
|
|
204
|
+
*
|
|
205
|
+
* // Get secondary outcome with fidelity
|
|
206
|
+
* const noLine = await sdk.markets.getKLines(conditionId, '1h', {
|
|
207
|
+
* outcomeIndex: 1,
|
|
208
|
+
* fidelity: 100,
|
|
209
|
+
* });
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
getKLines(conditionId: string, interval: PriceHistoryIntervalString, options?: {
|
|
213
|
+
fidelity?: number;
|
|
214
|
+
startTs?: number;
|
|
215
|
+
endTs?: number;
|
|
216
|
+
outcomeIndex?: number;
|
|
217
|
+
}): Promise<PricePoint[]>;
|
|
218
|
+
/**
|
|
219
|
+
* Get dual price lines (primary + secondary) from /prices-history API.
|
|
220
|
+
*
|
|
221
|
+
* Fetches price history for both outcomes in parallel and optionally
|
|
222
|
+
* computes spread analysis. For OHLCV dual candles from trade data,
|
|
223
|
+
* use `getDualKLinesOHLCV()` instead.
|
|
224
|
+
*
|
|
225
|
+
* @param conditionId - Market condition ID
|
|
226
|
+
* @param interval - Price history interval (1h, 6h, 1d, 1w, max)
|
|
227
|
+
* @param options - Query options
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* const dual = await sdk.markets.getDualKLines(conditionId, '1d');
|
|
232
|
+
* console.log(dual.primary.length); // Primary outcome price points
|
|
233
|
+
* console.log(dual.secondary.length); // Secondary outcome price points
|
|
234
|
+
* console.log(dual.spreadAnalysis); // Spread analysis
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
getDualKLines(conditionId: string, interval: PriceHistoryIntervalString, options?: {
|
|
238
|
+
fidelity?: number;
|
|
239
|
+
startTs?: number;
|
|
240
|
+
endTs?: number;
|
|
241
|
+
}): Promise<DualPriceLineData>;
|
|
242
|
+
/**
|
|
243
|
+
* Get OHLCV K-Line candles for a market (single token).
|
|
244
|
+
*
|
|
245
|
+
* Aggregates raw trade data into OHLCV candles. For lightweight
|
|
246
|
+
* price charts from /prices-history, use `getKLines()` instead.
|
|
190
247
|
*
|
|
191
248
|
* @param conditionId - Market condition ID
|
|
192
249
|
* @param interval - K-line interval (1s, 5s, 15s, 30s, 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d)
|
|
@@ -196,26 +253,40 @@ export declare class MarketService {
|
|
|
196
253
|
* @param options.outcomeIndex - Filter by outcome index (0 = primary, 1 = secondary)
|
|
197
254
|
* @param options.startTimestamp - Start timestamp (Unix ms) - filter trades after this time
|
|
198
255
|
* @param options.endTimestamp - End timestamp (Unix ms) - filter trades before this time
|
|
256
|
+
* @param options.paginate - Use paginated fetching for more complete data (default: false)
|
|
257
|
+
* @param options.maxTrades - Maximum trades when paginating (default: 5000)
|
|
199
258
|
*
|
|
200
259
|
* @example
|
|
201
260
|
* ```typescript
|
|
202
261
|
* // Get 5s candles for the last 15 minutes
|
|
203
262
|
* const now = Date.now();
|
|
204
|
-
* const candles = await sdk.markets.
|
|
263
|
+
* const candles = await sdk.markets.getKLinesOHLCV(conditionId, '5s', {
|
|
205
264
|
* startTimestamp: now - 15 * 60 * 1000,
|
|
206
265
|
* endTimestamp: now,
|
|
207
266
|
* });
|
|
267
|
+
*
|
|
268
|
+
* // Get all available trades with pagination
|
|
269
|
+
* const allCandles = await sdk.markets.getKLinesOHLCV(conditionId, '1h', {
|
|
270
|
+
* paginate: true,
|
|
271
|
+
* maxTrades: 5000,
|
|
272
|
+
* });
|
|
208
273
|
* ```
|
|
209
274
|
*/
|
|
210
|
-
|
|
275
|
+
getKLinesOHLCV(conditionId: string, interval: KLineInterval, options?: {
|
|
211
276
|
limit?: number;
|
|
212
277
|
tokenId?: string;
|
|
213
278
|
outcomeIndex?: number;
|
|
214
279
|
startTimestamp?: number;
|
|
215
280
|
endTimestamp?: number;
|
|
281
|
+
paginate?: boolean;
|
|
282
|
+
maxTrades?: number;
|
|
216
283
|
}): Promise<KLineCandle[]>;
|
|
217
284
|
/**
|
|
218
|
-
* Get dual K-Lines (YES + NO tokens)
|
|
285
|
+
* Get dual OHLCV K-Lines (YES + NO tokens) from trade data.
|
|
286
|
+
*
|
|
287
|
+
* Aggregates raw trade data into dual OHLCV candles with spread analysis
|
|
288
|
+
* and optional real-time orderbook data. For lightweight dual price lines
|
|
289
|
+
* from /prices-history, use `getDualKLines()` instead.
|
|
219
290
|
*
|
|
220
291
|
* @param conditionId - Market condition ID
|
|
221
292
|
* @param interval - K-line interval (1s, 5s, 15s, 30s, 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d)
|
|
@@ -223,22 +294,26 @@ export declare class MarketService {
|
|
|
223
294
|
* @param options.limit - Maximum number of trades to fetch for aggregation (default: 1000)
|
|
224
295
|
* @param options.startTimestamp - Start timestamp (Unix ms) - filter trades after this time
|
|
225
296
|
* @param options.endTimestamp - End timestamp (Unix ms) - filter trades before this time
|
|
297
|
+
* @param options.paginate - Use paginated fetching for more complete data (default: false)
|
|
298
|
+
* @param options.maxTrades - Maximum trades when paginating (default: 5000)
|
|
226
299
|
*
|
|
227
300
|
* @example
|
|
228
301
|
* ```typescript
|
|
229
302
|
* // Get 15s dual K-lines for a 15-minute market
|
|
230
303
|
* const now = Date.now();
|
|
231
|
-
* const data = await sdk.markets.
|
|
304
|
+
* const data = await sdk.markets.getDualKLinesOHLCV(conditionId, '15s', {
|
|
232
305
|
* startTimestamp: now - 15 * 60 * 1000,
|
|
233
306
|
* endTimestamp: now,
|
|
234
307
|
* });
|
|
235
308
|
* console.log(`Up candles: ${data.yes.length}, Down candles: ${data.no.length}`);
|
|
236
309
|
* ```
|
|
237
310
|
*/
|
|
238
|
-
|
|
311
|
+
getDualKLinesOHLCV(conditionId: string, interval: KLineInterval, options?: {
|
|
239
312
|
limit?: number;
|
|
240
313
|
startTimestamp?: number;
|
|
241
314
|
endTimestamp?: number;
|
|
315
|
+
paginate?: boolean;
|
|
316
|
+
maxTrades?: number;
|
|
242
317
|
}): Promise<DualKLineData>;
|
|
243
318
|
/**
|
|
244
319
|
* Get aligned K-line data for token and underlying asset
|
|
@@ -307,6 +382,13 @@ export declare class MarketService {
|
|
|
307
382
|
* - Identifying patterns when orderbook data unavailable
|
|
308
383
|
*/
|
|
309
384
|
private analyzeHistoricalSpread;
|
|
385
|
+
/**
|
|
386
|
+
* Analyze spread from price line data (from /prices-history API).
|
|
387
|
+
*
|
|
388
|
+
* Aligns primary and secondary price points by timestamp and computes
|
|
389
|
+
* the sum and deviation from 1.0 at each aligned point.
|
|
390
|
+
*/
|
|
391
|
+
private analyzePriceLineSpread;
|
|
310
392
|
/**
|
|
311
393
|
* Calculate real-time spread from orderbook (for live trading)
|
|
312
394
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"market-service.d.ts","sourceRoot":"","sources":["../../../src/services/market-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAUH,OAAO,EAAE,aAAa,EAAS,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAW,MAAM,yBAAyB,CAAC;AAE/D,OAAO,KAAK,EACV,aAAa,EAEb,kBAAkB,EAElB,oBAAoB,EACpB,aAAa,EACb,WAAW,EACX,aAAa,EAEb,sBAAsB,EACtB,IAAI,EACJ,SAAS,EACT,eAAe,EACf,0BAA0B,
|
|
1
|
+
{"version":3,"file":"market-service.d.ts","sourceRoot":"","sources":["../../../src/services/market-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAUH,OAAO,EAAE,aAAa,EAAS,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAW,MAAM,yBAAyB,CAAC;AAE/D,OAAO,KAAK,EACV,aAAa,EAEb,kBAAkB,EAElB,oBAAoB,EACpB,aAAa,EACb,WAAW,EACX,aAAa,EAEb,sBAAsB,EACtB,IAAI,EACJ,SAAS,EACT,eAAe,EACf,0BAA0B,EAE1B,UAAU,EAEV,iBAAiB,EAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,sBAAsB,CAAC;AAM5E,eAAO,MAAM,eAAe,MAAM,CAAC;AAmCnC,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,C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AAE,CAAC;IA8G1B;;OAEG;IACG,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CACrD,KAAK,CAAC;QACJ,IAAI,EAAE,cAAc,GAAG,iBAAiB,GAAG,aAAa,GAAG,UAAU,CAAC;QACtE,QAAQ,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;QACpC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAClC,CAAC,CACH;IAgED,OAAO,CAAC,mBAAmB;IAuB3B,OAAO,CAAC,iBAAiB;IAwEzB,OAAO,CAAC,YAAY;IA6BpB,OAAO,CAAC,eAAe;IA6BvB,OAAO,CAAC,cAAc;CA0BvB;AAID,wBAAgB,aAAa,CAAC,QAAQ,EAAE,aAAa,GAAG,MAAM,CAmB7D"}
|
|
@@ -403,9 +403,111 @@ export class MarketService {
|
|
|
403
403
|
}
|
|
404
404
|
throw new PolymarketError(ErrorCode.MARKET_NOT_FOUND, `Market not found: ${conditionId}`);
|
|
405
405
|
}
|
|
406
|
-
// ===== K-Line
|
|
406
|
+
// ===== K-Line: Price Lines (from /prices-history API) =====
|
|
407
407
|
/**
|
|
408
|
-
* Get
|
|
408
|
+
* Get price line for a market outcome from /prices-history API.
|
|
409
|
+
*
|
|
410
|
+
* This returns pre-computed price points from the CLOB API,
|
|
411
|
+
* suitable for lightweight price charts. For OHLCV candles
|
|
412
|
+
* aggregated from trade data, use `getKLinesOHLCV()` instead.
|
|
413
|
+
*
|
|
414
|
+
* @param conditionId - Market condition ID
|
|
415
|
+
* @param interval - Price history interval (1h, 6h, 1d, 1w, max)
|
|
416
|
+
* @param options - Query options
|
|
417
|
+
* @param options.fidelity - Number of price points to return
|
|
418
|
+
* @param options.startTs - Start timestamp (Unix seconds)
|
|
419
|
+
* @param options.endTs - End timestamp (Unix seconds)
|
|
420
|
+
* @param options.outcomeIndex - Outcome index (0 = primary, 1 = secondary; default: 0)
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* ```typescript
|
|
424
|
+
* // Get 1-day price line for primary outcome
|
|
425
|
+
* const prices = await sdk.markets.getKLines(conditionId, '1d');
|
|
426
|
+
*
|
|
427
|
+
* // Get secondary outcome with fidelity
|
|
428
|
+
* const noLine = await sdk.markets.getKLines(conditionId, '1h', {
|
|
429
|
+
* outcomeIndex: 1,
|
|
430
|
+
* fidelity: 100,
|
|
431
|
+
* });
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
async getKLines(conditionId, interval, options) {
|
|
435
|
+
const outcomeIndex = options?.outcomeIndex ?? 0;
|
|
436
|
+
const resolved = await this.resolveMarketTokens(conditionId);
|
|
437
|
+
if (!resolved) {
|
|
438
|
+
throw new PolymarketError(ErrorCode.MARKET_NOT_FOUND, `Cannot resolve tokens for market: ${conditionId}`);
|
|
439
|
+
}
|
|
440
|
+
const tokenId = outcomeIndex === 0 ? resolved.primaryTokenId : resolved.secondaryTokenId;
|
|
441
|
+
const points = await this.getPricesHistory({
|
|
442
|
+
tokenId,
|
|
443
|
+
interval,
|
|
444
|
+
fidelity: options?.fidelity,
|
|
445
|
+
startTs: options?.startTs,
|
|
446
|
+
endTs: options?.endTs,
|
|
447
|
+
});
|
|
448
|
+
// If no data returned, try fallback with 'max' interval (market may be closed)
|
|
449
|
+
if (points.length === 0 && interval !== 'max') {
|
|
450
|
+
const market = await this.getClobMarket(conditionId);
|
|
451
|
+
if (market?.closed) {
|
|
452
|
+
return this.getPricesHistory({
|
|
453
|
+
tokenId,
|
|
454
|
+
interval: 'max',
|
|
455
|
+
fidelity: options?.fidelity,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return points;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Get dual price lines (primary + secondary) from /prices-history API.
|
|
463
|
+
*
|
|
464
|
+
* Fetches price history for both outcomes in parallel and optionally
|
|
465
|
+
* computes spread analysis. For OHLCV dual candles from trade data,
|
|
466
|
+
* use `getDualKLinesOHLCV()` instead.
|
|
467
|
+
*
|
|
468
|
+
* @param conditionId - Market condition ID
|
|
469
|
+
* @param interval - Price history interval (1h, 6h, 1d, 1w, max)
|
|
470
|
+
* @param options - Query options
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```typescript
|
|
474
|
+
* const dual = await sdk.markets.getDualKLines(conditionId, '1d');
|
|
475
|
+
* console.log(dual.primary.length); // Primary outcome price points
|
|
476
|
+
* console.log(dual.secondary.length); // Secondary outcome price points
|
|
477
|
+
* console.log(dual.spreadAnalysis); // Spread analysis
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
async getDualKLines(conditionId, interval, options) {
|
|
481
|
+
const resolved = await this.resolveMarketTokens(conditionId);
|
|
482
|
+
if (!resolved) {
|
|
483
|
+
throw new PolymarketError(ErrorCode.MARKET_NOT_FOUND, `Cannot resolve tokens for market: ${conditionId}`);
|
|
484
|
+
}
|
|
485
|
+
const sharedOpts = {
|
|
486
|
+
fidelity: options?.fidelity,
|
|
487
|
+
startTs: options?.startTs,
|
|
488
|
+
endTs: options?.endTs,
|
|
489
|
+
};
|
|
490
|
+
const [primary, secondary] = await Promise.all([
|
|
491
|
+
this.getKLines(conditionId, interval, { ...sharedOpts, outcomeIndex: 0 }),
|
|
492
|
+
this.getKLines(conditionId, interval, { ...sharedOpts, outcomeIndex: 1 }),
|
|
493
|
+
]);
|
|
494
|
+
const spreadAnalysis = this.analyzePriceLineSpread(primary, secondary);
|
|
495
|
+
return {
|
|
496
|
+
conditionId,
|
|
497
|
+
interval,
|
|
498
|
+
fidelity: options?.fidelity,
|
|
499
|
+
primary,
|
|
500
|
+
secondary,
|
|
501
|
+
outcomes: resolved.outcomes,
|
|
502
|
+
spreadAnalysis,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
// ===== K-Line: OHLCV Candles (from Data API /trades) =====
|
|
506
|
+
/**
|
|
507
|
+
* Get OHLCV K-Line candles for a market (single token).
|
|
508
|
+
*
|
|
509
|
+
* Aggregates raw trade data into OHLCV candles. For lightweight
|
|
510
|
+
* price charts from /prices-history, use `getKLines()` instead.
|
|
409
511
|
*
|
|
410
512
|
* @param conditionId - Market condition ID
|
|
411
513
|
* @param interval - K-line interval (1s, 5s, 15s, 30s, 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d)
|
|
@@ -415,27 +517,45 @@ export class MarketService {
|
|
|
415
517
|
* @param options.outcomeIndex - Filter by outcome index (0 = primary, 1 = secondary)
|
|
416
518
|
* @param options.startTimestamp - Start timestamp (Unix ms) - filter trades after this time
|
|
417
519
|
* @param options.endTimestamp - End timestamp (Unix ms) - filter trades before this time
|
|
520
|
+
* @param options.paginate - Use paginated fetching for more complete data (default: false)
|
|
521
|
+
* @param options.maxTrades - Maximum trades when paginating (default: 5000)
|
|
418
522
|
*
|
|
419
523
|
* @example
|
|
420
524
|
* ```typescript
|
|
421
525
|
* // Get 5s candles for the last 15 minutes
|
|
422
526
|
* const now = Date.now();
|
|
423
|
-
* const candles = await sdk.markets.
|
|
527
|
+
* const candles = await sdk.markets.getKLinesOHLCV(conditionId, '5s', {
|
|
424
528
|
* startTimestamp: now - 15 * 60 * 1000,
|
|
425
529
|
* endTimestamp: now,
|
|
426
530
|
* });
|
|
531
|
+
*
|
|
532
|
+
* // Get all available trades with pagination
|
|
533
|
+
* const allCandles = await sdk.markets.getKLinesOHLCV(conditionId, '1h', {
|
|
534
|
+
* paginate: true,
|
|
535
|
+
* maxTrades: 5000,
|
|
536
|
+
* });
|
|
427
537
|
* ```
|
|
428
538
|
*/
|
|
429
|
-
async
|
|
539
|
+
async getKLinesOHLCV(conditionId, interval, options) {
|
|
430
540
|
if (!this.dataApi) {
|
|
431
|
-
throw new PolymarketError(ErrorCode.INVALID_CONFIG, 'DataApiClient is required for K-Line data');
|
|
541
|
+
throw new PolymarketError(ErrorCode.INVALID_CONFIG, 'DataApiClient is required for K-Line OHLCV data');
|
|
542
|
+
}
|
|
543
|
+
let trades;
|
|
544
|
+
if (options?.paginate) {
|
|
545
|
+
trades = await this.dataApi.getAllTrades({
|
|
546
|
+
market: conditionId,
|
|
547
|
+
startTimestamp: options?.startTimestamp,
|
|
548
|
+
endTimestamp: options?.endTimestamp,
|
|
549
|
+
}, options?.maxTrades || 5000);
|
|
550
|
+
}
|
|
551
|
+
else {
|
|
552
|
+
trades = await this.dataApi.getTrades({
|
|
553
|
+
market: conditionId,
|
|
554
|
+
limit: options?.limit || 1000,
|
|
555
|
+
startTimestamp: options?.startTimestamp,
|
|
556
|
+
endTimestamp: options?.endTimestamp,
|
|
557
|
+
});
|
|
432
558
|
}
|
|
433
|
-
const trades = await this.dataApi.getTrades({
|
|
434
|
-
market: conditionId,
|
|
435
|
-
limit: options?.limit || 1000,
|
|
436
|
-
startTimestamp: options?.startTimestamp,
|
|
437
|
-
endTimestamp: options?.endTimestamp,
|
|
438
|
-
});
|
|
439
559
|
// Filter by token/outcome if specified
|
|
440
560
|
let filteredTrades = trades;
|
|
441
561
|
if (options?.tokenId) {
|
|
@@ -447,7 +567,11 @@ export class MarketService {
|
|
|
447
567
|
return this.aggregateToKLines(filteredTrades, interval);
|
|
448
568
|
}
|
|
449
569
|
/**
|
|
450
|
-
* Get dual K-Lines (YES + NO tokens)
|
|
570
|
+
* Get dual OHLCV K-Lines (YES + NO tokens) from trade data.
|
|
571
|
+
*
|
|
572
|
+
* Aggregates raw trade data into dual OHLCV candles with spread analysis
|
|
573
|
+
* and optional real-time orderbook data. For lightweight dual price lines
|
|
574
|
+
* from /prices-history, use `getDualKLines()` instead.
|
|
451
575
|
*
|
|
452
576
|
* @param conditionId - Market condition ID
|
|
453
577
|
* @param interval - K-line interval (1s, 5s, 15s, 30s, 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d)
|
|
@@ -455,29 +579,41 @@ export class MarketService {
|
|
|
455
579
|
* @param options.limit - Maximum number of trades to fetch for aggregation (default: 1000)
|
|
456
580
|
* @param options.startTimestamp - Start timestamp (Unix ms) - filter trades after this time
|
|
457
581
|
* @param options.endTimestamp - End timestamp (Unix ms) - filter trades before this time
|
|
582
|
+
* @param options.paginate - Use paginated fetching for more complete data (default: false)
|
|
583
|
+
* @param options.maxTrades - Maximum trades when paginating (default: 5000)
|
|
458
584
|
*
|
|
459
585
|
* @example
|
|
460
586
|
* ```typescript
|
|
461
587
|
* // Get 15s dual K-lines for a 15-minute market
|
|
462
588
|
* const now = Date.now();
|
|
463
|
-
* const data = await sdk.markets.
|
|
589
|
+
* const data = await sdk.markets.getDualKLinesOHLCV(conditionId, '15s', {
|
|
464
590
|
* startTimestamp: now - 15 * 60 * 1000,
|
|
465
591
|
* endTimestamp: now,
|
|
466
592
|
* });
|
|
467
593
|
* console.log(`Up candles: ${data.yes.length}, Down candles: ${data.no.length}`);
|
|
468
594
|
* ```
|
|
469
595
|
*/
|
|
470
|
-
async
|
|
596
|
+
async getDualKLinesOHLCV(conditionId, interval, options) {
|
|
471
597
|
if (!this.dataApi) {
|
|
472
|
-
throw new PolymarketError(ErrorCode.INVALID_CONFIG, 'DataApiClient is required for K-Line data');
|
|
598
|
+
throw new PolymarketError(ErrorCode.INVALID_CONFIG, 'DataApiClient is required for K-Line OHLCV data');
|
|
473
599
|
}
|
|
474
600
|
const market = await this.getMarket(conditionId);
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
601
|
+
let trades;
|
|
602
|
+
if (options?.paginate) {
|
|
603
|
+
trades = await this.dataApi.getAllTrades({
|
|
604
|
+
market: conditionId,
|
|
605
|
+
startTimestamp: options?.startTimestamp,
|
|
606
|
+
endTimestamp: options?.endTimestamp,
|
|
607
|
+
}, options?.maxTrades || 5000);
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
trades = await this.dataApi.getTrades({
|
|
611
|
+
market: conditionId,
|
|
612
|
+
limit: options?.limit || 1000,
|
|
613
|
+
startTimestamp: options?.startTimestamp,
|
|
614
|
+
endTimestamp: options?.endTimestamp,
|
|
615
|
+
});
|
|
616
|
+
}
|
|
481
617
|
// Separate trades by outcome using index (more reliable than name matching)
|
|
482
618
|
// outcomeIndex 0 = primary (Yes/Up/Team1), outcomeIndex 1 = secondary (No/Down/Team2)
|
|
483
619
|
const yesTrades = trades.filter((t) => t.outcomeIndex === 0);
|
|
@@ -555,7 +691,7 @@ export class MarketService {
|
|
|
555
691
|
const limit = options?.limit || 500;
|
|
556
692
|
// Fetch data in parallel
|
|
557
693
|
const [dualKLines, binanceKLines] = await Promise.all([
|
|
558
|
-
this.
|
|
694
|
+
this.getDualKLinesOHLCV(conditionId, interval, { limit }),
|
|
559
695
|
this.binanceService.getKLines(UNDERLYING_TO_SYMBOL[underlying], binanceInterval, { limit }),
|
|
560
696
|
]);
|
|
561
697
|
// Create maps for quick lookup
|
|
@@ -770,6 +906,37 @@ export class MarketService {
|
|
|
770
906
|
}
|
|
771
907
|
return analysis;
|
|
772
908
|
}
|
|
909
|
+
/**
|
|
910
|
+
* Analyze spread from price line data (from /prices-history API).
|
|
911
|
+
*
|
|
912
|
+
* Aligns primary and secondary price points by timestamp and computes
|
|
913
|
+
* the sum and deviation from 1.0 at each aligned point.
|
|
914
|
+
*/
|
|
915
|
+
analyzePriceLineSpread(primary, secondary) {
|
|
916
|
+
const primaryMap = new Map(primary.map(p => [p.timestamp, p.price]));
|
|
917
|
+
const secondaryMap = new Map(secondary.map(p => [p.timestamp, p.price]));
|
|
918
|
+
const allTimestamps = [...new Set([...primaryMap.keys(), ...secondaryMap.keys()])].sort((a, b) => a - b);
|
|
919
|
+
let lastPrimary = 0.5;
|
|
920
|
+
let lastSecondary = 0.5;
|
|
921
|
+
const analysis = [];
|
|
922
|
+
for (const ts of allTimestamps) {
|
|
923
|
+
const pPrice = primaryMap.get(ts);
|
|
924
|
+
const sPrice = secondaryMap.get(ts);
|
|
925
|
+
if (pPrice !== undefined)
|
|
926
|
+
lastPrimary = pPrice;
|
|
927
|
+
if (sPrice !== undefined)
|
|
928
|
+
lastSecondary = sPrice;
|
|
929
|
+
const priceSum = lastPrimary + lastSecondary;
|
|
930
|
+
analysis.push({
|
|
931
|
+
timestamp: ts,
|
|
932
|
+
primaryPrice: lastPrimary,
|
|
933
|
+
secondaryPrice: lastSecondary,
|
|
934
|
+
priceSum,
|
|
935
|
+
priceSpread: priceSum - 1,
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
return analysis;
|
|
939
|
+
}
|
|
773
940
|
/**
|
|
774
941
|
* Calculate real-time spread from orderbook (for live trading)
|
|
775
942
|
*
|