skewtape 0.0.1 → 0.1.4
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/LICENSE +21 -0
- package/README.md +182 -0
- package/dist/cjs/backtest.d.ts +146 -0
- package/dist/cjs/backtest.js +410 -0
- package/dist/cjs/client.d.ts +255 -0
- package/dist/cjs/client.js +447 -0
- package/dist/cjs/download.d.ts +33 -0
- package/dist/cjs/download.js +160 -0
- package/dist/cjs/errors.d.ts +76 -0
- package/dist/cjs/errors.js +159 -0
- package/dist/cjs/exact.d.ts +36 -0
- package/dist/cjs/exact.js +168 -0
- package/dist/cjs/fees.d.ts +68 -0
- package/dist/cjs/fees.js +196 -0
- package/dist/cjs/geoblock.d.ts +48 -0
- package/dist/cjs/geoblock.js +78 -0
- package/dist/cjs/index.d.ts +31 -0
- package/dist/cjs/index.js +98 -0
- package/dist/cjs/kalshi.d.ts +261 -0
- package/dist/cjs/kalshi.js +416 -0
- package/dist/cjs/keccak.d.ts +10 -0
- package/dist/cjs/keccak.js +84 -0
- package/dist/cjs/msgpack.d.ts +15 -0
- package/dist/cjs/msgpack.js +112 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/paginated.d.ts +26 -0
- package/dist/cjs/paginated.js +51 -0
- package/dist/cjs/quote-math.d.ts +146 -0
- package/dist/cjs/quote-math.js +446 -0
- package/dist/cjs/quote.d.ts +108 -0
- package/dist/cjs/quote.js +307 -0
- package/dist/cjs/route-hyperliquid.d.ts +228 -0
- package/dist/cjs/route-hyperliquid.js +387 -0
- package/dist/cjs/route.d.ts +297 -0
- package/dist/cjs/route.js +516 -0
- package/dist/cjs/stream.d.ts +99 -0
- package/dist/cjs/stream.js +529 -0
- package/dist/cjs/version.d.ts +6 -0
- package/dist/cjs/version.js +9 -0
- package/dist/esm/backtest.d.ts +146 -0
- package/dist/esm/backtest.js +403 -0
- package/dist/esm/client.d.ts +255 -0
- package/dist/esm/client.js +441 -0
- package/dist/esm/download.d.ts +33 -0
- package/dist/esm/download.js +155 -0
- package/dist/esm/errors.d.ts +76 -0
- package/dist/esm/errors.js +142 -0
- package/dist/esm/exact.d.ts +36 -0
- package/dist/esm/exact.js +158 -0
- package/dist/esm/fees.d.ts +68 -0
- package/dist/esm/fees.js +185 -0
- package/dist/esm/geoblock.d.ts +48 -0
- package/dist/esm/geoblock.js +74 -0
- package/dist/esm/index.d.ts +31 -0
- package/dist/esm/index.js +30 -0
- package/dist/esm/kalshi.d.ts +261 -0
- package/dist/esm/kalshi.js +404 -0
- package/dist/esm/keccak.d.ts +10 -0
- package/dist/esm/keccak.js +80 -0
- package/dist/esm/msgpack.d.ts +15 -0
- package/dist/esm/msgpack.js +109 -0
- package/dist/esm/paginated.d.ts +26 -0
- package/dist/esm/paginated.js +47 -0
- package/dist/esm/quote-math.d.ts +146 -0
- package/dist/esm/quote-math.js +437 -0
- package/dist/esm/quote.d.ts +108 -0
- package/dist/esm/quote.js +297 -0
- package/dist/esm/route-hyperliquid.d.ts +228 -0
- package/dist/esm/route-hyperliquid.js +376 -0
- package/dist/esm/route.d.ts +297 -0
- package/dist/esm/route.js +507 -0
- package/dist/esm/stream.d.ts +99 -0
- package/dist/esm/stream.js +524 -0
- package/dist/esm/version.d.ts +6 -0
- package/dist/esm/version.js +6 -0
- package/package.json +48 -1
- package/src/backtest.ts +490 -0
- package/src/client.ts +595 -0
- package/src/download.ts +161 -0
- package/src/errors.ts +143 -0
- package/src/exact.ts +159 -0
- package/src/fees.ts +189 -0
- package/src/generated/schema.d.ts +2500 -0
- package/src/geoblock.ts +90 -0
- package/src/index.ts +146 -0
- package/src/kalshi.ts +563 -0
- package/src/keccak.ts +82 -0
- package/src/msgpack.ts +83 -0
- package/src/paginated.ts +51 -0
- package/src/quote-math.ts +555 -0
- package/src/quote.ts +324 -0
- package/src/route-hyperliquid.ts +457 -0
- package/src/route.ts +631 -0
- package/src/stream.ts +539 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sk.kalshi(): the optional live Kalshi client, under the user's own Kalshi
|
|
3
|
+
* membership (ARCHITECTURE 12, story E3; ADR-0022).
|
|
4
|
+
*
|
|
5
|
+
* It calls Kalshi's Trade API (/trade-api/v2) from the user's machine, read
|
|
6
|
+
* only: markets, one market, trades, the orderbook and candlesticks. It
|
|
7
|
+
* writes nothing to disk and its calls never go to a Skewtape host. It is
|
|
8
|
+
* not how Skewtape delivers Kalshi data: markets, fills, candles and
|
|
9
|
+
* resolutions ship from the API and the bundles under the Kalshi data
|
|
10
|
+
* licence; this client is a reading of the live venue beside them.
|
|
11
|
+
*
|
|
12
|
+
* A key is optional and comes from the options only (accessKey and
|
|
13
|
+
* privateKey, the user's own, from wherever the user keeps it); no
|
|
14
|
+
* environment variable is read. With one (the key id and the PEM private key,
|
|
15
|
+
* Ed25519 or RSA) every request carries the headers Kalshi's docs ask for,
|
|
16
|
+
* the timestamp in milliseconds, the method and the path signed with
|
|
17
|
+
* node:crypto, which puts the calls under the account's own rate-limit tier.
|
|
18
|
+
* Without one the calls are public requests. A 429 is retried with an
|
|
19
|
+
* exponential backoff (Kalshi sends no Retry-After); a request past timeout
|
|
20
|
+
* seconds (30 by default) and any other error is a KalshiError.
|
|
21
|
+
*
|
|
22
|
+
* Prices come back as Kalshi sends them, as dollar strings, and counts as
|
|
23
|
+
* fixed-point strings. Every row keeps the whole JSON object in raw for the
|
|
24
|
+
* fields not named here. Kalshi serves older trades and settled markets from
|
|
25
|
+
* its /historical routes, past a cutoff that moves; this client does not call
|
|
26
|
+
* them, so rows before the cutoff are not here: the history is Skewtape's
|
|
27
|
+
* (sk.fills, sk.markets).
|
|
28
|
+
*/
|
|
29
|
+
import { SkewtapeError, type ErrorFields } from "./errors.js";
|
|
30
|
+
import { Paginated } from "./paginated.js";
|
|
31
|
+
/** The production Trade API host Kalshi's docs prefer (api.elections.kalshi.com stays supported; pass baseUrl for it or for the demo host). */
|
|
32
|
+
export declare const DEFAULT_KALSHI_BASE_URL = "https://external-api.kalshi.com/trade-api/v2";
|
|
33
|
+
/** The matched-event member venues whose market_id is a Kalshi ticker. */
|
|
34
|
+
export declare const KALSHI_VENUES: ReadonlySet<string>;
|
|
35
|
+
export type KalshiPeriod = "1m" | "1h" | "1d";
|
|
36
|
+
/** Candlestick sizes, as sk.candles names them, to Kalshi's period_interval minutes. */
|
|
37
|
+
export declare const PERIODS: Readonly<Record<KalshiPeriod, 1 | 60 | 1440>>;
|
|
38
|
+
/**
|
|
39
|
+
* An error from Kalshi's API. status is the HTTP status, code and message
|
|
40
|
+
* what Kalshi sent ({"error": {"code", "message", "details"}}, or the flat
|
|
41
|
+
* {"code", "message", "details"} its docs' schema shows), details the detail
|
|
42
|
+
* string when there is one (an authentication error says why there).
|
|
43
|
+
*/
|
|
44
|
+
export declare class KalshiError extends SkewtapeError {
|
|
45
|
+
readonly details: string | undefined;
|
|
46
|
+
constructor(message: string, fields?: ErrorFields & {
|
|
47
|
+
details?: string | undefined;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/** The KalshiError for a non-2xx response, from either error shape. */
|
|
51
|
+
export declare function kalshiError(status: number, headers: Headers, body: string): KalshiError;
|
|
52
|
+
/**
|
|
53
|
+
* The series ticker a Kalshi market ticker starts with: KXSAMPLE for
|
|
54
|
+
* KXSAMPLE-26NOV05-A. Every market ticker is its event
|
|
55
|
+
* ticker and an outcome, and every event ticker its series ticker and a date
|
|
56
|
+
* or a strike, so the first segment is the series (true of every market in
|
|
57
|
+
* tape's Kalshi recordings, test/fixtures/kalshi); candlesticks takes
|
|
58
|
+
* seriesTicker when a market does not follow the pattern.
|
|
59
|
+
*/
|
|
60
|
+
export declare function seriesTickerOf(ticker: string): string;
|
|
61
|
+
/** One Kalshi market, as GET /markets lists it; series_ticker is derived from the ticker, raw is the whole row. */
|
|
62
|
+
export interface KalshiMarket {
|
|
63
|
+
ticker: string;
|
|
64
|
+
event_ticker: string | null;
|
|
65
|
+
series_ticker: string;
|
|
66
|
+
market_type: string | null;
|
|
67
|
+
title: string | null;
|
|
68
|
+
yes_sub_title: string | null;
|
|
69
|
+
no_sub_title: string | null;
|
|
70
|
+
status: string | null;
|
|
71
|
+
result: string | null;
|
|
72
|
+
open_time: Date | null;
|
|
73
|
+
close_time: Date | null;
|
|
74
|
+
expiration_time: Date | null;
|
|
75
|
+
yes_bid_dollars: string | null;
|
|
76
|
+
yes_ask_dollars: string | null;
|
|
77
|
+
no_bid_dollars: string | null;
|
|
78
|
+
no_ask_dollars: string | null;
|
|
79
|
+
last_price_dollars: string | null;
|
|
80
|
+
volume_fp: string | null;
|
|
81
|
+
volume_24h_fp: string | null;
|
|
82
|
+
open_interest_fp: string | null;
|
|
83
|
+
raw: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
export declare function marketFromRaw(raw: Record<string, unknown>): KalshiMarket;
|
|
86
|
+
/** One public trade, as GET /markets/trades lists it: the taker's outcome side and book side, both prices, the count. */
|
|
87
|
+
export interface KalshiTrade {
|
|
88
|
+
trade_id: string;
|
|
89
|
+
ticker: string | null;
|
|
90
|
+
created_time: Date | null;
|
|
91
|
+
count_fp: string | null;
|
|
92
|
+
yes_price_dollars: string | null;
|
|
93
|
+
no_price_dollars: string | null;
|
|
94
|
+
taker_outcome_side: string | null;
|
|
95
|
+
taker_book_side: string | null;
|
|
96
|
+
is_block_trade: boolean;
|
|
97
|
+
raw: Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
export declare function tradeFromRaw(raw: Record<string, unknown>): KalshiTrade;
|
|
100
|
+
/** One orderbook level: the price in dollars and the contracts at it. */
|
|
101
|
+
export interface KalshiLevel {
|
|
102
|
+
price_dollars: string;
|
|
103
|
+
count_fp: string;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The resting bids of one market, as GET /markets/{ticker}/orderbook returns
|
|
107
|
+
* them: yes bids and no bids, each ascending by price, so the best bid of a
|
|
108
|
+
* side is its last level (a no bid at p is a yes ask at 1 - p). Kalshi
|
|
109
|
+
* returns no asks and no order counts. A market with no book comes back with
|
|
110
|
+
* two empty sides.
|
|
111
|
+
*/
|
|
112
|
+
export interface KalshiOrderbook {
|
|
113
|
+
ticker: string;
|
|
114
|
+
yes: KalshiLevel[];
|
|
115
|
+
no: KalshiLevel[];
|
|
116
|
+
best_yes_bid: KalshiLevel | null;
|
|
117
|
+
best_no_bid: KalshiLevel | null;
|
|
118
|
+
raw: Record<string, unknown>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The book from Kalshi's reply. A reply without an orderbook_fp object is a
|
|
122
|
+
* shape this client does not know and throws KalshiError, so it is never read
|
|
123
|
+
* as an empty book; an empty or null side is an empty side.
|
|
124
|
+
*/
|
|
125
|
+
export declare function orderbookFromRaw(ticker: string, raw: Record<string, unknown>): KalshiOrderbook;
|
|
126
|
+
/** Open, high, low and close in dollars, null where the period had none. */
|
|
127
|
+
export interface KalshiOHLC {
|
|
128
|
+
open_dollars: string | null;
|
|
129
|
+
high_dollars: string | null;
|
|
130
|
+
low_dollars: string | null;
|
|
131
|
+
close_dollars: string | null;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* One candlestick: the period it ends (end_period, Kalshi's end_period_ts,
|
|
135
|
+
* inclusive) and starts (both null when Kalshi sends no end), its length in
|
|
136
|
+
* minutes; the trade price's open, high, low and close (null in a period with
|
|
137
|
+
* no trade), its volume and open interest, and the yes bid and yes ask
|
|
138
|
+
* distributions.
|
|
139
|
+
*/
|
|
140
|
+
export interface KalshiCandlestick {
|
|
141
|
+
end_period: Date | null;
|
|
142
|
+
start_period: Date | null;
|
|
143
|
+
period_minutes: number;
|
|
144
|
+
price: KalshiOHLC;
|
|
145
|
+
yes_bid: KalshiOHLC;
|
|
146
|
+
yes_ask: KalshiOHLC;
|
|
147
|
+
volume_fp: string | null;
|
|
148
|
+
open_interest_fp: string | null;
|
|
149
|
+
raw: Record<string, unknown>;
|
|
150
|
+
}
|
|
151
|
+
export declare function candlestickFromRaw(raw: Record<string, unknown>, periodMinutes: number): KalshiCandlestick;
|
|
152
|
+
/** What the join helpers read of a matched event: sk.events.get's MatchedEvent fits. */
|
|
153
|
+
export interface KalshiEventLike {
|
|
154
|
+
members: ReadonlyArray<{
|
|
155
|
+
venue: string;
|
|
156
|
+
market_id: string;
|
|
157
|
+
status: string;
|
|
158
|
+
}>;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The Kalshi tickers of a matched event: the market_id of its members on
|
|
162
|
+
* kalshi and kalshi_dflow (both use the ticker as the id), active ones unless
|
|
163
|
+
* includeInactive, in member order, each once.
|
|
164
|
+
*/
|
|
165
|
+
export declare function kalshiTickers(event: KalshiEventLike, includeInactive?: boolean): string[];
|
|
166
|
+
export interface KalshiOptions {
|
|
167
|
+
/** The user's own key id; with privateKey, every request is signed. Never read from the environment. */
|
|
168
|
+
accessKey?: string;
|
|
169
|
+
/** The user's own PEM private key (Ed25519 or RSA). */
|
|
170
|
+
privateKey?: string;
|
|
171
|
+
baseUrl?: string;
|
|
172
|
+
/** How many times a 429 is retried, with a backoff of 0.5, 1 and 2 seconds. Three by default. */
|
|
173
|
+
maxRetries?: number;
|
|
174
|
+
/** How long one request may take, in seconds; 30 by default. A request past it throws a KalshiError. */
|
|
175
|
+
timeout?: number;
|
|
176
|
+
/** The fetch to use; the global one by default. */
|
|
177
|
+
fetch?: typeof fetch;
|
|
178
|
+
/** How to wait, in seconds; a timer by default. Tests inject their own. */
|
|
179
|
+
sleep?: (seconds: number) => Promise<void>;
|
|
180
|
+
/** The clock, in milliseconds since the epoch; Date.now by default. */
|
|
181
|
+
now?: () => number;
|
|
182
|
+
}
|
|
183
|
+
export interface KalshiMarketsOptions {
|
|
184
|
+
/** open, closed, settled or unopened. */
|
|
185
|
+
status?: string;
|
|
186
|
+
seriesTicker?: string;
|
|
187
|
+
eventTicker?: string;
|
|
188
|
+
/** A list of tickers, in one request. */
|
|
189
|
+
tickers?: string[];
|
|
190
|
+
/** false (the default) leaves the multivariate collections out, as the Skewtape data does; true lists only them; null lists both. */
|
|
191
|
+
multivariate?: boolean | null;
|
|
192
|
+
limit?: number;
|
|
193
|
+
}
|
|
194
|
+
export interface KalshiTradesOptions {
|
|
195
|
+
ticker?: string;
|
|
196
|
+
/** A Date, an ISO string or Unix seconds. */
|
|
197
|
+
since?: Date | number | string;
|
|
198
|
+
until?: Date | number | string;
|
|
199
|
+
/** true keeps block trades only, false leaves them out; left out, all. */
|
|
200
|
+
blockTrades?: boolean;
|
|
201
|
+
limit?: number;
|
|
202
|
+
}
|
|
203
|
+
export interface KalshiCandlesticksOptions {
|
|
204
|
+
since: Date | number | string;
|
|
205
|
+
/** Now by default. */
|
|
206
|
+
until?: Date | number | string;
|
|
207
|
+
/** 1m, 1h or 1d (as sk.candles names them), or Kalshi's 1, 60 or 1440 minutes; 1h by default. */
|
|
208
|
+
period?: KalshiPeriod | 1 | 60 | 1440;
|
|
209
|
+
/** The series, derived from the ticker when left out. */
|
|
210
|
+
seriesTicker?: string;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The live Kalshi client. Build it as sk.kalshi(...) or on its own:
|
|
214
|
+
*
|
|
215
|
+
* const kalshi = sk.kalshi(); // public requests
|
|
216
|
+
* const kalshi = sk.kalshi({ accessKey: keyId, privateKey: pem }); // the user's own key
|
|
217
|
+
* for await (const market of kalshi.markets({ status: "open", seriesTicker: "KXSAMPLE" })) ...
|
|
218
|
+
*
|
|
219
|
+
* The key id and the private key come from the options only, never from the
|
|
220
|
+
* environment, and one without the other is refused.
|
|
221
|
+
*/
|
|
222
|
+
export declare class Kalshi {
|
|
223
|
+
readonly baseUrl: string;
|
|
224
|
+
readonly maxRetries: number;
|
|
225
|
+
readonly timeout: number;
|
|
226
|
+
/** Whether the calls are signed with a key. */
|
|
227
|
+
readonly authenticated: boolean;
|
|
228
|
+
private readonly pathPrefix;
|
|
229
|
+
private readonly signer;
|
|
230
|
+
private readonly fetchImpl;
|
|
231
|
+
private readonly sleep;
|
|
232
|
+
private readonly now;
|
|
233
|
+
constructor(options?: KalshiOptions);
|
|
234
|
+
private get;
|
|
235
|
+
/** A timeout or abort, while the request or its body is read, as the KalshiError naming the seconds; anything else as it came. */
|
|
236
|
+
private timedOut;
|
|
237
|
+
/** GET /markets: by status, series, event or tickers, the multivariate collections left out unless asked for. Settled markets past Kalshi's historical cutoff are not listed here; sk.markets has them. */
|
|
238
|
+
markets(options?: KalshiMarketsOptions): Paginated<KalshiMarket>;
|
|
239
|
+
/** GET /markets/{ticker}: one market. */
|
|
240
|
+
market(ticker: string): Promise<KalshiMarket>;
|
|
241
|
+
/** GET /markets/trades: public trades of one market or of all, from since to until, in Kalshi's order (newest first). Trades older than Kalshi's historical cutoff are not here; sk.fills has them. */
|
|
242
|
+
trades(options?: KalshiTradesOptions): Paginated<KalshiTrade>;
|
|
243
|
+
/** GET /markets/{ticker}/orderbook: the resting bids of one market, every level or the best depth levels of each side (1 to 100). */
|
|
244
|
+
orderbook(ticker: string, options?: {
|
|
245
|
+
depth?: number;
|
|
246
|
+
}): Promise<KalshiOrderbook>;
|
|
247
|
+
/**
|
|
248
|
+
* GET /series/{series}/markets/{ticker}/candlesticks: the sticks of one size
|
|
249
|
+
* ending from since to until (now by default). Kalshi serves at most 5,000
|
|
250
|
+
* periods a request and a 1d stick is a New York day.
|
|
251
|
+
*/
|
|
252
|
+
candlesticks(ticker: string, options: KalshiCandlesticksOptions): Promise<KalshiCandlestick[]>;
|
|
253
|
+
/** The Kalshi tickers of a Skewtape matched event: the market_id of its kalshi and kalshi_dflow members, active ones unless includeInactive. */
|
|
254
|
+
tickers(event: KalshiEventLike, options?: {
|
|
255
|
+
includeInactive?: boolean;
|
|
256
|
+
}): string[];
|
|
257
|
+
/** The live Kalshi markets of a matched event, one request (GET /markets?tickers=), in the members' order; none for an event with no Kalshi member. */
|
|
258
|
+
marketsFor(event: KalshiEventLike, options?: {
|
|
259
|
+
includeInactive?: boolean;
|
|
260
|
+
}): Promise<KalshiMarket[]>;
|
|
261
|
+
}
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* sk.kalshi(): the optional live Kalshi client, under the user's own Kalshi
|
|
4
|
+
* membership (ARCHITECTURE 12, story E3; ADR-0022).
|
|
5
|
+
*
|
|
6
|
+
* It calls Kalshi's Trade API (/trade-api/v2) from the user's machine, read
|
|
7
|
+
* only: markets, one market, trades, the orderbook and candlesticks. It
|
|
8
|
+
* writes nothing to disk and its calls never go to a Skewtape host. It is
|
|
9
|
+
* not how Skewtape delivers Kalshi data: markets, fills, candles and
|
|
10
|
+
* resolutions ship from the API and the bundles under the Kalshi data
|
|
11
|
+
* licence; this client is a reading of the live venue beside them.
|
|
12
|
+
*
|
|
13
|
+
* A key is optional and comes from the options only (accessKey and
|
|
14
|
+
* privateKey, the user's own, from wherever the user keeps it); no
|
|
15
|
+
* environment variable is read. With one (the key id and the PEM private key,
|
|
16
|
+
* Ed25519 or RSA) every request carries the headers Kalshi's docs ask for,
|
|
17
|
+
* the timestamp in milliseconds, the method and the path signed with
|
|
18
|
+
* node:crypto, which puts the calls under the account's own rate-limit tier.
|
|
19
|
+
* Without one the calls are public requests. A 429 is retried with an
|
|
20
|
+
* exponential backoff (Kalshi sends no Retry-After); a request past timeout
|
|
21
|
+
* seconds (30 by default) and any other error is a KalshiError.
|
|
22
|
+
*
|
|
23
|
+
* Prices come back as Kalshi sends them, as dollar strings, and counts as
|
|
24
|
+
* fixed-point strings. Every row keeps the whole JSON object in raw for the
|
|
25
|
+
* fields not named here. Kalshi serves older trades and settled markets from
|
|
26
|
+
* its /historical routes, past a cutoff that moves; this client does not call
|
|
27
|
+
* them, so rows before the cutoff are not here: the history is Skewtape's
|
|
28
|
+
* (sk.fills, sk.markets).
|
|
29
|
+
*/
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.Kalshi = exports.KalshiError = exports.PERIODS = exports.KALSHI_VENUES = exports.DEFAULT_KALSHI_BASE_URL = void 0;
|
|
32
|
+
exports.kalshiError = kalshiError;
|
|
33
|
+
exports.seriesTickerOf = seriesTickerOf;
|
|
34
|
+
exports.marketFromRaw = marketFromRaw;
|
|
35
|
+
exports.tradeFromRaw = tradeFromRaw;
|
|
36
|
+
exports.orderbookFromRaw = orderbookFromRaw;
|
|
37
|
+
exports.candlestickFromRaw = candlestickFromRaw;
|
|
38
|
+
exports.kalshiTickers = kalshiTickers;
|
|
39
|
+
const node_crypto_1 = require("node:crypto");
|
|
40
|
+
const errors_js_1 = require("./errors.js");
|
|
41
|
+
const paginated_js_1 = require("./paginated.js");
|
|
42
|
+
const version_js_1 = require("./version.js");
|
|
43
|
+
/** The production Trade API host Kalshi's docs prefer (api.elections.kalshi.com stays supported; pass baseUrl for it or for the demo host). */
|
|
44
|
+
exports.DEFAULT_KALSHI_BASE_URL = "https://external-api.kalshi.com/trade-api/v2";
|
|
45
|
+
/** The matched-event member venues whose market_id is a Kalshi ticker. */
|
|
46
|
+
exports.KALSHI_VENUES = new Set(["kalshi", "kalshi_dflow"]);
|
|
47
|
+
/** Candlestick sizes, as sk.candles names them, to Kalshi's period_interval minutes. */
|
|
48
|
+
exports.PERIODS = { "1m": 1, "1h": 60, "1d": 1440 };
|
|
49
|
+
/**
|
|
50
|
+
* An error from Kalshi's API. status is the HTTP status, code and message
|
|
51
|
+
* what Kalshi sent ({"error": {"code", "message", "details"}}, or the flat
|
|
52
|
+
* {"code", "message", "details"} its docs' schema shows), details the detail
|
|
53
|
+
* string when there is one (an authentication error says why there).
|
|
54
|
+
*/
|
|
55
|
+
class KalshiError extends errors_js_1.SkewtapeError {
|
|
56
|
+
details;
|
|
57
|
+
constructor(message, fields = {}) {
|
|
58
|
+
super(message, fields);
|
|
59
|
+
this.details = fields.details;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.KalshiError = KalshiError;
|
|
63
|
+
function str(v) {
|
|
64
|
+
return typeof v === "string" ? v : null;
|
|
65
|
+
}
|
|
66
|
+
function record(v) {
|
|
67
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? v : {};
|
|
68
|
+
}
|
|
69
|
+
/** The KalshiError for a non-2xx response, from either error shape. */
|
|
70
|
+
function kalshiError(status, headers, body) {
|
|
71
|
+
let parsed;
|
|
72
|
+
try {
|
|
73
|
+
parsed = body ? JSON.parse(body) : {};
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
parsed = undefined;
|
|
77
|
+
}
|
|
78
|
+
const outer = record(parsed);
|
|
79
|
+
const e = "error" in outer && outer.error !== null && typeof outer.error === "object" ? record(outer.error) : outer;
|
|
80
|
+
const code = str(e.code) ?? undefined;
|
|
81
|
+
const message = str(e.message) ?? `Kalshi HTTP ${status}${code ? ` ${code}` : ""}`;
|
|
82
|
+
return new KalshiError(message, { status, code, details: str(e.details) ?? undefined, retryAfter: (0, errors_js_1.retryAfterSeconds)(headers) });
|
|
83
|
+
}
|
|
84
|
+
/** A Kalshi RFC 3339 time as a Date, null when absent or odd. */
|
|
85
|
+
function time(v) {
|
|
86
|
+
if (typeof v !== "string" || v === "")
|
|
87
|
+
return null;
|
|
88
|
+
const d = new Date(v);
|
|
89
|
+
return Number.isNaN(d.getTime()) ? null : d;
|
|
90
|
+
}
|
|
91
|
+
/** A moment as Kalshi's Unix seconds: a Date, an ISO string, or seconds already. */
|
|
92
|
+
function unix(v) {
|
|
93
|
+
if (v instanceof Date)
|
|
94
|
+
return Math.floor(v.getTime() / 1000);
|
|
95
|
+
if (typeof v === "number")
|
|
96
|
+
return Math.floor(v);
|
|
97
|
+
const d = new Date(v);
|
|
98
|
+
if (Number.isNaN(d.getTime()))
|
|
99
|
+
throw new errors_js_1.ConfigurationError(`${v} is not a time`);
|
|
100
|
+
return Math.floor(d.getTime() / 1000);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The series ticker a Kalshi market ticker starts with: KXSAMPLE for
|
|
104
|
+
* KXSAMPLE-26NOV05-A. Every market ticker is its event
|
|
105
|
+
* ticker and an outcome, and every event ticker its series ticker and a date
|
|
106
|
+
* or a strike, so the first segment is the series (true of every market in
|
|
107
|
+
* tape's Kalshi recordings, test/fixtures/kalshi); candlesticks takes
|
|
108
|
+
* seriesTicker when a market does not follow the pattern.
|
|
109
|
+
*/
|
|
110
|
+
function seriesTickerOf(ticker) {
|
|
111
|
+
return ticker.split("-", 1)[0] ?? ticker;
|
|
112
|
+
}
|
|
113
|
+
function marketFromRaw(raw) {
|
|
114
|
+
const ticker = str(raw.ticker) ?? "";
|
|
115
|
+
return {
|
|
116
|
+
ticker,
|
|
117
|
+
event_ticker: str(raw.event_ticker),
|
|
118
|
+
series_ticker: seriesTickerOf(ticker),
|
|
119
|
+
market_type: str(raw.market_type),
|
|
120
|
+
title: str(raw.title),
|
|
121
|
+
yes_sub_title: str(raw.yes_sub_title),
|
|
122
|
+
no_sub_title: str(raw.no_sub_title),
|
|
123
|
+
status: str(raw.status),
|
|
124
|
+
result: str(raw.result),
|
|
125
|
+
open_time: time(raw.open_time),
|
|
126
|
+
close_time: time(raw.close_time),
|
|
127
|
+
expiration_time: time(raw.expiration_time),
|
|
128
|
+
yes_bid_dollars: str(raw.yes_bid_dollars),
|
|
129
|
+
yes_ask_dollars: str(raw.yes_ask_dollars),
|
|
130
|
+
no_bid_dollars: str(raw.no_bid_dollars),
|
|
131
|
+
no_ask_dollars: str(raw.no_ask_dollars),
|
|
132
|
+
last_price_dollars: str(raw.last_price_dollars),
|
|
133
|
+
volume_fp: str(raw.volume_fp),
|
|
134
|
+
volume_24h_fp: str(raw.volume_24h_fp),
|
|
135
|
+
open_interest_fp: str(raw.open_interest_fp),
|
|
136
|
+
raw,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function tradeFromRaw(raw) {
|
|
140
|
+
return {
|
|
141
|
+
trade_id: str(raw.trade_id) ?? "",
|
|
142
|
+
ticker: str(raw.ticker),
|
|
143
|
+
created_time: time(raw.created_time),
|
|
144
|
+
count_fp: str(raw.count_fp),
|
|
145
|
+
yes_price_dollars: str(raw.yes_price_dollars),
|
|
146
|
+
no_price_dollars: str(raw.no_price_dollars),
|
|
147
|
+
taker_outcome_side: str(raw.taker_outcome_side),
|
|
148
|
+
taker_book_side: str(raw.taker_book_side),
|
|
149
|
+
is_block_trade: raw.is_block_trade === true,
|
|
150
|
+
raw,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function levels(rows) {
|
|
154
|
+
if (!Array.isArray(rows))
|
|
155
|
+
return [];
|
|
156
|
+
const out = [];
|
|
157
|
+
for (const row of rows) {
|
|
158
|
+
if (Array.isArray(row) && row.length === 2 && typeof row[0] === "string" && typeof row[1] === "string")
|
|
159
|
+
out.push({ price_dollars: row[0], count_fp: row[1] });
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The book from Kalshi's reply. A reply without an orderbook_fp object is a
|
|
165
|
+
* shape this client does not know and throws KalshiError, so it is never read
|
|
166
|
+
* as an empty book; an empty or null side is an empty side.
|
|
167
|
+
*/
|
|
168
|
+
function orderbookFromRaw(ticker, raw) {
|
|
169
|
+
const fp = raw.orderbook_fp;
|
|
170
|
+
if (fp === null || typeof fp !== "object" || Array.isArray(fp)) {
|
|
171
|
+
throw new KalshiError(`Kalshi's orderbook reply for ${ticker} has no orderbook_fp object: a shape this client does not know`, { status: 200 });
|
|
172
|
+
}
|
|
173
|
+
const book = record(fp);
|
|
174
|
+
const yes = levels(book.yes_dollars);
|
|
175
|
+
const no = levels(book.no_dollars);
|
|
176
|
+
return { ticker, yes, no, best_yes_bid: yes.at(-1) ?? null, best_no_bid: no.at(-1) ?? null, raw };
|
|
177
|
+
}
|
|
178
|
+
function ohlc(raw) {
|
|
179
|
+
const r = record(raw);
|
|
180
|
+
return { open_dollars: str(r.open_dollars), high_dollars: str(r.high_dollars), low_dollars: str(r.low_dollars), close_dollars: str(r.close_dollars) };
|
|
181
|
+
}
|
|
182
|
+
function candlestickFromRaw(raw, periodMinutes) {
|
|
183
|
+
const end = typeof raw.end_period_ts === "number" ? new Date(raw.end_period_ts * 1000) : null;
|
|
184
|
+
return {
|
|
185
|
+
end_period: end,
|
|
186
|
+
start_period: end === null ? null : new Date(end.getTime() - periodMinutes * 60_000),
|
|
187
|
+
period_minutes: periodMinutes,
|
|
188
|
+
price: ohlc(raw.price),
|
|
189
|
+
yes_bid: ohlc(raw.yes_bid),
|
|
190
|
+
yes_ask: ohlc(raw.yes_ask),
|
|
191
|
+
volume_fp: str(raw.volume_fp),
|
|
192
|
+
open_interest_fp: str(raw.open_interest_fp),
|
|
193
|
+
raw,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Signs a request as Kalshi's docs say: the timestamp in milliseconds, the
|
|
198
|
+
* method and the path (from /trade-api/v2, no query string), with Ed25519 or
|
|
199
|
+
* RSA-PSS SHA-256 (MGF1 SHA-256, the salt the digest's length), base64, in
|
|
200
|
+
* KALSHI-ACCESS-KEY, KALSHI-ACCESS-SIGNATURE and KALSHI-ACCESS-TIMESTAMP.
|
|
201
|
+
*/
|
|
202
|
+
class Signer {
|
|
203
|
+
accessKey;
|
|
204
|
+
signMessage;
|
|
205
|
+
constructor(accessKey, pem) {
|
|
206
|
+
this.accessKey = accessKey;
|
|
207
|
+
let key;
|
|
208
|
+
try {
|
|
209
|
+
key = (0, node_crypto_1.createPrivateKey)(pem);
|
|
210
|
+
}
|
|
211
|
+
catch (err) {
|
|
212
|
+
throw new errors_js_1.ConfigurationError(`the Kalshi private key is not a PEM private key: ${err.message}`);
|
|
213
|
+
}
|
|
214
|
+
if (key.asymmetricKeyType === "ed25519") {
|
|
215
|
+
this.signMessage = (message) => (0, node_crypto_1.sign)(null, message, key);
|
|
216
|
+
}
|
|
217
|
+
else if (key.asymmetricKeyType === "rsa") {
|
|
218
|
+
this.signMessage = (message) => (0, node_crypto_1.sign)("sha256", message, { key, padding: node_crypto_1.constants.RSA_PKCS1_PSS_PADDING, saltLength: node_crypto_1.constants.RSA_PSS_SALTLEN_DIGEST });
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
throw new errors_js_1.ConfigurationError(`the Kalshi private key is ${key.asymmetricKeyType ?? "unknown"}; Kalshi keys are Ed25519 or RSA`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
headers(method, path, timestampMs) {
|
|
225
|
+
const signature = this.signMessage(Buffer.from(`${timestampMs}${method}${path}`));
|
|
226
|
+
return { "KALSHI-ACCESS-KEY": this.accessKey, "KALSHI-ACCESS-SIGNATURE": signature.toString("base64"), "KALSHI-ACCESS-TIMESTAMP": String(timestampMs) };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* The Kalshi tickers of a matched event: the market_id of its members on
|
|
231
|
+
* kalshi and kalshi_dflow (both use the ticker as the id), active ones unless
|
|
232
|
+
* includeInactive, in member order, each once.
|
|
233
|
+
*/
|
|
234
|
+
function kalshiTickers(event, includeInactive = false) {
|
|
235
|
+
const out = [];
|
|
236
|
+
for (const m of event.members) {
|
|
237
|
+
if (exports.KALSHI_VENUES.has(m.venue) && m.market_id && (includeInactive || m.status === "active") && !out.includes(m.market_id))
|
|
238
|
+
out.push(m.market_id);
|
|
239
|
+
}
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
function defaultSleep(seconds) {
|
|
243
|
+
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* The live Kalshi client. Build it as sk.kalshi(...) or on its own:
|
|
247
|
+
*
|
|
248
|
+
* const kalshi = sk.kalshi(); // public requests
|
|
249
|
+
* const kalshi = sk.kalshi({ accessKey: keyId, privateKey: pem }); // the user's own key
|
|
250
|
+
* for await (const market of kalshi.markets({ status: "open", seriesTicker: "KXSAMPLE" })) ...
|
|
251
|
+
*
|
|
252
|
+
* The key id and the private key come from the options only, never from the
|
|
253
|
+
* environment, and one without the other is refused.
|
|
254
|
+
*/
|
|
255
|
+
class Kalshi {
|
|
256
|
+
baseUrl;
|
|
257
|
+
maxRetries;
|
|
258
|
+
timeout;
|
|
259
|
+
/** Whether the calls are signed with a key. */
|
|
260
|
+
authenticated;
|
|
261
|
+
pathPrefix;
|
|
262
|
+
signer;
|
|
263
|
+
fetchImpl;
|
|
264
|
+
sleep;
|
|
265
|
+
now;
|
|
266
|
+
constructor(options = {}) {
|
|
267
|
+
const accessKey = options.accessKey || undefined;
|
|
268
|
+
const privateKey = options.privateKey || undefined;
|
|
269
|
+
if ((accessKey === undefined) !== (privateKey === undefined)) {
|
|
270
|
+
throw new errors_js_1.ConfigurationError("a Kalshi key is the key id and the private key together: pass accessKey and privateKey");
|
|
271
|
+
}
|
|
272
|
+
if (options.maxRetries !== undefined && options.maxRetries < 0)
|
|
273
|
+
throw new errors_js_1.ConfigurationError("maxRetries must not be negative");
|
|
274
|
+
if (options.timeout !== undefined && !(options.timeout > 0))
|
|
275
|
+
throw new errors_js_1.ConfigurationError("timeout is seconds, above zero");
|
|
276
|
+
this.timeout = options.timeout ?? 30;
|
|
277
|
+
this.baseUrl = (options.baseUrl ?? exports.DEFAULT_KALSHI_BASE_URL).replace(/\/+$/, "");
|
|
278
|
+
this.pathPrefix = new URL(this.baseUrl).pathname.replace(/\/+$/, "");
|
|
279
|
+
this.signer = accessKey !== undefined && privateKey !== undefined ? new Signer(accessKey, privateKey) : undefined;
|
|
280
|
+
this.authenticated = this.signer !== undefined;
|
|
281
|
+
this.maxRetries = options.maxRetries ?? 3;
|
|
282
|
+
this.fetchImpl = options.fetch ?? fetch;
|
|
283
|
+
this.sleep = options.sleep ?? defaultSleep;
|
|
284
|
+
this.now = options.now ?? Date.now;
|
|
285
|
+
}
|
|
286
|
+
async get(path, query = {}) {
|
|
287
|
+
const url = new URL(this.baseUrl + path);
|
|
288
|
+
for (const [k, v] of Object.entries(query))
|
|
289
|
+
if (v !== undefined)
|
|
290
|
+
url.searchParams.set(k, String(v));
|
|
291
|
+
for (let attempt = 0;; attempt++) {
|
|
292
|
+
const headers = { Accept: "application/json", "User-Agent": `skewtape-ts/${version_js_1.VERSION}` };
|
|
293
|
+
if (this.signer)
|
|
294
|
+
Object.assign(headers, this.signer.headers("GET", this.pathPrefix + path, this.now()));
|
|
295
|
+
let response;
|
|
296
|
+
try {
|
|
297
|
+
// The timeout covers the whole request, the body read included. A
|
|
298
|
+
// redirect is not followed, as Python's client follows none: a 3xx is
|
|
299
|
+
// a KalshiError naming its status.
|
|
300
|
+
response = await this.fetchImpl(url.toString(), { method: "GET", redirect: "manual", headers, signal: AbortSignal.timeout(this.timeout * 1000) });
|
|
301
|
+
}
|
|
302
|
+
catch (err) {
|
|
303
|
+
throw this.timedOut(err);
|
|
304
|
+
}
|
|
305
|
+
// A browser's fetch hides a manual redirect's status: say what it was.
|
|
306
|
+
if (response.type === "opaqueredirect")
|
|
307
|
+
throw new KalshiError("Kalshi answered with a redirect, which this client does not follow", { status: response.status });
|
|
308
|
+
if (response.status === 429 && attempt < this.maxRetries) {
|
|
309
|
+
// The body is not read; cancelling it frees the connection.
|
|
310
|
+
await response.body?.cancel();
|
|
311
|
+
await this.sleep((0, errors_js_1.retryAfterSeconds)(response.headers) ?? 0.5 * 2 ** attempt);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
let body;
|
|
315
|
+
try {
|
|
316
|
+
body = await response.text();
|
|
317
|
+
}
|
|
318
|
+
catch (err) {
|
|
319
|
+
throw this.timedOut(err);
|
|
320
|
+
}
|
|
321
|
+
if (response.ok) {
|
|
322
|
+
try {
|
|
323
|
+
return JSON.parse(body);
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
throw new KalshiError("Kalshi sent a body that is not JSON", { status: response.status });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
throw kalshiError(response.status, response.headers, body);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/** A timeout or abort, while the request or its body is read, as the KalshiError naming the seconds; anything else as it came. */
|
|
333
|
+
timedOut(err) {
|
|
334
|
+
if (err instanceof Error && (err.name === "TimeoutError" || err.name === "AbortError"))
|
|
335
|
+
return new KalshiError(`the Kalshi request took longer than ${this.timeout} s`);
|
|
336
|
+
return err;
|
|
337
|
+
}
|
|
338
|
+
/** GET /markets: by status, series, event or tickers, the multivariate collections left out unless asked for. Settled markets past Kalshi's historical cutoff are not listed here; sk.markets has them. */
|
|
339
|
+
markets(options = {}) {
|
|
340
|
+
const mve = options.multivariate === null ? undefined : options.multivariate ? "only" : "exclude";
|
|
341
|
+
return new paginated_js_1.Paginated(async (cursor) => {
|
|
342
|
+
const body = record(await this.get("/markets", {
|
|
343
|
+
limit: options.limit ?? 100,
|
|
344
|
+
cursor,
|
|
345
|
+
status: options.status,
|
|
346
|
+
series_ticker: options.seriesTicker,
|
|
347
|
+
event_ticker: options.eventTicker,
|
|
348
|
+
tickers: options.tickers?.length ? options.tickers.join(",") : undefined,
|
|
349
|
+
mve_filter: mve,
|
|
350
|
+
}));
|
|
351
|
+
const rows = Array.isArray(body.markets) ? body.markets : [];
|
|
352
|
+
return { items: rows.map((m) => marketFromRaw(record(m))), nextCursor: str(body.cursor) || null };
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
/** GET /markets/{ticker}: one market. */
|
|
356
|
+
async market(ticker) {
|
|
357
|
+
const body = record(await this.get(`/markets/${encodeURIComponent(ticker)}`));
|
|
358
|
+
if (body.market === null || typeof body.market !== "object")
|
|
359
|
+
throw new KalshiError(`Kalshi sent no market for ${ticker}`, { status: 200 });
|
|
360
|
+
return marketFromRaw(record(body.market));
|
|
361
|
+
}
|
|
362
|
+
/** GET /markets/trades: public trades of one market or of all, from since to until, in Kalshi's order (newest first). Trades older than Kalshi's historical cutoff are not here; sk.fills has them. */
|
|
363
|
+
trades(options = {}) {
|
|
364
|
+
return new paginated_js_1.Paginated(async (cursor) => {
|
|
365
|
+
const body = record(await this.get("/markets/trades", {
|
|
366
|
+
limit: options.limit ?? 100,
|
|
367
|
+
cursor,
|
|
368
|
+
ticker: options.ticker,
|
|
369
|
+
min_ts: options.since === undefined ? undefined : unix(options.since),
|
|
370
|
+
max_ts: options.until === undefined ? undefined : unix(options.until),
|
|
371
|
+
is_block_trade: options.blockTrades === undefined ? undefined : String(options.blockTrades),
|
|
372
|
+
}));
|
|
373
|
+
const rows = Array.isArray(body.trades) ? body.trades : [];
|
|
374
|
+
return { items: rows.map((t) => tradeFromRaw(record(t))), nextCursor: str(body.cursor) || null };
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
/** GET /markets/{ticker}/orderbook: the resting bids of one market, every level or the best depth levels of each side (1 to 100). */
|
|
378
|
+
async orderbook(ticker, options = {}) {
|
|
379
|
+
const body = record(await this.get(`/markets/${encodeURIComponent(ticker)}/orderbook`, { depth: options.depth }));
|
|
380
|
+
return orderbookFromRaw(ticker, body);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* GET /series/{series}/markets/{ticker}/candlesticks: the sticks of one size
|
|
384
|
+
* ending from since to until (now by default). Kalshi serves at most 5,000
|
|
385
|
+
* periods a request and a 1d stick is a New York day.
|
|
386
|
+
*/
|
|
387
|
+
async candlesticks(ticker, options) {
|
|
388
|
+
const period = options.period ?? "1h";
|
|
389
|
+
const minutes = typeof period === "string" ? exports.PERIODS[period] : period;
|
|
390
|
+
if (minutes !== 1 && minutes !== 60 && minutes !== 1440)
|
|
391
|
+
throw new errors_js_1.ConfigurationError(`period is 1m, 1h or 1d (or 1, 60 or 1440 minutes), not ${String(period)}`);
|
|
392
|
+
const series = options.seriesTicker ?? seriesTickerOf(ticker);
|
|
393
|
+
const body = record(await this.get(`/series/${encodeURIComponent(series)}/markets/${encodeURIComponent(ticker)}/candlesticks`, {
|
|
394
|
+
start_ts: unix(options.since),
|
|
395
|
+
end_ts: options.until === undefined ? Math.floor(this.now() / 1000) : unix(options.until),
|
|
396
|
+
period_interval: minutes,
|
|
397
|
+
}));
|
|
398
|
+
const rows = Array.isArray(body.candlesticks) ? body.candlesticks : [];
|
|
399
|
+
return rows.map((s) => candlestickFromRaw(record(s), minutes));
|
|
400
|
+
}
|
|
401
|
+
/** The Kalshi tickers of a Skewtape matched event: the market_id of its kalshi and kalshi_dflow members, active ones unless includeInactive. */
|
|
402
|
+
tickers(event, options = {}) {
|
|
403
|
+
return kalshiTickers(event, options.includeInactive ?? false);
|
|
404
|
+
}
|
|
405
|
+
/** The live Kalshi markets of a matched event, one request (GET /markets?tickers=), in the members' order; none for an event with no Kalshi member. */
|
|
406
|
+
async marketsFor(event, options = {}) {
|
|
407
|
+
const wanted = this.tickers(event, options);
|
|
408
|
+
if (wanted.length === 0)
|
|
409
|
+
return [];
|
|
410
|
+
const byTicker = new Map();
|
|
411
|
+
for await (const m of this.markets({ tickers: wanted }))
|
|
412
|
+
byTicker.set(m.ticker, m);
|
|
413
|
+
return wanted.flatMap((t) => byTicker.get(t) ?? []);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
exports.Kalshi = Kalshi;
|