tigeropen 0.4.0 → 0.5.7

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.
Files changed (116) hide show
  1. package/README.md +82 -2
  2. package/dist/cjs/auth/authentication.js +74 -0
  3. package/dist/cjs/auth/index.js +24 -0
  4. package/dist/cjs/auth/oauth2/client-registration.js +44 -0
  5. package/dist/cjs/auth/oauth2/device-authorization.js +40 -0
  6. package/dist/cjs/auth/oauth2/errors.js +64 -0
  7. package/dist/cjs/auth/oauth2/http-utils.js +112 -0
  8. package/dist/cjs/auth/oauth2/index.js +42 -0
  9. package/dist/cjs/auth/oauth2/loopback-receiver.js +215 -0
  10. package/dist/cjs/auth/oauth2/metadata.js +34 -0
  11. package/dist/cjs/auth/oauth2/oauth2-authentication.js +74 -0
  12. package/dist/cjs/auth/oauth2/session-manager.js +762 -0
  13. package/dist/cjs/auth/oauth2/token-store.js +252 -0
  14. package/dist/cjs/auth/oauth2/token.js +76 -0
  15. package/dist/cjs/client/api-response.js +28 -7
  16. package/dist/cjs/client/case-convert.js +4 -3
  17. package/dist/cjs/client/http-client.js +124 -8
  18. package/dist/cjs/client/token-methods.js +42 -0
  19. package/dist/cjs/config/client-config.js +115 -16
  20. package/dist/cjs/config/config-parser.js +19 -9
  21. package/dist/cjs/config/index.js +4 -1
  22. package/dist/cjs/config/token-manager.js +54 -4
  23. package/dist/cjs/index.js +53 -2
  24. package/dist/cjs/model/enums.js +18 -1
  25. package/dist/cjs/model/order-helpers.js +149 -0
  26. package/dist/cjs/push/index.js +1 -0
  27. package/dist/cjs/push/pb/Request.js +12 -1
  28. package/dist/cjs/push/proto-message.js +5 -1
  29. package/dist/cjs/push/push-client.js +69 -25
  30. package/dist/cjs/push/tick-util.js +172 -0
  31. package/dist/cjs/quote/quote-client.js +300 -70
  32. package/dist/cjs/trade/trade-client.js +186 -16
  33. package/dist/cjs/version.js +4 -0
  34. package/dist/esm/auth/authentication.js +68 -0
  35. package/dist/esm/auth/index.js +3 -0
  36. package/dist/esm/auth/oauth2/client-registration.js +40 -0
  37. package/dist/esm/auth/oauth2/device-authorization.js +36 -0
  38. package/dist/esm/auth/oauth2/errors.js +59 -0
  39. package/dist/esm/auth/oauth2/http-utils.js +105 -0
  40. package/dist/esm/auth/oauth2/index.js +11 -0
  41. package/dist/esm/auth/oauth2/loopback-receiver.js +178 -0
  42. package/dist/esm/auth/oauth2/metadata.js +30 -0
  43. package/dist/esm/auth/oauth2/oauth2-authentication.js +70 -0
  44. package/dist/esm/auth/oauth2/session-manager.js +725 -0
  45. package/dist/esm/auth/oauth2/token-store.js +214 -0
  46. package/dist/esm/auth/oauth2/token.js +71 -0
  47. package/dist/esm/client/api-request.js +1 -1
  48. package/dist/esm/client/api-response.js +29 -8
  49. package/dist/esm/client/case-convert.js +4 -3
  50. package/dist/esm/client/http-client.js +128 -12
  51. package/dist/esm/client/index.js +6 -6
  52. package/dist/esm/client/token-methods.js +37 -0
  53. package/dist/esm/config/client-config.js +118 -20
  54. package/dist/esm/config/config-parser.js +19 -9
  55. package/dist/esm/config/index.js +4 -3
  56. package/dist/esm/config/token-manager.js +55 -5
  57. package/dist/esm/index.js +15 -1
  58. package/dist/esm/logger/index.js +1 -1
  59. package/dist/esm/model/contract-helpers.js +1 -1
  60. package/dist/esm/model/enums.js +17 -0
  61. package/dist/esm/model/order-helpers.js +142 -1
  62. package/dist/esm/model/order-status.js +1 -1
  63. package/dist/esm/package.json +3 -0
  64. package/dist/esm/push/index.js +6 -5
  65. package/dist/esm/push/pb/PushData.js +11 -11
  66. package/dist/esm/push/pb/QuoteBasicData.js +1 -1
  67. package/dist/esm/push/pb/Request.js +12 -1
  68. package/dist/esm/push/pb/Response.js +1 -1
  69. package/dist/esm/push/pb/index.js +17 -17
  70. package/dist/esm/push/proto-message.js +8 -4
  71. package/dist/esm/push/push-client.js +76 -32
  72. package/dist/esm/push/tick-util.js +165 -0
  73. package/dist/esm/quote/index.js +1 -1
  74. package/dist/esm/quote/quote-client.js +269 -72
  75. package/dist/esm/signer/index.js +2 -2
  76. package/dist/esm/trade/index.js +1 -1
  77. package/dist/esm/trade/trade-client.js +156 -19
  78. package/dist/esm/version.js +1 -0
  79. package/dist/types/auth/authentication.d.ts +98 -0
  80. package/dist/types/auth/index.d.ts +4 -0
  81. package/dist/types/auth/oauth2/client-registration.d.ts +32 -0
  82. package/dist/types/auth/oauth2/device-authorization.d.ts +40 -0
  83. package/dist/types/auth/oauth2/errors.d.ts +46 -0
  84. package/dist/types/auth/oauth2/http-utils.d.ts +21 -0
  85. package/dist/types/auth/oauth2/index.d.ts +16 -0
  86. package/dist/types/auth/oauth2/loopback-receiver.d.ts +29 -0
  87. package/dist/types/auth/oauth2/metadata.d.ts +32 -0
  88. package/dist/types/auth/oauth2/oauth2-authentication.d.ts +31 -0
  89. package/dist/types/auth/oauth2/session-manager.d.ts +241 -0
  90. package/dist/types/auth/oauth2/token-store.d.ts +49 -0
  91. package/dist/types/auth/oauth2/token.d.ts +56 -0
  92. package/dist/types/client/api-response.d.ts +6 -4
  93. package/dist/types/client/case-convert.d.ts +3 -1
  94. package/dist/types/client/http-client.d.ts +32 -1
  95. package/dist/types/client/token-methods.d.ts +34 -0
  96. package/dist/types/config/client-config.d.ts +22 -1
  97. package/dist/types/config/index.d.ts +3 -1
  98. package/dist/types/config/token-manager.d.ts +27 -3
  99. package/dist/types/index.d.ts +22 -1
  100. package/dist/types/model/enums.d.ts +18 -2
  101. package/dist/types/model/order-helpers.d.ts +52 -1
  102. package/dist/types/model/order.d.ts +127 -10
  103. package/dist/types/model/quote-requests.d.ts +27 -15
  104. package/dist/types/model/quote.d.ts +151 -24
  105. package/dist/types/model/trade-requests.d.ts +123 -4
  106. package/dist/types/model/trade.d.ts +121 -17
  107. package/dist/types/push/callbacks.d.ts +3 -7
  108. package/dist/types/push/index.d.ts +2 -1
  109. package/dist/types/push/pb/Request.d.ts +1 -0
  110. package/dist/types/push/proto-message.d.ts +4 -1
  111. package/dist/types/push/push-client.d.ts +38 -3
  112. package/dist/types/push/tick-util.d.ts +30 -0
  113. package/dist/types/quote/quote-client.d.ts +101 -36
  114. package/dist/types/trade/trade-client.d.ts +69 -14
  115. package/dist/types/version.d.ts +1 -0
  116. package/package.json +9 -4
@@ -22,6 +22,7 @@ var push_message_1 = require("./push-message");
22
22
  Object.defineProperty(exports, "SubjectType", { enumerable: true, get: function () { return push_message_1.SubjectType; } });
23
23
  __exportStar(require("./callbacks"), exports);
24
24
  __exportStar(require("./varint"), exports);
25
+ __exportStar(require("./tick-util"), exports);
25
26
  __exportStar(require("./proto-message"), exports);
26
27
  var push_client_1 = require("./push-client");
27
28
  Object.defineProperty(exports, "PushClient", { enumerable: true, get: function () { return push_client_1.PushClient; } });
@@ -203,7 +203,7 @@ exports.Request_Connect = {
203
203
  },
204
204
  };
205
205
  function createBaseRequest_Subscribe() {
206
- return { dataType: 0, symbols: undefined, account: undefined, market: undefined };
206
+ return { dataType: 0, symbols: undefined, account: undefined, market: undefined, assetQuoteType: undefined };
207
207
  }
208
208
  exports.Request_Subscribe = {
209
209
  encode(message, writer = new wire_1.BinaryWriter()) {
@@ -219,6 +219,9 @@ exports.Request_Subscribe = {
219
219
  if (message.market !== undefined) {
220
220
  writer.uint32(34).string(message.market);
221
221
  }
222
+ if (message.assetQuoteType !== undefined) {
223
+ writer.uint32(42).string(message.assetQuoteType);
224
+ }
222
225
  return writer;
223
226
  },
224
227
  decode(input, length) {
@@ -256,6 +259,13 @@ exports.Request_Subscribe = {
256
259
  message.market = reader.string();
257
260
  continue;
258
261
  }
262
+ case 5: {
263
+ if (tag !== 42) {
264
+ break;
265
+ }
266
+ message.assetQuoteType = reader.string();
267
+ continue;
268
+ }
259
269
  }
260
270
  if ((tag & 7) === 4 || tag === 0) {
261
271
  break;
@@ -273,6 +283,7 @@ exports.Request_Subscribe = {
273
283
  message.symbols = object.symbols ?? undefined;
274
284
  message.account = object.account ?? undefined;
275
285
  message.market = object.market ?? undefined;
286
+ message.assetQuoteType = object.assetQuoteType ?? undefined;
276
287
  return message;
277
288
  },
278
289
  };
@@ -53,8 +53,11 @@ function buildHeartBeatMessage() {
53
53
  }
54
54
  /**
55
55
  * 构建 SUBSCRIBE 请求消息
56
+ *
57
+ * @param assetQuoteType 资产行情类型枚举名(ETH / RTH / OVERNIGHT),
58
+ * 仅对 Asset / Position 两种订阅有意义,其余场景传 undefined
56
59
  */
57
- function buildSubscribeMessage(dataType, symbols, account, market) {
60
+ function buildSubscribeMessage(dataType, symbols, account, market, assetQuoteType) {
58
61
  return Request_1.Request.fromPartial({
59
62
  command: SocketCommon_1.SocketCommon_Command.SUBSCRIBE,
60
63
  id: nextRequestId(),
@@ -63,6 +66,7 @@ function buildSubscribeMessage(dataType, symbols, account, market) {
63
66
  symbols,
64
67
  account,
65
68
  market,
69
+ assetQuoteType,
66
70
  }),
67
71
  });
68
72
  }
@@ -47,6 +47,7 @@ exports.PushClient = exports.ConnectionState = void 0;
47
47
  const tls = __importStar(require("tls"));
48
48
  const signer_1 = require("../signer/signer");
49
49
  const push_message_1 = require("./push-message");
50
+ const tick_util_1 = require("./tick-util");
50
51
  const Request_1 = require("./pb/Request");
51
52
  const Response_1 = require("./pb/Response");
52
53
  const SocketCommon_1 = require("./pb/SocketCommon");
@@ -64,8 +65,9 @@ const DEFAULT_RECONNECT_INTERVAL = 5000;
64
65
  const MAX_RECONNECT_INTERVAL = 60000;
65
66
  /** Default connect timeout (ms) */
66
67
  const DEFAULT_CONNECT_TIMEOUT = 30000;
68
+ const version_1 = require("../version");
67
69
  /** SDK version identifier */
68
- const SDK_VERSION = 'typescript/0.4.0';
70
+ const SDK_VERSION = `typescript/${version_1.SDK_VERSION}`;
69
71
  /** Protocol version */
70
72
  const ACCEPT_VERSION = '1.0';
71
73
  /** Default send heartbeat interval (ms) */
@@ -86,11 +88,15 @@ class PushClient {
86
88
  constructor(config, options) {
87
89
  this.socket = null;
88
90
  this._state = ConnectionState.Disconnected;
91
+ /** Whether this connect attempt already fell back to unverified TLS */
92
+ this.tlsFallback = false;
89
93
  this.callbacks = {};
90
94
  /** Subscription state: subject -> Set<symbol> */
91
95
  this.subscriptions = new Map();
92
- /** Account-level subscriptions */
93
- this.accountSubs = new Set();
96
+ /** Account-level subscriptions: subject -> account string used at subscribe time */
97
+ this.accountSubs = new Map();
98
+ /** Account-level subscriptions: subject -> assetQuoteType used at subscribe time (reconnect replay) */
99
+ this.accountSubQuoteTypes = new Map();
94
100
  this.heartbeatTimer = null;
95
101
  this.reconnectTimer = null;
96
102
  this.stopped = false;
@@ -124,6 +130,7 @@ class PushClient {
124
130
  }
125
131
  this._state = ConnectionState.Connecting;
126
132
  this.stopped = false;
133
+ this.tlsFallback = false;
127
134
  this.recvBuffer = Buffer.alloc(0);
128
135
  const socket = this.createSocket(this.pushHost, this.pushPort);
129
136
  this.socket = socket;
@@ -180,6 +187,24 @@ class PushClient {
180
187
  });
181
188
  socket.on('error', (err) => {
182
189
  if (!settled) {
190
+ // If this is a TLS certificate error, warn and retry without verification.
191
+ const isTlsCertError = err.message.includes('certificate') ||
192
+ err.message.includes('CERT') ||
193
+ err.code === 'DEPTH_ZERO_SELF_SIGNED_CERT' ||
194
+ err.code === 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' ||
195
+ err.code === 'CERT_HAS_EXPIRED';
196
+ if (isTlsCertError && !this.tlsFallback) {
197
+ this.tlsFallback = true;
198
+ settled = true;
199
+ clearTimeout(timeout);
200
+ socket.destroy();
201
+ this.socket = null;
202
+ this._state = ConnectionState.Disconnected;
203
+ console.warn(`[TigerOpen] TLS certificate verification failed: ${err.message}. Retrying without verification.`);
204
+ // Retry immediately using unverified TLS
205
+ this.connect().then(resolve).catch(reject);
206
+ return;
207
+ }
183
208
  settled = true;
184
209
  clearTimeout(timeout);
185
210
  this._state = ConnectionState.Disconnected;
@@ -232,7 +257,7 @@ class PushClient {
232
257
  createSocket(host, port) {
233
258
  if (this.socketFactory)
234
259
  return this.socketFactory(host, port);
235
- return tls.connect({ host, port, rejectUnauthorized: false });
260
+ return tls.connect({ host, port, rejectUnauthorized: !this.tlsFallback });
236
261
  }
237
262
  /** Handle incoming TCP data: buffer + decode varint32 frames */
238
263
  handleData(chunk, onConnected) {
@@ -331,8 +356,8 @@ class PushClient {
331
356
  for (const [subject, symbols] of this.subscriptions) {
332
357
  this.doSubscribe(subject, Array.from(symbols));
333
358
  }
334
- for (const subject of this.accountSubs) {
335
- this.doSubscribe(subject, undefined, this.config.account);
359
+ for (const [subject, account] of this.accountSubs) {
360
+ this.doSubscribe(subject, undefined, account, undefined, this.accountSubQuoteTypes.get(subject));
336
361
  }
337
362
  }
338
363
  /** Dispatch push data to the appropriate callback based on dataType */
@@ -356,8 +381,10 @@ class PushClient {
356
381
  cb.onDepth?.(pushData.quoteDepthData);
357
382
  break;
358
383
  case SocketCommon_1.SocketCommon_DataType.TradeTick:
384
+ if (pushData.tickData)
385
+ cb.onFullTick?.(pushData.tickData);
359
386
  if (pushData.tradeTickData)
360
- cb.onTick?.(pushData.tradeTickData);
387
+ cb.onTick?.((0, tick_util_1.convertTradeTick)(pushData.tradeTickData));
361
388
  break;
362
389
  case SocketCommon_1.SocketCommon_DataType.Asset:
363
390
  if (pushData.assetData)
@@ -393,18 +420,17 @@ class PushClient {
393
420
  cb.onQuote?.(pushData.quoteData);
394
421
  break;
395
422
  default:
396
- // FullTick and QuoteBBO use TradeTick/Quote dataType
397
- // TickData (fullTick) via tickData field
423
+ // QuoteBBO and some full-tick payloads reuse an existing dataType.
398
424
  if (pushData.tickData)
399
425
  cb.onFullTick?.(pushData.tickData);
400
426
  break;
401
427
  }
402
428
  }
403
429
  /** Internal subscribe method */
404
- doSubscribe(subject, symbols, account, market) {
430
+ doSubscribe(subject, symbols, account, market, assetQuoteType) {
405
431
  const dataType = (0, proto_message_1.subjectToDataType)(subject);
406
432
  const symbolsStr = symbols ? symbols.join(',') : undefined;
407
- const req = (0, proto_message_1.buildSubscribeMessage)(dataType, symbolsStr, account, market);
433
+ const req = (0, proto_message_1.buildSubscribeMessage)(dataType, symbolsStr, account, market, assetQuoteType);
408
434
  this.sendMessage(req);
409
435
  }
410
436
  /** Internal unsubscribe method */
@@ -564,30 +590,47 @@ class PushClient {
564
590
  this.removeSubscription(push_message_1.SubjectType.Market, [market]);
565
591
  }
566
592
  // ===== Account push subscribe/unsubscribe =====
567
- /** Subscribe to asset changes */
568
- subscribeAsset(account) {
569
- this.doSubscribe(push_message_1.SubjectType.Asset, undefined, account || this.config.account);
570
- this.accountSubs.add(push_message_1.SubjectType.Asset);
593
+ /**
594
+ * Shared implementation for the account/options overloads of
595
+ * subscribeAsset / subscribePosition.
596
+ */
597
+ subscribeAccount(subject, accountOrOptions) {
598
+ const opts = typeof accountOrOptions === 'string'
599
+ ? { account: accountOrOptions }
600
+ : (accountOrOptions ?? {});
601
+ const acct = opts.account || this.config.account;
602
+ this.doSubscribe(subject, undefined, acct, undefined, opts.assetQuoteType);
603
+ this.accountSubs.set(subject, acct);
604
+ if (opts.assetQuoteType) {
605
+ this.accountSubQuoteTypes.set(subject, opts.assetQuoteType);
606
+ }
607
+ else {
608
+ this.accountSubQuoteTypes.delete(subject);
609
+ }
610
+ }
611
+ subscribeAsset(accountOrOptions) {
612
+ this.subscribeAccount(push_message_1.SubjectType.Asset, accountOrOptions);
571
613
  }
572
614
  /** Unsubscribe from asset changes */
573
615
  unsubscribeAsset() {
574
616
  this.doUnsubscribe(push_message_1.SubjectType.Asset);
575
617
  this.accountSubs.delete(push_message_1.SubjectType.Asset);
618
+ this.accountSubQuoteTypes.delete(push_message_1.SubjectType.Asset);
576
619
  }
577
- /** Subscribe to position changes */
578
- subscribePosition(account) {
579
- this.doSubscribe(push_message_1.SubjectType.Position, undefined, account || this.config.account);
580
- this.accountSubs.add(push_message_1.SubjectType.Position);
620
+ subscribePosition(accountOrOptions) {
621
+ this.subscribeAccount(push_message_1.SubjectType.Position, accountOrOptions);
581
622
  }
582
623
  /** Unsubscribe from position changes */
583
624
  unsubscribePosition() {
584
625
  this.doUnsubscribe(push_message_1.SubjectType.Position);
585
626
  this.accountSubs.delete(push_message_1.SubjectType.Position);
627
+ this.accountSubQuoteTypes.delete(push_message_1.SubjectType.Position);
586
628
  }
587
629
  /** Subscribe to order status */
588
630
  subscribeOrder(account) {
589
- this.doSubscribe(push_message_1.SubjectType.Order, undefined, account || this.config.account);
590
- this.accountSubs.add(push_message_1.SubjectType.Order);
631
+ const acct = account || this.config.account;
632
+ this.doSubscribe(push_message_1.SubjectType.Order, undefined, acct);
633
+ this.accountSubs.set(push_message_1.SubjectType.Order, acct);
591
634
  }
592
635
  /** Unsubscribe from order status */
593
636
  unsubscribeOrder() {
@@ -596,8 +639,9 @@ class PushClient {
596
639
  }
597
640
  /** Subscribe to transaction details */
598
641
  subscribeTransaction(account) {
599
- this.doSubscribe(push_message_1.SubjectType.Transaction, undefined, account || this.config.account);
600
- this.accountSubs.add(push_message_1.SubjectType.Transaction);
642
+ const acct = account || this.config.account;
643
+ this.doSubscribe(push_message_1.SubjectType.Transaction, undefined, acct);
644
+ this.accountSubs.set(push_message_1.SubjectType.Transaction, acct);
601
645
  }
602
646
  /** Unsubscribe from transaction details */
603
647
  unsubscribeTransaction() {
@@ -613,9 +657,9 @@ class PushClient {
613
657
  }
614
658
  return result;
615
659
  }
616
- /** Get account-level subscriptions */
660
+ /** Get account-level subscriptions: subject -> account */
617
661
  getAccountSubscriptions() {
618
- return Array.from(this.accountSubs);
662
+ return new Map(this.accountSubs);
619
663
  }
620
664
  }
621
665
  exports.PushClient = PushClient;
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPartNameByCode = getPartNameByCode;
4
+ exports.getPartShortNameByCode = getPartShortNameByCode;
5
+ exports.getTradeCondByCode = getTradeCondByCode;
6
+ exports.isUsStockSymbol = isUsStockSymbol;
7
+ exports.convertTradeTick = convertTradeTick;
8
+ const PART_CODE_NAME_MAP = {
9
+ a: 'NYSE American, LLC (NYSE American)',
10
+ b: 'NASDAQ OMX BX, Inc. (NASDAQ OMX BX)',
11
+ c: 'NYSE National, Inc. (NYSE National)',
12
+ d: 'FINRA Alternative Display Facility (ADF)',
13
+ h: 'MIAX Pearl Exchange, LLC (MIAX)',
14
+ i: 'International Securities Exchange, LLC (ISE)',
15
+ j: 'Cboe EDGA Exchange, Inc. (Cboe EDGA)',
16
+ k: 'Cboe EDGX Exchange, Inc. (Cboe EDGX)',
17
+ l: 'Long-Term Stock Exchange, Inc. (LTSE)',
18
+ m: 'NYSE Chicago, Inc. (NYSE Chicago)',
19
+ n: 'New York Stock Exchange, LLC (NYSE)',
20
+ p: 'NYSE Arca, Inc. (NYSE Arca)',
21
+ s: 'Consolidated Tape System (CTS)',
22
+ t: 'NASDAQ Stock Market, LLC (NASDAQ)',
23
+ u: 'Members Exchange, LLC (MEMX)',
24
+ v: "Investors' Exchange, LLC. (IEX)",
25
+ w: 'CBOE Stock Exchange, Inc. (CBSX)',
26
+ x: 'NASDAQ OMX PSX, Inc. (NASDAQ OMX PSX)',
27
+ y: 'Cboe BYX Exchange, Inc. (Cboe BYX)',
28
+ z: 'Cboe BZX Exchange, Inc. (Cboe BZX)',
29
+ };
30
+ const PART_CODE_SHORT_NAME_MAP = {
31
+ a: 'AMEX',
32
+ b: 'BX',
33
+ c: 'NSX',
34
+ d: 'ADF',
35
+ h: 'MIAX',
36
+ i: 'ISE',
37
+ j: 'EDGA',
38
+ k: 'EDGX',
39
+ l: 'LTSE',
40
+ m: 'CHO',
41
+ n: 'NYSE',
42
+ p: 'ARCA',
43
+ s: 'CTS',
44
+ t: 'NSDQ',
45
+ u: 'MEMX',
46
+ v: 'IEX',
47
+ w: 'CBSX',
48
+ x: 'PSX',
49
+ y: 'BYX',
50
+ z: 'BZX',
51
+ };
52
+ const US_TRADE_COND = {
53
+ ' ': 'US_REGULAR_SALE',
54
+ B: 'US_BUNCHED_TRADE',
55
+ C: 'US_CASH_TRADE',
56
+ F: 'US_INTERMARKET_SWEEP',
57
+ G: 'US_BUNCHED_SOLD_TRADE',
58
+ H: 'US_PRICE_VARIATION_TRADE',
59
+ I: 'US_ODD_LOT_TRADE',
60
+ K: 'US_RULE_127_OR_155_TRADE',
61
+ L: 'US_SOLD_LAST',
62
+ M: 'US_MARKET_CENTER_CLOSE_PRICE',
63
+ N: 'US_NEXT_DAY_TRADE',
64
+ O: 'US_MARKET_CENTER_OPENING_TRADE',
65
+ P: 'US_PRIOR_REFERENCE_PRICE',
66
+ Q: 'US_MARKET_CENTER_OPEN_PRICE',
67
+ R: 'US_SELLER',
68
+ T: 'US_FORM_T',
69
+ U: 'US_EXTENDED_TRADING_HOURS',
70
+ V: 'US_CONTINGENT_TRADE',
71
+ W: 'US_AVERAGE_PRICE_TRADE',
72
+ X: 'US_CROSS_TRADE',
73
+ Z: 'US_SOLD_OUT_OF_SEQUENCE',
74
+ '0': 'US_ODD_LOST_CROSS_TRADE',
75
+ '4': 'US_DERIVATIVELY_PRICED',
76
+ '5': 'US_MARKET_CENTER_RE_OPENING_TRADE',
77
+ '6': 'US_MARKET_CENTER_CLOSING_TRADE',
78
+ '7': 'US_QUALIFIED_CONTINGENT_TRADE',
79
+ '9': 'US_CONSOLIDATED_LAST_PRICE_PER_LISTING_PACKET',
80
+ };
81
+ const HK_TRADE_COND = {
82
+ ' ': 'HK_AUTOMATCH_NORMAL',
83
+ D: 'HK_ODD_LOT_TRADE',
84
+ U: 'HK_AUCTION_TRADE',
85
+ '*': 'HK_OVERSEAS_TRADE',
86
+ P: 'HK_LATE_TRADE_OFF_EXCHG',
87
+ M: 'HK_NON_DIRECT_OFF_EXCHG_TRADE',
88
+ X: 'HK_DIRECT_OFF_EXCHG_TRADE',
89
+ Y: 'HK_AUTOMATIC_INTERNALIZED',
90
+ };
91
+ /** Returns the full exchange name for a partCode letter, or the raw code if unknown. */
92
+ function getPartNameByCode(code) {
93
+ if (!code)
94
+ return code;
95
+ return PART_CODE_NAME_MAP[code] ?? code;
96
+ }
97
+ /** Returns the short exchange name for a partCode letter, or the raw code if unknown. */
98
+ function getPartShortNameByCode(code) {
99
+ if (!code)
100
+ return code;
101
+ return PART_CODE_SHORT_NAME_MAP[code] ?? code;
102
+ }
103
+ /** Resolves a raw cond character to a readable string. */
104
+ function getTradeCondByCode(isUs, ch) {
105
+ const map = isUs ? US_TRADE_COND : HK_TRADE_COND;
106
+ return map[ch] ?? ch;
107
+ }
108
+ const US_SYMBOL_RE = /^[A-Z]+(\.[A-Z0-9]+)?$/;
109
+ /** Reports whether symbol is a US stock (uppercase letters, optional dot suffix). */
110
+ function isUsStockSymbol(symbol) {
111
+ return US_SYMBOL_RE.test(symbol);
112
+ }
113
+ function isUsSymbol(symbol) {
114
+ return US_SYMBOL_RE.test(symbol);
115
+ }
116
+ function getTradeCond(isUs, ch) {
117
+ const map = isUs ? US_TRADE_COND : HK_TRADE_COND;
118
+ return map[ch] ?? ch;
119
+ }
120
+ /** Decodes a TradeTickData pb message into a PushTradeTick with resolved partCode/partName. */
121
+ function convertTradeTick(src) {
122
+ if (src.secType === 'FUT')
123
+ return convertFutureTick(src);
124
+ return convertStockTick(src);
125
+ }
126
+ function convertStockTick(src) {
127
+ const isUs = isUsSymbol(src.symbol);
128
+ const denominator = Math.pow(10, src.priceOffset);
129
+ const ticks = [];
130
+ let currentTime = 0;
131
+ for (let i = 0; i < src.time.length; i++) {
132
+ currentTime += src.time[i];
133
+ const condCh = src.cond.length > i ? src.cond[i] : ' ';
134
+ const rawCode = i < src.partCode.length ? src.partCode[i] : '';
135
+ ticks.push({
136
+ sn: src.sn + i,
137
+ time: currentTime,
138
+ price: (src.priceBase + src.price[i]) / denominator,
139
+ volume: src.volume[i],
140
+ tickType: src.type.length > i ? src.type[i] : '',
141
+ cond: getTradeCond(isUs, condCh),
142
+ partCode: getPartShortNameByCode(rawCode),
143
+ partName: getPartNameByCode(rawCode),
144
+ });
145
+ }
146
+ return { symbol: src.symbol, secType: src.secType, quoteLevel: src.quoteLevel, timestamp: src.timestamp, ticks };
147
+ }
148
+ function convertFutureTick(src) {
149
+ const denominator = Math.pow(10, src.priceOffset);
150
+ const ticks = [];
151
+ let currentTime = 0;
152
+ let startSn = src.sn;
153
+ for (let i = 0; i < src.time.length; i++) {
154
+ currentTime += src.time[i];
155
+ const curPrice = (src.priceBase + src.price[i]) / denominator;
156
+ const mv = src.mergedVols[i];
157
+ for (let j = 0; j < mv.vol.length; j++) {
158
+ ticks.push({
159
+ sn: startSn * 10 + j,
160
+ time: currentTime,
161
+ price: curPrice,
162
+ volume: mv.vol[j],
163
+ tickType: '',
164
+ cond: '',
165
+ partCode: '',
166
+ partName: '',
167
+ });
168
+ }
169
+ startSn++;
170
+ }
171
+ return { symbol: src.symbol, secType: src.secType, quoteLevel: '', timestamp: src.timestamp, ticks };
172
+ }