pmxt-core 1.3.3 → 1.4.0

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/API_REFERENCE.md CHANGED
@@ -129,6 +129,29 @@ const trades = await kalshi.fetchTrades('FED-25JAN', {
129
129
 
130
130
  ---
131
131
 
132
+ ## Helper Methods
133
+
134
+ ### `getExecutionPrice(orderBook, side, amount)`
135
+ Calculate the volume-weighted average price for a given amount. Returns `0` if there is insufficient liquidity to fully fill the amount.
136
+
137
+ ```typescript
138
+ const orderBook = await polymarket.fetchOrderBook(outcomeId);
139
+ const price = await polymarket.getExecutionPrice(orderBook, 'buy', 100);
140
+ console.log(`Average price for 100 shares: ${price}`);
141
+ ```
142
+
143
+ ### `getExecutionPriceDetailed(orderBook, side, amount)`
144
+ Calculate detailed execution price information, including partial fills.
145
+
146
+ ```typescript
147
+ const detailed = await polymarket.getExecutionPriceDetailed(orderBook, 'buy', 100);
148
+ console.log(`Average Price: ${detailed.price}`);
149
+ console.log(`Filled Amount: ${detailed.filledAmount}`);
150
+ console.log(`Fully Filled: ${detailed.fullyFilled}`);
151
+ ```
152
+
153
+ ---
154
+
132
155
  ## Data Models
133
156
 
134
157
  ### `UnifiedMarket`
@@ -196,6 +219,12 @@ interface Trade {
196
219
  amount: number;
197
220
  side: 'buy' | 'sell' | 'unknown';
198
221
  }
222
+
223
+ interface ExecutionPriceResult {
224
+ price: number; // Average execution price
225
+ filledAmount: number; // Amount filled (shares)
226
+ fullyFilled: boolean; // If the requested amount was met
227
+ }
199
228
  ```
200
229
 
201
230
  ---
@@ -1,4 +1,5 @@
1
1
  import { UnifiedMarket, UnifiedEvent, PriceCandle, CandleInterval, OrderBook, Trade, Order, Position, Balance, CreateOrderParams } from './types';
2
+ import { ExecutionPriceResult } from './utils/math';
2
3
  export interface MarketFilterParams {
3
4
  limit?: number;
4
5
  offset?: number;
@@ -32,6 +33,11 @@ export declare abstract class PredictionMarketExchange {
32
33
  * By default, searches only in market titles. Use params.searchIn to search descriptions or both.
33
34
  */
34
35
  abstract searchMarkets(query: string, params?: MarketFilterParams): Promise<UnifiedMarket[]>;
36
+ /**
37
+ * Fetch markets by URL slug (Polymarket) or ticker (Kalshi).
38
+ * @param slug - Market slug or ticker
39
+ */
40
+ abstract getMarketsBySlug(slug: string): Promise<UnifiedMarket[]>;
35
41
  /**
36
42
  * Search for events matching a keyword query.
37
43
  * Returns grouped events, each containing related markets.
@@ -78,6 +84,8 @@ export declare abstract class PredictionMarketExchange {
78
84
  * Fetch account balances.
79
85
  */
80
86
  fetchBalance(): Promise<Balance[]>;
87
+ getExecutionPrice(orderBook: OrderBook, side: 'buy' | 'sell', amount: number): number;
88
+ getExecutionPriceDetailed(orderBook: OrderBook, side: 'buy' | 'sell', amount: number): ExecutionPriceResult;
81
89
  /**
82
90
  * Watch orderbook updates in real-time via WebSocket.
83
91
  * Returns a promise that resolves with the latest orderbook state.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PredictionMarketExchange = void 0;
4
+ const math_1 = require("./utils/math");
4
5
  // ----------------------------------------------------------------------------
5
6
  // Base Exchange Class
6
7
  // ----------------------------------------------------------------------------
@@ -77,6 +78,12 @@ class PredictionMarketExchange {
77
78
  async fetchBalance() {
78
79
  throw new Error("Method fetchBalance not implemented.");
79
80
  }
81
+ getExecutionPrice(orderBook, side, amount) {
82
+ return (0, math_1.getExecutionPrice)(orderBook, side, amount);
83
+ }
84
+ getExecutionPriceDetailed(orderBook, side, amount) {
85
+ return (0, math_1.getExecutionPriceDetailed)(orderBook, side, amount);
86
+ }
80
87
  // ----------------------------------------------------------------------------
81
88
  // WebSocket Streaming Methods
82
89
  // ----------------------------------------------------------------------------
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './BaseExchange';
2
2
  export * from './types';
3
+ export * from './utils/math';
3
4
  export * from './exchanges/polymarket';
4
5
  export * from './exchanges/kalshi';
5
6
  export * from './server/app';
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.kalshi = exports.polymarket = void 0;
18
18
  __exportStar(require("./BaseExchange"), exports);
19
19
  __exportStar(require("./types"), exports);
20
+ __exportStar(require("./utils/math"), exports);
20
21
  __exportStar(require("./exchanges/polymarket"), exports);
21
22
  __exportStar(require("./exchanges/kalshi"), exports);
22
23
  __exportStar(require("./server/app"), exports);
@@ -0,0 +1,8 @@
1
+ import { OrderBook } from '../types';
2
+ export declare function getExecutionPrice(orderBook: OrderBook, side: 'buy' | 'sell', amount: number): number;
3
+ export interface ExecutionPriceResult {
4
+ price: number;
5
+ filledAmount: number;
6
+ fullyFilled: boolean;
7
+ }
8
+ export declare function getExecutionPriceDetailed(orderBook: OrderBook, side: 'buy' | 'sell', amount: number): ExecutionPriceResult;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getExecutionPrice = getExecutionPrice;
4
+ exports.getExecutionPriceDetailed = getExecutionPriceDetailed;
5
+ function getExecutionPrice(orderBook, side, amount) {
6
+ const result = getExecutionPriceDetailed(orderBook, side, amount);
7
+ return result.fullyFilled ? result.price : 0;
8
+ }
9
+ function getExecutionPriceDetailed(orderBook, side, amount) {
10
+ if (amount <= 0) {
11
+ throw new Error('Amount must be greater than 0');
12
+ }
13
+ // Defensive copy to avoid mutating the original object if we were to sort in place (though toSorted/spread is safer)
14
+ // We filter out any zero-size levels just in case
15
+ let levels = (side === 'buy' ? orderBook.asks : orderBook.bids).filter(l => l.size > 0);
16
+ // Sort levels to ensure we get the best price
17
+ // Asks: Lowest Price First (Ascending)
18
+ // Bids: Highest Price First (Descending)
19
+ levels.sort((a, b) => side === 'buy' ? a.price - b.price : b.price - a.price);
20
+ if (levels.length === 0) {
21
+ return {
22
+ price: 0,
23
+ filledAmount: 0,
24
+ fullyFilled: false,
25
+ };
26
+ }
27
+ let remainingAmount = amount;
28
+ let totalCost = 0;
29
+ let filledAmount = 0;
30
+ // Use a small epsilon to handle floating point arithmetic errors
31
+ const EPSILON = 0.00000001;
32
+ for (const level of levels) {
33
+ if (remainingAmount <= EPSILON) {
34
+ break;
35
+ }
36
+ const fillSize = Math.min(remainingAmount, level.size);
37
+ totalCost += fillSize * level.price;
38
+ filledAmount += fillSize;
39
+ remainingAmount -= fillSize;
40
+ }
41
+ const fullyFilled = remainingAmount <= EPSILON;
42
+ const executionPrice = filledAmount > EPSILON ? totalCost / filledAmount : 0;
43
+ return {
44
+ price: executionPrice,
45
+ filledAmount,
46
+ fullyFilled,
47
+ };
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmxt-core",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "description": "pmxt is a unified prediction market data API. The ccxt for prediction markets.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -29,8 +29,8 @@
29
29
  "test": "jest -c jest.config.js",
30
30
  "server": "tsx watch src/server/index.ts",
31
31
  "server:prod": "node dist/server/index.js",
32
- "generate:sdk:python": "npx @openapitools/openapi-generator-cli generate -i src/server/openapi.yaml -g python -o ../sdks/python/generated --package-name pmxt_internal --additional-properties=projectName=pmxt-internal,packageVersion=1.3.3,library=urllib3",
33
- "generate:sdk:typescript": "npx @openapitools/openapi-generator-cli generate -i src/server/openapi.yaml -g typescript-fetch -o ../sdks/typescript/generated --additional-properties=npmName=pmxtjs,npmVersion=1.3.3,supportsES6=true,typescriptThreePlus=true",
32
+ "generate:sdk:python": "npx @openapitools/openapi-generator-cli generate -i src/server/openapi.yaml -g python -o ../sdks/python/generated --package-name pmxt_internal --additional-properties=projectName=pmxt-internal,packageVersion=1.4.0,library=urllib3",
33
+ "generate:sdk:typescript": "npx @openapitools/openapi-generator-cli generate -i src/server/openapi.yaml -g typescript-fetch -o ../sdks/typescript/generated --additional-properties=npmName=pmxtjs,npmVersion=1.4.0,supportsES6=true,typescriptThreePlus=true",
34
34
  "generate:docs": "node ../scripts/generate-api-docs.js",
35
35
  "generate:sdk:all": "npm run generate:sdk:python && npm run generate:sdk:typescript && npm run generate:docs"
36
36
  },