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
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
* heartbeat keep-alive, and automatic reconnection.
|
|
10
10
|
*/
|
|
11
11
|
import * as tls from 'tls';
|
|
12
|
-
import { signWithRSA } from '../signer/signer';
|
|
13
|
-
import { SubjectType } from './push-message';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
12
|
+
import { signWithRSA } from '../signer/signer.js';
|
|
13
|
+
import { SubjectType } from './push-message.js';
|
|
14
|
+
import { convertTradeTick } from './tick-util.js';
|
|
15
|
+
import { Request } from './pb/Request.js';
|
|
16
|
+
import { Response } from './pb/Response.js';
|
|
17
|
+
import { SocketCommon_Command, SocketCommon_DataType } from './pb/SocketCommon.js';
|
|
18
|
+
import { encodeVarint32, decodeVarint32 } from './varint.js';
|
|
19
|
+
import { buildConnectMessage, buildHeartBeatMessage, buildSubscribeMessage, buildUnSubscribeMessage, buildDisconnectMessage, subjectToDataType, } from './proto-message.js';
|
|
19
20
|
/** Default push server host */
|
|
20
21
|
const DEFAULT_PUSH_HOST = 'openapi.tigerfintech.com';
|
|
21
22
|
/** Default push server port */
|
|
@@ -28,8 +29,9 @@ const DEFAULT_RECONNECT_INTERVAL = 5000;
|
|
|
28
29
|
const MAX_RECONNECT_INTERVAL = 60000;
|
|
29
30
|
/** Default connect timeout (ms) */
|
|
30
31
|
const DEFAULT_CONNECT_TIMEOUT = 30000;
|
|
32
|
+
import { SDK_VERSION as _SDK_VERSION } from '../version.js';
|
|
31
33
|
/** SDK version identifier */
|
|
32
|
-
const SDK_VERSION =
|
|
34
|
+
const SDK_VERSION = `typescript/${_SDK_VERSION}`;
|
|
33
35
|
/** Protocol version */
|
|
34
36
|
const ACCEPT_VERSION = '1.0';
|
|
35
37
|
/** Default send heartbeat interval (ms) */
|
|
@@ -50,11 +52,13 @@ export class PushClient {
|
|
|
50
52
|
constructor(config, options) {
|
|
51
53
|
this.socket = null;
|
|
52
54
|
this._state = ConnectionState.Disconnected;
|
|
55
|
+
/** Whether this connect attempt already fell back to unverified TLS */
|
|
56
|
+
this.tlsFallback = false;
|
|
53
57
|
this.callbacks = {};
|
|
54
58
|
/** Subscription state: subject -> Set<symbol> */
|
|
55
59
|
this.subscriptions = new Map();
|
|
56
|
-
/** Account-level subscriptions */
|
|
57
|
-
this.accountSubs = new
|
|
60
|
+
/** Account-level subscriptions: subject -> account string used at subscribe time */
|
|
61
|
+
this.accountSubs = new Map();
|
|
58
62
|
this.heartbeatTimer = null;
|
|
59
63
|
this.reconnectTimer = null;
|
|
60
64
|
this.stopped = false;
|
|
@@ -88,6 +92,7 @@ export class PushClient {
|
|
|
88
92
|
}
|
|
89
93
|
this._state = ConnectionState.Connecting;
|
|
90
94
|
this.stopped = false;
|
|
95
|
+
this.tlsFallback = false;
|
|
91
96
|
this.recvBuffer = Buffer.alloc(0);
|
|
92
97
|
const socket = this.createSocket(this.pushHost, this.pushPort);
|
|
93
98
|
this.socket = socket;
|
|
@@ -144,6 +149,24 @@ export class PushClient {
|
|
|
144
149
|
});
|
|
145
150
|
socket.on('error', (err) => {
|
|
146
151
|
if (!settled) {
|
|
152
|
+
// If this is a TLS certificate error, warn and retry without verification.
|
|
153
|
+
const isTlsCertError = err.message.includes('certificate') ||
|
|
154
|
+
err.message.includes('CERT') ||
|
|
155
|
+
err.code === 'DEPTH_ZERO_SELF_SIGNED_CERT' ||
|
|
156
|
+
err.code === 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' ||
|
|
157
|
+
err.code === 'CERT_HAS_EXPIRED';
|
|
158
|
+
if (isTlsCertError && !this.tlsFallback) {
|
|
159
|
+
this.tlsFallback = true;
|
|
160
|
+
settled = true;
|
|
161
|
+
clearTimeout(timeout);
|
|
162
|
+
socket.destroy();
|
|
163
|
+
this.socket = null;
|
|
164
|
+
this._state = ConnectionState.Disconnected;
|
|
165
|
+
console.warn(`[TigerOpen] TLS certificate verification failed: ${err.message}. Retrying without verification.`);
|
|
166
|
+
// Retry immediately using unverified TLS
|
|
167
|
+
this.connect().then(resolve).catch(reject);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
147
170
|
settled = true;
|
|
148
171
|
clearTimeout(timeout);
|
|
149
172
|
this._state = ConnectionState.Disconnected;
|
|
@@ -196,7 +219,7 @@ export class PushClient {
|
|
|
196
219
|
createSocket(host, port) {
|
|
197
220
|
if (this.socketFactory)
|
|
198
221
|
return this.socketFactory(host, port);
|
|
199
|
-
return tls.connect({ host, port, rejectUnauthorized:
|
|
222
|
+
return tls.connect({ host, port, rejectUnauthorized: !this.tlsFallback });
|
|
200
223
|
}
|
|
201
224
|
/** Handle incoming TCP data: buffer + decode varint32 frames */
|
|
202
225
|
handleData(chunk, onConnected) {
|
|
@@ -295,8 +318,8 @@ export class PushClient {
|
|
|
295
318
|
for (const [subject, symbols] of this.subscriptions) {
|
|
296
319
|
this.doSubscribe(subject, Array.from(symbols));
|
|
297
320
|
}
|
|
298
|
-
for (const subject of this.accountSubs) {
|
|
299
|
-
this.doSubscribe(subject, undefined,
|
|
321
|
+
for (const [subject, account] of this.accountSubs) {
|
|
322
|
+
this.doSubscribe(subject, undefined, account);
|
|
300
323
|
}
|
|
301
324
|
}
|
|
302
325
|
/** Dispatch push data to the appropriate callback based on dataType */
|
|
@@ -320,8 +343,10 @@ export class PushClient {
|
|
|
320
343
|
cb.onDepth?.(pushData.quoteDepthData);
|
|
321
344
|
break;
|
|
322
345
|
case SocketCommon_DataType.TradeTick:
|
|
346
|
+
if (pushData.tickData)
|
|
347
|
+
cb.onFullTick?.(pushData.tickData);
|
|
323
348
|
if (pushData.tradeTickData)
|
|
324
|
-
cb.onTick?.(pushData.tradeTickData);
|
|
349
|
+
cb.onTick?.(convertTradeTick(pushData.tradeTickData));
|
|
325
350
|
break;
|
|
326
351
|
case SocketCommon_DataType.Asset:
|
|
327
352
|
if (pushData.assetData)
|
|
@@ -351,9 +376,13 @@ export class PushClient {
|
|
|
351
376
|
if (pushData.klineData)
|
|
352
377
|
cb.onKline?.(pushData.klineData);
|
|
353
378
|
break;
|
|
379
|
+
case SocketCommon_DataType.Cc:
|
|
380
|
+
// 加密货币(Cc)推送复用 QuoteData body,路由到 onQuote 回调
|
|
381
|
+
if (pushData.quoteData)
|
|
382
|
+
cb.onQuote?.(pushData.quoteData);
|
|
383
|
+
break;
|
|
354
384
|
default:
|
|
355
|
-
//
|
|
356
|
-
// TickData (fullTick) via tickData field
|
|
385
|
+
// QuoteBBO and some full-tick payloads reuse an existing dataType.
|
|
357
386
|
if (pushData.tickData)
|
|
358
387
|
cb.onFullTick?.(pushData.tickData);
|
|
359
388
|
break;
|
|
@@ -457,11 +486,77 @@ export class PushClient {
|
|
|
457
486
|
this.doUnsubscribe(SubjectType.Kline, symbols);
|
|
458
487
|
this.removeSubscription(SubjectType.Kline, symbols);
|
|
459
488
|
}
|
|
489
|
+
// ===== v0.4.0: stock_top / option_top / cc / market =====
|
|
490
|
+
/**
|
|
491
|
+
* Subscribe to stock top rankings (v0.4.0).
|
|
492
|
+
* @param market US / HK / CN
|
|
493
|
+
* @param indicators LATEST_PRICE / HIGHER / LOWER / VOLUME / AMOUNT / AMPLITUDE / TURNOVER_RATE
|
|
494
|
+
*/
|
|
495
|
+
subscribeStockTop(market, indicators) {
|
|
496
|
+
this.doSubscribe(SubjectType.StockTop, indicators, undefined, market);
|
|
497
|
+
this.addSubscription(SubjectType.StockTop, indicators);
|
|
498
|
+
}
|
|
499
|
+
/** Unsubscribe from stock top rankings (v0.4.0). */
|
|
500
|
+
unsubscribeStockTop(market, indicators) {
|
|
501
|
+
const dataType = subjectToDataType(SubjectType.StockTop);
|
|
502
|
+
const symbolsStr = indicators.join(',');
|
|
503
|
+
const req = buildUnSubscribeMessage(dataType, symbolsStr, undefined, market);
|
|
504
|
+
this.sendMessage(req);
|
|
505
|
+
this.removeSubscription(SubjectType.StockTop, indicators);
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Subscribe to option top rankings (v0.4.0).
|
|
509
|
+
* @param market US / HK / CN
|
|
510
|
+
*/
|
|
511
|
+
subscribeOptionTop(market, indicators) {
|
|
512
|
+
this.doSubscribe(SubjectType.OptionTop, indicators, undefined, market);
|
|
513
|
+
this.addSubscription(SubjectType.OptionTop, indicators);
|
|
514
|
+
}
|
|
515
|
+
/** Unsubscribe from option top rankings (v0.4.0). */
|
|
516
|
+
unsubscribeOptionTop(market, indicators) {
|
|
517
|
+
const dataType = subjectToDataType(SubjectType.OptionTop);
|
|
518
|
+
const symbolsStr = indicators.join(',');
|
|
519
|
+
const req = buildUnSubscribeMessage(dataType, symbolsStr, undefined, market);
|
|
520
|
+
this.sendMessage(req);
|
|
521
|
+
this.removeSubscription(SubjectType.OptionTop, indicators);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Subscribe to crypto realtime quotes (v0.4.0).
|
|
525
|
+
* Data is delivered via the `onQuote` callback (same QuoteData body).
|
|
526
|
+
* @param symbols e.g. ["BTCUSD", "ETHUSD"]
|
|
527
|
+
*/
|
|
528
|
+
subscribeCc(symbols) {
|
|
529
|
+
this.doSubscribe(SubjectType.Cc, symbols);
|
|
530
|
+
this.addSubscription(SubjectType.Cc, symbols);
|
|
531
|
+
}
|
|
532
|
+
/** Unsubscribe from crypto realtime quotes (v0.4.0). */
|
|
533
|
+
unsubscribeCc(symbols) {
|
|
534
|
+
this.doUnsubscribe(SubjectType.Cc, symbols);
|
|
535
|
+
this.removeSubscription(SubjectType.Cc, symbols);
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Subscribe to market status updates (v0.4.0).
|
|
539
|
+
* Protocol uses dataType=Quote with only the `market` field set.
|
|
540
|
+
* Data is delivered via the `onQuote` callback.
|
|
541
|
+
*/
|
|
542
|
+
subscribeMarket(market) {
|
|
543
|
+
// symbols 为空,只带 market 字段
|
|
544
|
+
this.doSubscribe(SubjectType.Market, undefined, undefined, market);
|
|
545
|
+
this.addSubscription(SubjectType.Market, [market]);
|
|
546
|
+
}
|
|
547
|
+
/** Unsubscribe from market status (v0.4.0). */
|
|
548
|
+
unsubscribeMarket(market) {
|
|
549
|
+
const dataType = subjectToDataType(SubjectType.Market);
|
|
550
|
+
const req = buildUnSubscribeMessage(dataType, undefined, undefined, market);
|
|
551
|
+
this.sendMessage(req);
|
|
552
|
+
this.removeSubscription(SubjectType.Market, [market]);
|
|
553
|
+
}
|
|
460
554
|
// ===== Account push subscribe/unsubscribe =====
|
|
461
555
|
/** Subscribe to asset changes */
|
|
462
556
|
subscribeAsset(account) {
|
|
463
|
-
|
|
464
|
-
this.
|
|
557
|
+
const acct = account || this.config.account;
|
|
558
|
+
this.doSubscribe(SubjectType.Asset, undefined, acct);
|
|
559
|
+
this.accountSubs.set(SubjectType.Asset, acct);
|
|
465
560
|
}
|
|
466
561
|
/** Unsubscribe from asset changes */
|
|
467
562
|
unsubscribeAsset() {
|
|
@@ -470,8 +565,9 @@ export class PushClient {
|
|
|
470
565
|
}
|
|
471
566
|
/** Subscribe to position changes */
|
|
472
567
|
subscribePosition(account) {
|
|
473
|
-
|
|
474
|
-
this.
|
|
568
|
+
const acct = account || this.config.account;
|
|
569
|
+
this.doSubscribe(SubjectType.Position, undefined, acct);
|
|
570
|
+
this.accountSubs.set(SubjectType.Position, acct);
|
|
475
571
|
}
|
|
476
572
|
/** Unsubscribe from position changes */
|
|
477
573
|
unsubscribePosition() {
|
|
@@ -480,8 +576,9 @@ export class PushClient {
|
|
|
480
576
|
}
|
|
481
577
|
/** Subscribe to order status */
|
|
482
578
|
subscribeOrder(account) {
|
|
483
|
-
|
|
484
|
-
this.
|
|
579
|
+
const acct = account || this.config.account;
|
|
580
|
+
this.doSubscribe(SubjectType.Order, undefined, acct);
|
|
581
|
+
this.accountSubs.set(SubjectType.Order, acct);
|
|
485
582
|
}
|
|
486
583
|
/** Unsubscribe from order status */
|
|
487
584
|
unsubscribeOrder() {
|
|
@@ -490,8 +587,9 @@ export class PushClient {
|
|
|
490
587
|
}
|
|
491
588
|
/** Subscribe to transaction details */
|
|
492
589
|
subscribeTransaction(account) {
|
|
493
|
-
|
|
494
|
-
this.
|
|
590
|
+
const acct = account || this.config.account;
|
|
591
|
+
this.doSubscribe(SubjectType.Transaction, undefined, acct);
|
|
592
|
+
this.accountSubs.set(SubjectType.Transaction, acct);
|
|
495
593
|
}
|
|
496
594
|
/** Unsubscribe from transaction details */
|
|
497
595
|
unsubscribeTransaction() {
|
|
@@ -507,8 +605,8 @@ export class PushClient {
|
|
|
507
605
|
}
|
|
508
606
|
return result;
|
|
509
607
|
}
|
|
510
|
-
/** Get account-level subscriptions */
|
|
608
|
+
/** Get account-level subscriptions: subject -> account */
|
|
511
609
|
getAccountSubscriptions() {
|
|
512
|
-
return
|
|
610
|
+
return new Map(this.accountSubs);
|
|
513
611
|
}
|
|
514
612
|
}
|
|
@@ -17,6 +17,10 @@ export var SubjectType;
|
|
|
17
17
|
SubjectType["OptionTop"] = "option_top";
|
|
18
18
|
SubjectType["FullTick"] = "full_tick";
|
|
19
19
|
SubjectType["QuoteBBO"] = "quote_bbo";
|
|
20
|
+
/** 加密货币实时行情 (v0.4.0) */
|
|
21
|
+
SubjectType["Cc"] = "cc";
|
|
22
|
+
/** 市场状态 (v0.4.0) */
|
|
23
|
+
SubjectType["Market"] = "market";
|
|
20
24
|
SubjectType["Asset"] = "asset";
|
|
21
25
|
SubjectType["Position"] = "position";
|
|
22
26
|
SubjectType["Order"] = "order";
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
const PART_CODE_NAME_MAP = {
|
|
2
|
+
a: 'NYSE American, LLC (NYSE American)',
|
|
3
|
+
b: 'NASDAQ OMX BX, Inc. (NASDAQ OMX BX)',
|
|
4
|
+
c: 'NYSE National, Inc. (NYSE National)',
|
|
5
|
+
d: 'FINRA Alternative Display Facility (ADF)',
|
|
6
|
+
h: 'MIAX Pearl Exchange, LLC (MIAX)',
|
|
7
|
+
i: 'International Securities Exchange, LLC (ISE)',
|
|
8
|
+
j: 'Cboe EDGA Exchange, Inc. (Cboe EDGA)',
|
|
9
|
+
k: 'Cboe EDGX Exchange, Inc. (Cboe EDGX)',
|
|
10
|
+
l: 'Long-Term Stock Exchange, Inc. (LTSE)',
|
|
11
|
+
m: 'NYSE Chicago, Inc. (NYSE Chicago)',
|
|
12
|
+
n: 'New York Stock Exchange, LLC (NYSE)',
|
|
13
|
+
p: 'NYSE Arca, Inc. (NYSE Arca)',
|
|
14
|
+
s: 'Consolidated Tape System (CTS)',
|
|
15
|
+
t: 'NASDAQ Stock Market, LLC (NASDAQ)',
|
|
16
|
+
u: 'Members Exchange, LLC (MEMX)',
|
|
17
|
+
v: "Investors' Exchange, LLC. (IEX)",
|
|
18
|
+
w: 'CBOE Stock Exchange, Inc. (CBSX)',
|
|
19
|
+
x: 'NASDAQ OMX PSX, Inc. (NASDAQ OMX PSX)',
|
|
20
|
+
y: 'Cboe BYX Exchange, Inc. (Cboe BYX)',
|
|
21
|
+
z: 'Cboe BZX Exchange, Inc. (Cboe BZX)',
|
|
22
|
+
};
|
|
23
|
+
const PART_CODE_SHORT_NAME_MAP = {
|
|
24
|
+
a: 'AMEX',
|
|
25
|
+
b: 'BX',
|
|
26
|
+
c: 'NSX',
|
|
27
|
+
d: 'ADF',
|
|
28
|
+
h: 'MIAX',
|
|
29
|
+
i: 'ISE',
|
|
30
|
+
j: 'EDGA',
|
|
31
|
+
k: 'EDGX',
|
|
32
|
+
l: 'LTSE',
|
|
33
|
+
m: 'CHO',
|
|
34
|
+
n: 'NYSE',
|
|
35
|
+
p: 'ARCA',
|
|
36
|
+
s: 'CTS',
|
|
37
|
+
t: 'NSDQ',
|
|
38
|
+
u: 'MEMX',
|
|
39
|
+
v: 'IEX',
|
|
40
|
+
w: 'CBSX',
|
|
41
|
+
x: 'PSX',
|
|
42
|
+
y: 'BYX',
|
|
43
|
+
z: 'BZX',
|
|
44
|
+
};
|
|
45
|
+
const US_TRADE_COND = {
|
|
46
|
+
' ': 'US_REGULAR_SALE',
|
|
47
|
+
B: 'US_BUNCHED_TRADE',
|
|
48
|
+
C: 'US_CASH_TRADE',
|
|
49
|
+
F: 'US_INTERMARKET_SWEEP',
|
|
50
|
+
G: 'US_BUNCHED_SOLD_TRADE',
|
|
51
|
+
H: 'US_PRICE_VARIATION_TRADE',
|
|
52
|
+
I: 'US_ODD_LOT_TRADE',
|
|
53
|
+
K: 'US_RULE_127_OR_155_TRADE',
|
|
54
|
+
L: 'US_SOLD_LAST',
|
|
55
|
+
M: 'US_MARKET_CENTER_CLOSE_PRICE',
|
|
56
|
+
N: 'US_NEXT_DAY_TRADE',
|
|
57
|
+
O: 'US_MARKET_CENTER_OPENING_TRADE',
|
|
58
|
+
P: 'US_PRIOR_REFERENCE_PRICE',
|
|
59
|
+
Q: 'US_MARKET_CENTER_OPEN_PRICE',
|
|
60
|
+
R: 'US_SELLER',
|
|
61
|
+
T: 'US_FORM_T',
|
|
62
|
+
U: 'US_EXTENDED_TRADING_HOURS',
|
|
63
|
+
V: 'US_CONTINGENT_TRADE',
|
|
64
|
+
W: 'US_AVERAGE_PRICE_TRADE',
|
|
65
|
+
X: 'US_CROSS_TRADE',
|
|
66
|
+
Z: 'US_SOLD_OUT_OF_SEQUENCE',
|
|
67
|
+
'0': 'US_ODD_LOST_CROSS_TRADE',
|
|
68
|
+
'4': 'US_DERIVATIVELY_PRICED',
|
|
69
|
+
'5': 'US_MARKET_CENTER_RE_OPENING_TRADE',
|
|
70
|
+
'6': 'US_MARKET_CENTER_CLOSING_TRADE',
|
|
71
|
+
'7': 'US_QUALIFIED_CONTINGENT_TRADE',
|
|
72
|
+
'9': 'US_CONSOLIDATED_LAST_PRICE_PER_LISTING_PACKET',
|
|
73
|
+
};
|
|
74
|
+
const HK_TRADE_COND = {
|
|
75
|
+
' ': 'HK_AUTOMATCH_NORMAL',
|
|
76
|
+
D: 'HK_ODD_LOT_TRADE',
|
|
77
|
+
U: 'HK_AUCTION_TRADE',
|
|
78
|
+
'*': 'HK_OVERSEAS_TRADE',
|
|
79
|
+
P: 'HK_LATE_TRADE_OFF_EXCHG',
|
|
80
|
+
M: 'HK_NON_DIRECT_OFF_EXCHG_TRADE',
|
|
81
|
+
X: 'HK_DIRECT_OFF_EXCHG_TRADE',
|
|
82
|
+
Y: 'HK_AUTOMATIC_INTERNALIZED',
|
|
83
|
+
};
|
|
84
|
+
/** Returns the full exchange name for a partCode letter, or the raw code if unknown. */
|
|
85
|
+
export function getPartNameByCode(code) {
|
|
86
|
+
if (!code)
|
|
87
|
+
return code;
|
|
88
|
+
return PART_CODE_NAME_MAP[code] ?? code;
|
|
89
|
+
}
|
|
90
|
+
/** Returns the short exchange name for a partCode letter, or the raw code if unknown. */
|
|
91
|
+
export function getPartShortNameByCode(code) {
|
|
92
|
+
if (!code)
|
|
93
|
+
return code;
|
|
94
|
+
return PART_CODE_SHORT_NAME_MAP[code] ?? code;
|
|
95
|
+
}
|
|
96
|
+
/** Resolves a raw cond character to a readable string. */
|
|
97
|
+
export function getTradeCondByCode(isUs, ch) {
|
|
98
|
+
const map = isUs ? US_TRADE_COND : HK_TRADE_COND;
|
|
99
|
+
return map[ch] ?? ch;
|
|
100
|
+
}
|
|
101
|
+
const US_SYMBOL_RE = /^[A-Z]+(\.[A-Z0-9]+)?$/;
|
|
102
|
+
/** Reports whether symbol is a US stock (uppercase letters, optional dot suffix). */
|
|
103
|
+
export function isUsStockSymbol(symbol) {
|
|
104
|
+
return US_SYMBOL_RE.test(symbol);
|
|
105
|
+
}
|
|
106
|
+
function isUsSymbol(symbol) {
|
|
107
|
+
return US_SYMBOL_RE.test(symbol);
|
|
108
|
+
}
|
|
109
|
+
function getTradeCond(isUs, ch) {
|
|
110
|
+
const map = isUs ? US_TRADE_COND : HK_TRADE_COND;
|
|
111
|
+
return map[ch] ?? ch;
|
|
112
|
+
}
|
|
113
|
+
/** Decodes a TradeTickData pb message into a PushTradeTick with resolved partCode/partName. */
|
|
114
|
+
export function convertTradeTick(src) {
|
|
115
|
+
if (src.secType === 'FUT')
|
|
116
|
+
return convertFutureTick(src);
|
|
117
|
+
return convertStockTick(src);
|
|
118
|
+
}
|
|
119
|
+
function convertStockTick(src) {
|
|
120
|
+
const isUs = isUsSymbol(src.symbol);
|
|
121
|
+
const denominator = Math.pow(10, src.priceOffset);
|
|
122
|
+
const ticks = [];
|
|
123
|
+
let currentTime = 0;
|
|
124
|
+
for (let i = 0; i < src.time.length; i++) {
|
|
125
|
+
currentTime += src.time[i];
|
|
126
|
+
const condCh = src.cond.length > i ? src.cond[i] : ' ';
|
|
127
|
+
const rawCode = i < src.partCode.length ? src.partCode[i] : '';
|
|
128
|
+
ticks.push({
|
|
129
|
+
sn: src.sn + i,
|
|
130
|
+
time: currentTime,
|
|
131
|
+
price: (src.priceBase + src.price[i]) / denominator,
|
|
132
|
+
volume: src.volume[i],
|
|
133
|
+
tickType: src.type.length > i ? src.type[i] : '',
|
|
134
|
+
cond: getTradeCond(isUs, condCh),
|
|
135
|
+
partCode: getPartShortNameByCode(rawCode),
|
|
136
|
+
partName: getPartNameByCode(rawCode),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return { symbol: src.symbol, secType: src.secType, quoteLevel: src.quoteLevel, timestamp: src.timestamp, ticks };
|
|
140
|
+
}
|
|
141
|
+
function convertFutureTick(src) {
|
|
142
|
+
const denominator = Math.pow(10, src.priceOffset);
|
|
143
|
+
const ticks = [];
|
|
144
|
+
let currentTime = 0;
|
|
145
|
+
let startSn = src.sn;
|
|
146
|
+
for (let i = 0; i < src.time.length; i++) {
|
|
147
|
+
currentTime += src.time[i];
|
|
148
|
+
const curPrice = (src.priceBase + src.price[i]) / denominator;
|
|
149
|
+
const mv = src.mergedVols[i];
|
|
150
|
+
for (let j = 0; j < mv.vol.length; j++) {
|
|
151
|
+
ticks.push({
|
|
152
|
+
sn: startSn * 10 + j,
|
|
153
|
+
time: currentTime,
|
|
154
|
+
price: curPrice,
|
|
155
|
+
volume: mv.vol[j],
|
|
156
|
+
tickType: '',
|
|
157
|
+
cond: '',
|
|
158
|
+
partCode: '',
|
|
159
|
+
partName: '',
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
startSn++;
|
|
163
|
+
}
|
|
164
|
+
return { symbol: src.symbol, secType: src.secType, quoteLevel: '', timestamp: src.timestamp, ticks };
|
|
165
|
+
}
|
package/dist/esm/quote/index.js
CHANGED