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,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sk.geoblock: whether a venue would accept orders from this machine's
|
|
3
|
+
* network location, as the venue itself says (ARCHITECTURE 13.3).
|
|
4
|
+
*
|
|
5
|
+
* const result = await geoblock.polymarket();
|
|
6
|
+
* if (result.status !== "allowed") {
|
|
7
|
+
* // do not route this user's orders to Polymarket
|
|
8
|
+
* }
|
|
9
|
+
*
|
|
10
|
+
* It asks the venue, not Skewtape, and answers "unknown" on any failure: a
|
|
11
|
+
* network error, a timeout, an unexpected status or body. It never answers
|
|
12
|
+
* "allowed" unless the venue said so. The developer is responsible for their
|
|
13
|
+
* users: this is one check for the machine it runs on, not a licence, and it
|
|
14
|
+
* says nothing about the venue's terms or the law where the user is.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* GET https://polymarket.com/api/geoblock answers {"blocked", "ip", "country",
|
|
18
|
+
* "region"} ("This endpoint is on polymarket.com, not the API servers",
|
|
19
|
+
* docs.polymarket.com/api-reference/geoblock.md, read 14 Nov 2026). The page
|
|
20
|
+
* lists blocked and close-only jurisdictions and does not say how close-only
|
|
21
|
+
* ones answer, so "allowed" means only that the venue answered blocked false.
|
|
22
|
+
*/
|
|
23
|
+
export declare const POLYMARKET_GEOBLOCK_URL = "https://polymarket.com/api/geoblock";
|
|
24
|
+
export declare const GEOBLOCK_DEFAULT_TIMEOUT_MS = 5000;
|
|
25
|
+
export interface GeoblockResult {
|
|
26
|
+
/** "allowed" (the venue answered blocked false), "blocked" (blocked true), or "unknown" (anything else; detail says why). */
|
|
27
|
+
status: "allowed" | "blocked" | "unknown";
|
|
28
|
+
allowed: boolean;
|
|
29
|
+
detail: string;
|
|
30
|
+
/** ISO 3166-1 alpha-2, as the venue gave it. */
|
|
31
|
+
country?: string;
|
|
32
|
+
region?: string;
|
|
33
|
+
ip?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface GeoblockOptions {
|
|
36
|
+
/** A fetch to use (tests pass one); the global fetch by default. */
|
|
37
|
+
fetch?: typeof fetch;
|
|
38
|
+
timeoutMs?: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Ask Polymarket whether it would take orders from this machine's network
|
|
42
|
+
* location. Sends nothing but the request: no Skewtape key, no order.
|
|
43
|
+
*/
|
|
44
|
+
export declare function polymarketGeoblock(options?: GeoblockOptions): Promise<GeoblockResult>;
|
|
45
|
+
/** sk.geoblock: one helper per venue that publishes a check. */
|
|
46
|
+
export declare const geoblock: {
|
|
47
|
+
readonly polymarket: typeof polymarketGeoblock;
|
|
48
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sk.geoblock: whether a venue would accept orders from this machine's
|
|
3
|
+
* network location, as the venue itself says (ARCHITECTURE 13.3).
|
|
4
|
+
*
|
|
5
|
+
* const result = await geoblock.polymarket();
|
|
6
|
+
* if (result.status !== "allowed") {
|
|
7
|
+
* // do not route this user's orders to Polymarket
|
|
8
|
+
* }
|
|
9
|
+
*
|
|
10
|
+
* It asks the venue, not Skewtape, and answers "unknown" on any failure: a
|
|
11
|
+
* network error, a timeout, an unexpected status or body. It never answers
|
|
12
|
+
* "allowed" unless the venue said so. The developer is responsible for their
|
|
13
|
+
* users: this is one check for the machine it runs on, not a licence, and it
|
|
14
|
+
* says nothing about the venue's terms or the law where the user is.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* GET https://polymarket.com/api/geoblock answers {"blocked", "ip", "country",
|
|
18
|
+
* "region"} ("This endpoint is on polymarket.com, not the API servers",
|
|
19
|
+
* docs.polymarket.com/api-reference/geoblock.md, read 14 Nov 2026). The page
|
|
20
|
+
* lists blocked and close-only jurisdictions and does not say how close-only
|
|
21
|
+
* ones answer, so "allowed" means only that the venue answered blocked false.
|
|
22
|
+
*/
|
|
23
|
+
export const POLYMARKET_GEOBLOCK_URL = "https://polymarket.com/api/geoblock";
|
|
24
|
+
export const GEOBLOCK_DEFAULT_TIMEOUT_MS = 5000;
|
|
25
|
+
const text = (v) => (typeof v === "string" && v ? v : undefined);
|
|
26
|
+
function unknown(detail) {
|
|
27
|
+
return { status: "unknown", allowed: false, detail };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Ask Polymarket whether it would take orders from this machine's network
|
|
31
|
+
* location. Sends nothing but the request: no Skewtape key, no order.
|
|
32
|
+
*/
|
|
33
|
+
export async function polymarketGeoblock(options = {}) {
|
|
34
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
35
|
+
try {
|
|
36
|
+
// A redirect is not followed: the answer must be polymarket.com's own,
|
|
37
|
+
// as the Python SDK's httpx client follows none.
|
|
38
|
+
const response = await doFetch(POLYMARKET_GEOBLOCK_URL, {
|
|
39
|
+
method: "GET",
|
|
40
|
+
redirect: "manual",
|
|
41
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? GEOBLOCK_DEFAULT_TIMEOUT_MS),
|
|
42
|
+
});
|
|
43
|
+
if (response.type === "opaqueredirect")
|
|
44
|
+
return unknown("a redirect, not followed");
|
|
45
|
+
if (response.status !== 200)
|
|
46
|
+
return unknown(`HTTP ${response.status}`);
|
|
47
|
+
const body = await response.json();
|
|
48
|
+
if (typeof body !== "object" || body === null || Array.isArray(body) || typeof body.blocked !== "boolean") {
|
|
49
|
+
return unknown("the reply carries no boolean blocked field");
|
|
50
|
+
}
|
|
51
|
+
const b = body;
|
|
52
|
+
const result = {
|
|
53
|
+
status: b.blocked ? "blocked" : "allowed",
|
|
54
|
+
allowed: !b.blocked,
|
|
55
|
+
detail: b.blocked ? "the venue answered blocked" : "the venue answered not blocked",
|
|
56
|
+
};
|
|
57
|
+
const country = text(b.country);
|
|
58
|
+
const region = text(b.region);
|
|
59
|
+
const ip = text(b.ip);
|
|
60
|
+
if (country)
|
|
61
|
+
result.country = country;
|
|
62
|
+
if (region)
|
|
63
|
+
result.region = region;
|
|
64
|
+
if (ip)
|
|
65
|
+
result.ip = ip;
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
// Any failure is unknown, never allowed.
|
|
70
|
+
return unknown(err instanceof Error ? `${err.name}: ${err.message}` : String(err));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** sk.geoblock: one helper per venue that publishes a check. */
|
|
74
|
+
export const geoblock = { polymarket: polymarketGeoblock };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skewtape: prediction-market data across venues in one schema.
|
|
3
|
+
*
|
|
4
|
+
* import { Skewtape } from "skewtape";
|
|
5
|
+
* const sk = new Skewtape(); // the key from SKEWTAPE_API_KEY, or { apiKey }
|
|
6
|
+
* for await (const event of sk.events({ minConfidence: 0.9, resolutionEqual: true })) {
|
|
7
|
+
* console.log(event.event_id, event.question_canonical);
|
|
8
|
+
* }
|
|
9
|
+
*
|
|
10
|
+
* sk.kalshi() is the optional live Kalshi client under the user's own Kalshi
|
|
11
|
+
* membership (src/kalshi.ts), apart from the API: Skewtape's Kalshi data
|
|
12
|
+
* comes from the calls above.
|
|
13
|
+
*
|
|
14
|
+
* The hand-written facade lives in src/; src/generated/schema.d.ts is
|
|
15
|
+
* generated from the OpenAPI document and never edited.
|
|
16
|
+
*/
|
|
17
|
+
export { DEFAULT_BASE_URL, ENV_API_KEY, Paginated, Skewtape, activeMembers, type Bundle, type BundleDownloadOptions, type BundleFile, type Candle, type CandlesOptions, type DownloadURL, type EventsOptions, type Fee, type FeesOptions, type Fill, type FillsOptions, type Market, type MarketsOptions, type MatchedEvent, type MatchedEventMember, type Page, type Resolution, type ResolutionsOptions, type SkBacktestOptions, type SkewtapeOptions, type Venue, } from "./client.js";
|
|
18
|
+
export { BacktestError, backtestToJSON, runBacktest, type BacktestOptions, type BacktestResult, type BacktestWarning, type PositionResult, type Totals, type Trade, type TradeResult, } from "./backtest.js";
|
|
19
|
+
export { downloadFile, verifyChecksums, type DownloadOptions } from "./download.js";
|
|
20
|
+
export { Rat, fractionStr, roundHalfUp } from "./exact.js";
|
|
21
|
+
export { FeeError, feeRowOf, makerFeeRaw, microsToRaw, selectFeeRow, takerFeeRaw, type FeeRow, type FeeRowInput } from "./fees.js";
|
|
22
|
+
export { DEFAULT_KALSHI_BASE_URL, KALSHI_VENUES, Kalshi, KalshiError, PERIODS, kalshiTickers, seriesTickerOf, type KalshiCandlestick, type KalshiCandlesticksOptions, type KalshiEventLike, type KalshiLevel, type KalshiMarket, type KalshiMarketsOptions, type KalshiOHLC, type KalshiOptions, type KalshiOrderbook, type KalshiPeriod, type KalshiTrade, type KalshiTradesOptions, } from "./kalshi.js";
|
|
23
|
+
export { BookUnavailable, DEFAULT_BOOK_READERS, KALSHI_FRACTION_MICRO, quoteFromContext, readKalshiBook, readKalshiBookThroughDflow, readPolymarketBook, type BookReader, type BookRequest, type QuoteContext, type QuoteContextMember, type QuoteOptions, } from "./quote.js";
|
|
24
|
+
export { InvalidQuoteInputError, QuoteError, RuleMismatchError, VenueUnreachableError, computeQuote, type Book, type Leg, type Level, type QuoteInput, type QuoteResult, type Skip, type VenueQuote, } from "./quote-math.js";
|
|
25
|
+
export { AuthenticationError, BlockedError, ChecksumError, ConfigurationError, ForbiddenPlanError, InvalidRequestError, NotFoundError, QuotaExceededError, RateLimitError, ServerError, SkewtapeError, } from "./errors.js";
|
|
26
|
+
export { DFLOW_TRADING_API, DFlowOrderRequest, POLYMARKET_EXCHANGES, PolymarketOrder, RouteError, route, type DFlowRoute, type PolymarketRoute, type PolymarketTypedData, type RouteInput, } from "./route.js";
|
|
27
|
+
export { HYPERLIQUID_EXCHANGE_URL, HYPERLIQUID_TESTNET_EXCHANGE_URL, HyperliquidApproveBuilderFee, HyperliquidOrder, hyperliquidApproveBuilderFee, type HyperliquidRoute, } from "./route-hyperliquid.js";
|
|
28
|
+
export { geoblock, type GeoblockResult, type GeoblockOptions } from "./geoblock.js";
|
|
29
|
+
export { Stream, feedUrl, type FeedMessage, type FeedSocket, type FeedSocketConstructor, type StreamOptions } from "./stream.js";
|
|
30
|
+
export { VERSION } from "./version.js";
|
|
31
|
+
export type { components, paths } from "./generated/schema.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skewtape: prediction-market data across venues in one schema.
|
|
3
|
+
*
|
|
4
|
+
* import { Skewtape } from "skewtape";
|
|
5
|
+
* const sk = new Skewtape(); // the key from SKEWTAPE_API_KEY, or { apiKey }
|
|
6
|
+
* for await (const event of sk.events({ minConfidence: 0.9, resolutionEqual: true })) {
|
|
7
|
+
* console.log(event.event_id, event.question_canonical);
|
|
8
|
+
* }
|
|
9
|
+
*
|
|
10
|
+
* sk.kalshi() is the optional live Kalshi client under the user's own Kalshi
|
|
11
|
+
* membership (src/kalshi.ts), apart from the API: Skewtape's Kalshi data
|
|
12
|
+
* comes from the calls above.
|
|
13
|
+
*
|
|
14
|
+
* The hand-written facade lives in src/; src/generated/schema.d.ts is
|
|
15
|
+
* generated from the OpenAPI document and never edited.
|
|
16
|
+
*/
|
|
17
|
+
export { DEFAULT_BASE_URL, ENV_API_KEY, Paginated, Skewtape, activeMembers, } from "./client.js";
|
|
18
|
+
export { BacktestError, backtestToJSON, runBacktest, } from "./backtest.js";
|
|
19
|
+
export { downloadFile, verifyChecksums } from "./download.js";
|
|
20
|
+
export { Rat, fractionStr, roundHalfUp } from "./exact.js";
|
|
21
|
+
export { FeeError, feeRowOf, makerFeeRaw, microsToRaw, selectFeeRow, takerFeeRaw } from "./fees.js";
|
|
22
|
+
export { DEFAULT_KALSHI_BASE_URL, KALSHI_VENUES, Kalshi, KalshiError, PERIODS, kalshiTickers, seriesTickerOf, } from "./kalshi.js";
|
|
23
|
+
export { BookUnavailable, DEFAULT_BOOK_READERS, KALSHI_FRACTION_MICRO, quoteFromContext, readKalshiBook, readKalshiBookThroughDflow, readPolymarketBook, } from "./quote.js";
|
|
24
|
+
export { InvalidQuoteInputError, QuoteError, RuleMismatchError, VenueUnreachableError, computeQuote, } from "./quote-math.js";
|
|
25
|
+
export { AuthenticationError, BlockedError, ChecksumError, ConfigurationError, ForbiddenPlanError, InvalidRequestError, NotFoundError, QuotaExceededError, RateLimitError, ServerError, SkewtapeError, } from "./errors.js";
|
|
26
|
+
export { DFLOW_TRADING_API, DFlowOrderRequest, POLYMARKET_EXCHANGES, PolymarketOrder, RouteError, route, } from "./route.js";
|
|
27
|
+
export { HYPERLIQUID_EXCHANGE_URL, HYPERLIQUID_TESTNET_EXCHANGE_URL, HyperliquidApproveBuilderFee, HyperliquidOrder, hyperliquidApproveBuilderFee, } from "./route-hyperliquid.js";
|
|
28
|
+
export { geoblock } from "./geoblock.js";
|
|
29
|
+
export { Stream, feedUrl } from "./stream.js";
|
|
30
|
+
export { VERSION } from "./version.js";
|
|
@@ -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
|
+
}
|