tigeropen 0.3.0 → 0.5.5
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 +55 -2
- package/dist/cjs/client/api-response.js +28 -7
- package/dist/cjs/client/case-convert.js +4 -3
- package/dist/cjs/client/http-client.js +124 -8
- package/dist/cjs/client/token-methods.js +42 -0
- package/dist/cjs/config/client-config.js +115 -16
- package/dist/cjs/config/config-parser.js +19 -9
- package/dist/cjs/config/index.js +4 -1
- package/dist/cjs/config/token-manager.js +54 -4
- package/dist/cjs/index.js +53 -2
- package/dist/cjs/model/enums.js +106 -7
- package/dist/cjs/model/order-helpers.js +149 -0
- package/dist/cjs/model/order-status.js +46 -0
- package/dist/cjs/model/quote-requests.js +11 -0
- package/dist/cjs/model/trade-requests.js +13 -0
- package/dist/cjs/push/index.js +1 -0
- package/dist/cjs/push/pb/OrderStatusData.js +24 -0
- package/dist/cjs/push/proto-message.js +6 -0
- package/dist/cjs/push/push-client.js +117 -19
- package/dist/cjs/push/push-message.js +4 -0
- package/dist/cjs/push/tick-util.js +172 -0
- package/dist/cjs/quote/quote-client.js +584 -42
- package/dist/cjs/trade/trade-client.js +334 -32
- package/dist/cjs/version.js +4 -0
- package/dist/esm/client/api-request.js +1 -1
- package/dist/esm/client/api-response.js +29 -8
- package/dist/esm/client/case-convert.js +4 -3
- package/dist/esm/client/http-client.js +128 -12
- package/dist/esm/client/index.js +6 -6
- package/dist/esm/client/token-methods.js +37 -0
- package/dist/esm/config/client-config.js +118 -20
- package/dist/esm/config/config-parser.js +19 -9
- package/dist/esm/config/index.js +4 -3
- package/dist/esm/config/token-manager.js +55 -5
- package/dist/esm/index.js +15 -1
- package/dist/esm/logger/index.js +1 -1
- package/dist/esm/model/contract-helpers.js +1 -1
- package/dist/esm/model/enums.js +104 -6
- package/dist/esm/model/order-helpers.js +142 -1
- package/dist/esm/model/order-status.js +43 -0
- package/dist/esm/model/quote-requests.js +10 -0
- package/dist/esm/model/trade-requests.js +12 -0
- package/dist/esm/push/index.js +6 -5
- package/dist/esm/push/pb/OrderStatusData.js +24 -0
- package/dist/esm/push/pb/PushData.js +11 -11
- package/dist/esm/push/pb/QuoteBasicData.js +1 -1
- package/dist/esm/push/pb/Response.js +1 -1
- package/dist/esm/push/pb/index.js +17 -17
- package/dist/esm/push/proto-message.js +9 -3
- package/dist/esm/push/push-client.js +124 -26
- package/dist/esm/push/push-message.js +4 -0
- package/dist/esm/push/tick-util.js +165 -0
- package/dist/esm/quote/index.js +1 -1
- package/dist/esm/quote/quote-client.js +553 -44
- package/dist/esm/signer/index.js +2 -2
- package/dist/esm/trade/index.js +1 -1
- package/dist/esm/trade/trade-client.js +303 -34
- package/dist/esm/version.js +1 -0
- package/dist/types/client/api-response.d.ts +6 -4
- package/dist/types/client/case-convert.d.ts +3 -1
- package/dist/types/client/http-client.d.ts +32 -1
- package/dist/types/client/token-methods.d.ts +34 -0
- package/dist/types/config/client-config.d.ts +22 -1
- package/dist/types/config/index.d.ts +3 -1
- package/dist/types/config/token-manager.d.ts +27 -3
- package/dist/types/index.d.ts +22 -1
- package/dist/types/model/enums.d.ts +77 -7
- package/dist/types/model/order-helpers.d.ts +52 -1
- package/dist/types/model/order-status.d.ts +14 -0
- package/dist/types/model/order.d.ts +127 -10
- package/dist/types/model/quote-requests.d.ts +356 -0
- package/dist/types/model/quote.d.ts +391 -4
- package/dist/types/model/trade-requests.d.ts +309 -0
- package/dist/types/model/trade.d.ts +231 -5
- package/dist/types/push/callbacks.d.ts +3 -7
- package/dist/types/push/index.d.ts +1 -0
- package/dist/types/push/pb/OrderStatusData.d.ts +4 -0
- package/dist/types/push/push-client.d.ts +36 -3
- package/dist/types/push/push-message.d.ts +4 -0
- package/dist/types/push/tick-util.d.ts +30 -0
- package/dist/types/quote/quote-client.d.ts +193 -15
- package/dist/types/trade/trade-client.d.ts +136 -18
- package/dist/types/version.d.ts +1 -0
- package/package.json +5 -2
|
@@ -1,16 +1,119 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
36
|
exports.TradeClient = void 0;
|
|
37
|
+
/**
|
|
38
|
+
* TradeClient — trading client.
|
|
39
|
+
*
|
|
40
|
+
* All methods return strongly-typed responses; request parameters are
|
|
41
|
+
* written in camelCase in TypeScript and converted to snake_case on the wire.
|
|
42
|
+
*
|
|
43
|
+
* v0.4.0: 所有查询类方法改为 Request 对象签名(OrdersRequest 等),补齐 17
|
|
44
|
+
* 个新方法(账户管理 / 资产分析 / 资金调拨 / 持仓划转等)。
|
|
45
|
+
*/
|
|
46
|
+
const http_client_1 = require("../client/http-client");
|
|
4
47
|
const api_request_1 = require("../client/api-request");
|
|
5
48
|
const api_response_1 = require("../client/api-response");
|
|
49
|
+
const tokenMethods = __importStar(require("../client/token-methods"));
|
|
50
|
+
const order_status_1 = require("../model/order-status");
|
|
51
|
+
/** Normalize `Order.status` field in-place (handles int → string). */
|
|
52
|
+
function normalizeOrderInPlace(o) {
|
|
53
|
+
if (o && o.status !== undefined && o.status !== null) {
|
|
54
|
+
o.status = (0, order_status_1.normalizeOrderStatus)(o.status);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Normalize every Order in a list in-place. */
|
|
58
|
+
function normalizeOrders(orders) {
|
|
59
|
+
for (const o of orders)
|
|
60
|
+
normalizeOrderInPlace(o);
|
|
61
|
+
return orders;
|
|
62
|
+
}
|
|
63
|
+
/** Shallow-copy a request object and fill account if empty. */
|
|
64
|
+
function withAccount(req, account) {
|
|
65
|
+
const r = { ...(req ?? {}) };
|
|
66
|
+
if (!r.account)
|
|
67
|
+
r.account = account;
|
|
68
|
+
return r;
|
|
69
|
+
}
|
|
6
70
|
/** Trading client wrapping all trade-related APIs. */
|
|
7
71
|
class TradeClient {
|
|
8
|
-
constructor(httpClient, account) {
|
|
72
|
+
constructor(httpClient, account, secretKey) {
|
|
9
73
|
this.httpClient = httpClient;
|
|
10
74
|
this.account = account;
|
|
75
|
+
this.secretKey = secretKey;
|
|
76
|
+
}
|
|
77
|
+
/** Create a TradeClient directly from a ClientConfig — no need to construct HttpClient manually. */
|
|
78
|
+
static fromConfig(config, account, secretKey) {
|
|
79
|
+
return new TradeClient(new http_client_1.HttpClient(config), account, secretKey);
|
|
80
|
+
}
|
|
81
|
+
// ── Token management ──────────────────────────────────────────────────────
|
|
82
|
+
/** Call the `user_token_refresh` API and return the new token. Read-only. */
|
|
83
|
+
async queryToken() {
|
|
84
|
+
return tokenMethods.queryToken(this.httpClient);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Refresh the token: call the API, update the in-memory config token, and
|
|
88
|
+
* optionally persist via the provided TokenManager (file + writer callback).
|
|
89
|
+
*
|
|
90
|
+
* @param tokenManager - optional TokenManager for file persistence and writer callback
|
|
91
|
+
*/
|
|
92
|
+
async refreshToken(tokenManager) {
|
|
93
|
+
return tokenMethods.refreshToken(this.httpClient, tokenManager);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Start background token auto-refresh. Returns a TokenManager that can be
|
|
97
|
+
* used to stop the background task via `tm.stopAutoRefresh()`.
|
|
98
|
+
*
|
|
99
|
+
* @param refreshDurationSecs - Refresh threshold in seconds (minimum 30).
|
|
100
|
+
* @param checkIntervalMs - How often to check, in milliseconds (default 300 000 / 5 min).
|
|
101
|
+
* @param tokenWriter - Optional callback invoked after each successful refresh.
|
|
102
|
+
*/
|
|
103
|
+
startTokenAutoRefresh(refreshDurationSecs, checkIntervalMs, tokenWriter) {
|
|
104
|
+
return tokenMethods.startTokenAutoRefresh(this.httpClient, refreshDurationSecs, checkIntervalMs, tokenWriter);
|
|
11
105
|
}
|
|
12
106
|
async callInto(method, bizParams) {
|
|
13
|
-
|
|
107
|
+
// Inject secretKey into every request when available (institution accounts require it).
|
|
108
|
+
// Individual methods that already set secretKey will not be overwritten (|| not &&).
|
|
109
|
+
let params = bizParams;
|
|
110
|
+
if (this.secretKey && params !== null && typeof params === 'object' && !Array.isArray(params)) {
|
|
111
|
+
const p = params;
|
|
112
|
+
if (p['secretKey'] == null && p['secret_key'] == null) {
|
|
113
|
+
params = { ...p, secretKey: this.secretKey };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const request = (0, api_request_1.createApiRequest)(method, params);
|
|
14
117
|
const response = await this.httpClient.executeRequest(request);
|
|
15
118
|
return (0, api_response_1.unmarshalData)(response.data);
|
|
16
119
|
}
|
|
@@ -42,52 +145,251 @@ class TradeClient {
|
|
|
42
145
|
});
|
|
43
146
|
}
|
|
44
147
|
// === Order operations ===
|
|
148
|
+
/**
|
|
149
|
+
* Prepare an order request for the wire.
|
|
150
|
+
*
|
|
151
|
+
* The gateway expects `algo_params` as a `[{tag, value}, ...]` array
|
|
152
|
+
* (matching the Python SDK's AlgoParams.to_dict serialization), not the
|
|
153
|
+
* plain object shape that reads naturally in TypeScript. This helper
|
|
154
|
+
* normalizes the object form into the array form on outbound payloads
|
|
155
|
+
* so callers can pass the natural object and TWAP/VWAP orders work end
|
|
156
|
+
* to end without wire-format workarounds.
|
|
157
|
+
*
|
|
158
|
+
* Fields with undefined/null values are dropped; empty algoParams
|
|
159
|
+
* becomes an empty array rather than being stripped, matching the
|
|
160
|
+
* Python SDK behavior when the user passes an empty AlgoParams().
|
|
161
|
+
*/
|
|
162
|
+
marshalOrderForWire(order) {
|
|
163
|
+
const { algoParams, ...rest } = order;
|
|
164
|
+
const withAccount = { ...rest, account: this.account };
|
|
165
|
+
if (algoParams === undefined)
|
|
166
|
+
return withAccount;
|
|
167
|
+
// Already a tag/value array (advanced callers who want raw control)
|
|
168
|
+
if (Array.isArray(algoParams)) {
|
|
169
|
+
withAccount.algoParams = algoParams;
|
|
170
|
+
return withAccount;
|
|
171
|
+
}
|
|
172
|
+
// Object shape → tag/value array.
|
|
173
|
+
// Keys are converted to snake_case explicitly here (e.g. `startTime` →
|
|
174
|
+
// `start_time`) so they land correctly in the `tag` string that the
|
|
175
|
+
// gateway reads. `keysToSnakeCase` only rewrites object *keys*, not
|
|
176
|
+
// string *values*, so `tag` would be left camelCase if we didn't do
|
|
177
|
+
// this conversion ourselves.
|
|
178
|
+
const tagValues = [];
|
|
179
|
+
for (const [key, value] of Object.entries(algoParams)) {
|
|
180
|
+
if (value === undefined || value === null)
|
|
181
|
+
continue;
|
|
182
|
+
const snakeKey = key.replace(/[A-Z]/g, (c) => '_' + c.toLowerCase());
|
|
183
|
+
tagValues.push({ tag: snakeKey, value });
|
|
184
|
+
}
|
|
185
|
+
withAccount.algoParams = tagValues;
|
|
186
|
+
return withAccount;
|
|
187
|
+
}
|
|
45
188
|
async placeOrder(order) {
|
|
46
|
-
return this.callInto('place_order',
|
|
189
|
+
return this.callInto('place_order', this.marshalOrderForWire(order));
|
|
47
190
|
}
|
|
48
191
|
async previewOrder(order) {
|
|
49
|
-
return this.callInto('preview_order',
|
|
192
|
+
return this.callInto('preview_order', this.marshalOrderForWire(order));
|
|
50
193
|
}
|
|
51
194
|
async modifyOrder(id, order) {
|
|
52
|
-
return this.callInto('modify_order', { ...
|
|
195
|
+
return this.callInto('modify_order', { ...this.marshalOrderForWire(order), id });
|
|
53
196
|
}
|
|
54
|
-
async cancelOrder(id) {
|
|
55
|
-
|
|
197
|
+
async cancelOrder(id, secretKey) {
|
|
198
|
+
const params = { account: this.account, id };
|
|
199
|
+
if (secretKey !== undefined)
|
|
200
|
+
params.secretKey = secretKey; // per-call override takes precedence; undefined means use config default
|
|
201
|
+
return this.callInto('cancel_order', params);
|
|
56
202
|
}
|
|
57
|
-
// === Order queries ===
|
|
58
|
-
|
|
59
|
-
|
|
203
|
+
// === Order queries (BREAKING v0.4.0) ===
|
|
204
|
+
/**
|
|
205
|
+
* Query all historical orders. v0.4.0: optional OrdersRequest for filtering.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* // Default (unfiltered)
|
|
209
|
+
* await tc.getOrders();
|
|
210
|
+
* // Filter by status
|
|
211
|
+
* await tc.getOrders({ states: ['Filled'], limit: 100 });
|
|
212
|
+
*/
|
|
213
|
+
async getOrders(req) {
|
|
214
|
+
return normalizeOrders(await this.callIntoItems('orders', withAccount(req, this.account)));
|
|
60
215
|
}
|
|
61
|
-
|
|
62
|
-
|
|
216
|
+
/** Query active (open) orders. v0.4.0: supports ParentId to filter attached legs. */
|
|
217
|
+
async getActiveOrders(req) {
|
|
218
|
+
return normalizeOrders(await this.callIntoItems('active_orders', withAccount(req, this.account)));
|
|
63
219
|
}
|
|
64
|
-
|
|
65
|
-
|
|
220
|
+
/** Query inactive/cancelled orders. */
|
|
221
|
+
async getInactiveOrders(req) {
|
|
222
|
+
return normalizeOrders(await this.callIntoItems('inactive_orders', withAccount(req, this.account)));
|
|
66
223
|
}
|
|
67
224
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
225
|
+
* Query filled orders. v0.4.0 BREAKING: signature changed from positional
|
|
226
|
+
* (startDateMs, endDateMs) to OrdersRequest object.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* // Last 30 days
|
|
230
|
+
* const now = Date.now();
|
|
231
|
+
* await tc.getFilledOrders({ startDate: now - 30*24*3600*1000, endDate: now });
|
|
70
232
|
*/
|
|
71
|
-
async getFilledOrders(
|
|
72
|
-
return this.callIntoItems('filled_orders',
|
|
73
|
-
account: this.account, startDate: startDateMs, endDate: endDateMs,
|
|
74
|
-
});
|
|
233
|
+
async getFilledOrders(req) {
|
|
234
|
+
return normalizeOrders(await this.callIntoItems('filled_orders', withAccount(req, this.account)));
|
|
75
235
|
}
|
|
76
|
-
/**
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
236
|
+
/**
|
|
237
|
+
* Get a single order by ID (v0.4.0 new).
|
|
238
|
+
* Returns `undefined` if no match.
|
|
239
|
+
*/
|
|
240
|
+
async getOrder(req) {
|
|
241
|
+
const merged = withAccount(req, this.account);
|
|
242
|
+
const out = await this.callInto('order_no', merged);
|
|
243
|
+
if (!out || (!out.id && !out.orderId))
|
|
244
|
+
return undefined;
|
|
245
|
+
normalizeOrderInPlace(out);
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Query order transaction details. v0.4.0 BREAKING: all fields now optional
|
|
250
|
+
* (previously symbol/secType required).
|
|
251
|
+
*/
|
|
252
|
+
async getOrderTransactions(req) {
|
|
253
|
+
return this.callIntoItems('order_transactions', withAccount(req, this.account));
|
|
254
|
+
}
|
|
255
|
+
// === Positions & assets (BREAKING v0.4.0) ===
|
|
256
|
+
/** Query positions. v0.4.0: supports Symbol / SecType / Currency / Market filters. */
|
|
257
|
+
async getPositions(req) {
|
|
258
|
+
return this.callIntoItems('positions', withAccount(req, this.account));
|
|
259
|
+
}
|
|
260
|
+
/** Query assets. v0.4.0: supports sub-account list and aggregation options. */
|
|
261
|
+
async getAssets(req) {
|
|
262
|
+
return this.callIntoItems('assets', withAccount(req, this.account));
|
|
263
|
+
}
|
|
264
|
+
/** Query prime assets. v0.4.0: takes AssetsRequest for filters. */
|
|
265
|
+
async getPrimeAssets(req) {
|
|
266
|
+
return this.callInto('prime_assets', withAccount(req, this.account));
|
|
267
|
+
}
|
|
268
|
+
// ===== v0.4.0 NEW: Account management =====
|
|
269
|
+
/** Query managed sub-accounts (institutional). */
|
|
270
|
+
async getManagedAccounts(req) {
|
|
271
|
+
return this.callIntoItems('accounts', withAccount(req, this.account));
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Query derivative contracts list. Same wire as getQuoteContract but accepts
|
|
275
|
+
* full request struct. Python: `get_derivative_contracts`.
|
|
276
|
+
*/
|
|
277
|
+
async getDerivativeContracts(req) {
|
|
278
|
+
return this.callIntoItems('quote_contract', withAccount(req, this.account));
|
|
279
|
+
}
|
|
280
|
+
// ===== v0.4.0 NEW: Asset analytics =====
|
|
281
|
+
/** Daily P&L / net-value / holding-value analytics. */
|
|
282
|
+
async getAnalyticsAsset(req) {
|
|
283
|
+
return this.callIntoItems('analytics_asset', withAccount(req, this.account));
|
|
284
|
+
}
|
|
285
|
+
/** Aggregate assets (prime) by base currency. */
|
|
286
|
+
async getAggregateAssets(req) {
|
|
287
|
+
return this.callInto('aggregate_assets', withAccount(req, this.account));
|
|
288
|
+
}
|
|
289
|
+
/** Estimate tradable quantity given order parameters. */
|
|
290
|
+
async getEstimateTradableQuantity(req) {
|
|
291
|
+
return this.callInto('estimate_tradable_quantity', withAccount(req, this.account));
|
|
292
|
+
}
|
|
293
|
+
// ===== v0.4.0 NEW: Forex =====
|
|
294
|
+
/** Place forex order (sub-account base-currency swap). */
|
|
295
|
+
async placeForexOrder(req) {
|
|
296
|
+
return this.callInto('place_forex_order', withAccount(req, this.account));
|
|
297
|
+
}
|
|
298
|
+
// ===== v0.4.0 NEW: Segment fund transfer =====
|
|
299
|
+
/** Query amount available for inter-segment transfer. Returns array. */
|
|
300
|
+
async getSegmentFundAvailable(req) {
|
|
301
|
+
return this.callInto('segment_fund_available', withAccount(req, this.account));
|
|
302
|
+
}
|
|
303
|
+
/** Query segment fund transfer history. Server returns plain array (no items wrapper). */
|
|
304
|
+
async getSegmentFundHistory(req) {
|
|
305
|
+
return this.callInto('segment_fund_history', withAccount(req, this.account));
|
|
306
|
+
}
|
|
307
|
+
/** Execute an inter-segment fund transfer. */
|
|
308
|
+
async transferSegmentFund(req) {
|
|
309
|
+
return this.callInto('transfer_segment_fund', withAccount(req, this.account));
|
|
310
|
+
}
|
|
311
|
+
/** Cancel a pending inter-segment transfer. */
|
|
312
|
+
async cancelSegmentFund(req) {
|
|
313
|
+
return this.callInto('cancel_segment_fund', withAccount(req, this.account));
|
|
314
|
+
}
|
|
315
|
+
// ===== v0.4.0 NEW: Fund details / history =====
|
|
316
|
+
/** Fund-movement details (by segment / currency / fund-type). */
|
|
317
|
+
async getFundDetails(req) {
|
|
318
|
+
return this.callIntoItems('fund_details', withAccount(req, this.account));
|
|
319
|
+
}
|
|
320
|
+
/** Funding history (wire method: `transfer_fund`).
|
|
321
|
+
* Server returns bare list (no items wrapper). */
|
|
322
|
+
async getFundingHistory(req) {
|
|
323
|
+
return this.callInto('transfer_fund', withAccount(req, this.account));
|
|
324
|
+
}
|
|
325
|
+
// ===== v0.4.0 NEW: Internal / external position transfer =====
|
|
326
|
+
/**
|
|
327
|
+
* Internal position transfer (cross sub-account).
|
|
328
|
+
* @note Uses `fromAccount` / `toAccount` (not `account`).
|
|
329
|
+
*/
|
|
330
|
+
async transferPosition(req) {
|
|
331
|
+
const merged = { ...req };
|
|
332
|
+
if (!merged.fromAccount)
|
|
333
|
+
merged.fromAccount = this.account;
|
|
334
|
+
return this.callInto('position_transfer', merged);
|
|
335
|
+
}
|
|
336
|
+
/** Query internal position transfer records. Uses `accountId` (wire: `account_id`). */
|
|
337
|
+
async getPositionTransferRecords(req) {
|
|
338
|
+
const merged = { ...req };
|
|
339
|
+
if (!merged.accountId)
|
|
340
|
+
merged.accountId = this.account;
|
|
341
|
+
return this.callIntoItems('position_transfer_records', merged);
|
|
342
|
+
}
|
|
343
|
+
/** Query single internal position transfer detail by ID. */
|
|
344
|
+
async getPositionTransferDetail(req) {
|
|
345
|
+
const merged = { ...req };
|
|
346
|
+
if (!merged.accountId)
|
|
347
|
+
merged.accountId = this.account;
|
|
348
|
+
return this.callInto('position_transfer_detail', merged);
|
|
349
|
+
}
|
|
350
|
+
/** Query external position transfer records (DWAC / FOP / etc.). */
|
|
351
|
+
async getPositionTransferExternalRecords(req) {
|
|
352
|
+
const merged = { ...req };
|
|
353
|
+
if (!merged.accountId)
|
|
354
|
+
merged.accountId = this.account;
|
|
355
|
+
return this.callIntoItems('position_transfer_external_records', merged);
|
|
356
|
+
}
|
|
357
|
+
/** 行权检验:预估行权/作废后正股持仓变化 (wire: option_exercise_check) */
|
|
358
|
+
async checkOptionExercise(req) {
|
|
359
|
+
const merged = { ...req };
|
|
360
|
+
if (!merged.account)
|
|
361
|
+
merged.account = this.account;
|
|
362
|
+
return this.callInto('option_exercise_check', merged);
|
|
363
|
+
}
|
|
364
|
+
/** 查询可行权持仓列表 (wire: option_exercise_position) */
|
|
365
|
+
async getOptionExercisePositions(req) {
|
|
366
|
+
const merged = { ...req };
|
|
367
|
+
if (!merged.account)
|
|
368
|
+
merged.account = this.account;
|
|
369
|
+
return this.callInto('option_exercise_position', merged);
|
|
81
370
|
}
|
|
82
|
-
|
|
83
|
-
async
|
|
84
|
-
|
|
371
|
+
/** 提交行权/作废申请 (wire: option_exercise_submit) */
|
|
372
|
+
async submitOptionExercise(req) {
|
|
373
|
+
const merged = { ...req };
|
|
374
|
+
if (!merged.account)
|
|
375
|
+
merged.account = this.account;
|
|
376
|
+
const result = await this.callInto('option_exercise_submit', merged);
|
|
377
|
+
return result ?? false;
|
|
85
378
|
}
|
|
86
|
-
|
|
87
|
-
|
|
379
|
+
/** 分页查询行权记录 (wire: option_exercise_record) */
|
|
380
|
+
async getOptionExerciseRecords(req) {
|
|
381
|
+
const merged = { ...req };
|
|
382
|
+
if (!merged.account)
|
|
383
|
+
merged.account = this.account;
|
|
384
|
+
return this.callInto('option_exercise_record', merged);
|
|
88
385
|
}
|
|
89
|
-
|
|
90
|
-
|
|
386
|
+
/** 撤销行权申请 (wire: option_exercise_cancel) */
|
|
387
|
+
async cancelOptionExercise(req) {
|
|
388
|
+
const merged = { ...req };
|
|
389
|
+
if (!merged.account)
|
|
390
|
+
merged.account = this.account;
|
|
391
|
+
const result = await this.callInto('option_exercise_cancel', merged);
|
|
392
|
+
return result ?? false;
|
|
91
393
|
}
|
|
92
394
|
}
|
|
93
395
|
exports.TradeClient = TradeClient;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Requests use camelCase parameters in TypeScript (idiomatic), but the
|
|
5
5
|
* server wire format is snake_case, so we convert keys before serializing.
|
|
6
6
|
*/
|
|
7
|
-
import { keysToSnakeCase } from './case-convert';
|
|
7
|
+
import { keysToSnakeCase } from './case-convert.js';
|
|
8
8
|
/**
|
|
9
9
|
* Create an API request, serializing business parameters to JSON as bizContent.
|
|
10
10
|
* camelCase keys are automatically converted to snake_case to match the server.
|
|
@@ -1,13 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* API response parsing.
|
|
3
3
|
*/
|
|
4
|
-
import { TigerError } from './errors';
|
|
4
|
+
import { TigerError } from './errors.js';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Replaces bare int64 values (≥17 digits, not part of a float) with quoted
|
|
7
|
+
* strings so they survive JSON.parse without precision loss.
|
|
8
|
+
* 17+ digits are guaranteed to exceed Number.MAX_SAFE_INTEGER (≈9.007e15),
|
|
9
|
+
* so 16-digit microsecond timestamps remain as numbers.
|
|
10
|
+
*/
|
|
11
|
+
function patchLargeIntegers(text) {
|
|
12
|
+
const strings = [];
|
|
13
|
+
const masked = text.replace(/"(?:[^"\\]|\\.)*"/g, (match) => {
|
|
14
|
+
strings.push(match);
|
|
15
|
+
return '\x01' + (strings.length - 1) + '\x01';
|
|
16
|
+
});
|
|
17
|
+
// Match bare integers preceded by a JSON structural character or line start,
|
|
18
|
+
// not followed by decimal point or exponent (avoids matching float components).
|
|
19
|
+
// The `m` flag makes `^` match line-start for multi-line JSON.
|
|
20
|
+
const patched = masked.replace(/([:,\[{]|^)\s*(-?\d{17,})(?![.\deE])/gm, (full, prefix, num) => {
|
|
21
|
+
return prefix + '"' + num + '"';
|
|
22
|
+
});
|
|
23
|
+
return patched.replace(/\x01(\d+)\x01/g, (_, i) => strings[parseInt(i, 10)]);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parse the raw HTTP response body. Throws TigerError when code != 0.
|
|
27
|
+
*
|
|
28
|
+
* patchLargeIntegers is applied before JSON.parse to preserve int64 order IDs
|
|
29
|
+
* (e.g. 28868646234578944) as strings rather than losing precision.
|
|
8
30
|
*/
|
|
9
31
|
export function parseApiResponse(body) {
|
|
10
|
-
const resp = JSON.parse(body);
|
|
32
|
+
const resp = JSON.parse(patchLargeIntegers(body));
|
|
11
33
|
if (resp.code !== 0) {
|
|
12
34
|
throw new TigerError(resp.code, resp.message);
|
|
13
35
|
}
|
|
@@ -15,16 +37,15 @@ export function parseApiResponse(body) {
|
|
|
15
37
|
}
|
|
16
38
|
/**
|
|
17
39
|
* Decode an ApiResponse.data payload into a typed value.
|
|
18
|
-
* Handles
|
|
19
|
-
*
|
|
40
|
+
* Handles double-encoded JSON (where `data` is a JSON string wrapping JSON),
|
|
41
|
+
* seen on some trade endpoints.
|
|
20
42
|
*/
|
|
21
43
|
export function unmarshalData(data) {
|
|
22
44
|
if (data == null)
|
|
23
45
|
return undefined;
|
|
24
46
|
if (typeof data === 'string') {
|
|
25
|
-
// Attempt to parse JSON string (double-encoded case)
|
|
26
47
|
try {
|
|
27
|
-
return JSON.parse(data);
|
|
48
|
+
return JSON.parse(patchLargeIntegers(data));
|
|
28
49
|
}
|
|
29
50
|
catch {
|
|
30
51
|
return data;
|
|
@@ -13,7 +13,9 @@ export function camelToSnake(key) {
|
|
|
13
13
|
/**
|
|
14
14
|
* Recursively convert all object keys from camelCase to snake_case.
|
|
15
15
|
* Preserves arrays, primitives, and Date/null/undefined.
|
|
16
|
-
*
|
|
16
|
+
*
|
|
17
|
+
* Note: camelToSnake is idempotent for pure snake_case keys (no uppercase
|
|
18
|
+
* letters → no replacements), so no pre-check is needed.
|
|
17
19
|
*/
|
|
18
20
|
export function keysToSnakeCase(value) {
|
|
19
21
|
if (Array.isArray(value)) {
|
|
@@ -22,8 +24,7 @@ export function keysToSnakeCase(value) {
|
|
|
22
24
|
if (value && typeof value === 'object' && value.constructor === Object) {
|
|
23
25
|
const out = {};
|
|
24
26
|
for (const [k, v] of Object.entries(value)) {
|
|
25
|
-
|
|
26
|
-
out[newKey] = keysToSnakeCase(v);
|
|
27
|
+
out[camelToSnake(k)] = keysToSnakeCase(v);
|
|
27
28
|
}
|
|
28
29
|
return out;
|
|
29
30
|
}
|