@tigeropenapi/tigeropen 0.1.0 → 0.3.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.
Files changed (129) hide show
  1. package/README.md +475 -104
  2. package/dist/cjs/client/api-request.js +16 -10
  3. package/dist/cjs/client/api-response.js +23 -3
  4. package/dist/cjs/client/case-convert.js +35 -0
  5. package/dist/cjs/client/http-client.js +46 -20
  6. package/dist/cjs/client/index.js +6 -2
  7. package/dist/cjs/config/client-config.js +130 -49
  8. package/dist/cjs/config/domain.js +21 -2
  9. package/dist/cjs/config/index.js +3 -1
  10. package/dist/cjs/index.js +1 -1
  11. package/dist/cjs/model/contract.js +1 -2
  12. package/dist/cjs/model/order-helpers.js +9 -9
  13. package/dist/cjs/model/order.js +5 -2
  14. package/dist/cjs/model/position.js +1 -2
  15. package/dist/cjs/model/quote.js +7 -0
  16. package/dist/cjs/model/trade.js +2 -0
  17. package/dist/cjs/push/index.js +6 -3
  18. package/dist/cjs/push/pb/AssetData.js +208 -0
  19. package/dist/cjs/push/pb/KlineData.js +184 -0
  20. package/dist/cjs/push/pb/OptionTopData.js +427 -0
  21. package/dist/cjs/push/pb/OrderStatusData.js +568 -0
  22. package/dist/cjs/push/pb/OrderTransactionData.js +256 -0
  23. package/dist/cjs/push/pb/PositionData.js +316 -0
  24. package/dist/cjs/push/pb/PushData.js +219 -0
  25. package/dist/cjs/push/pb/QuoteBBOData.js +160 -0
  26. package/dist/cjs/push/pb/QuoteBasicData.js +329 -0
  27. package/dist/cjs/push/pb/QuoteData.js +507 -0
  28. package/dist/cjs/push/pb/QuoteDepthData.js +220 -0
  29. package/dist/cjs/push/pb/Request.js +278 -0
  30. package/dist/cjs/push/pb/Response.js +97 -0
  31. package/dist/cjs/push/pb/SocketCommon.js +86 -0
  32. package/dist/cjs/push/pb/StockTopData.js +199 -0
  33. package/dist/cjs/push/pb/TickData.js +202 -0
  34. package/dist/cjs/push/pb/TradeTickData.js +308 -0
  35. package/dist/cjs/push/pb/index.js +49 -0
  36. package/dist/cjs/push/proto-message.js +129 -0
  37. package/dist/cjs/push/push-client.js +292 -164
  38. package/dist/cjs/push/push-message.js +4 -60
  39. package/dist/cjs/push/varint.js +76 -0
  40. package/dist/cjs/quote/index.js +3 -2
  41. package/dist/cjs/quote/quote-client.js +128 -94
  42. package/dist/cjs/signer/signer.js +10 -10
  43. package/dist/cjs/trade/trade-client.js +56 -62
  44. package/dist/esm/client/api-request.js +14 -8
  45. package/dist/esm/client/api-response.js +22 -3
  46. package/dist/esm/client/case-convert.js +31 -0
  47. package/dist/esm/client/http-client.js +47 -21
  48. package/dist/esm/client/index.js +3 -2
  49. package/dist/esm/config/client-config.js +131 -51
  50. package/dist/esm/config/domain.js +20 -2
  51. package/dist/esm/config/index.js +2 -2
  52. package/dist/esm/index.js +1 -1
  53. package/dist/esm/model/contract.js +1 -2
  54. package/dist/esm/model/order-helpers.js +9 -9
  55. package/dist/esm/model/order.js +5 -2
  56. package/dist/esm/model/position.js +1 -2
  57. package/dist/esm/model/quote.js +6 -0
  58. package/dist/esm/model/trade.js +1 -0
  59. package/dist/esm/push/index.js +4 -2
  60. package/dist/esm/push/pb/AssetData.js +205 -0
  61. package/dist/esm/push/pb/KlineData.js +181 -0
  62. package/dist/esm/push/pb/OptionTopData.js +424 -0
  63. package/dist/esm/push/pb/OrderStatusData.js +565 -0
  64. package/dist/esm/push/pb/OrderTransactionData.js +253 -0
  65. package/dist/esm/push/pb/PositionData.js +313 -0
  66. package/dist/esm/push/pb/PushData.js +216 -0
  67. package/dist/esm/push/pb/QuoteBBOData.js +157 -0
  68. package/dist/esm/push/pb/QuoteBasicData.js +326 -0
  69. package/dist/esm/push/pb/QuoteData.js +504 -0
  70. package/dist/esm/push/pb/QuoteDepthData.js +217 -0
  71. package/dist/esm/push/pb/Request.js +275 -0
  72. package/dist/esm/push/pb/Response.js +94 -0
  73. package/dist/esm/push/pb/SocketCommon.js +83 -0
  74. package/dist/esm/push/pb/StockTopData.js +196 -0
  75. package/dist/esm/push/pb/TickData.js +199 -0
  76. package/dist/esm/push/pb/TradeTickData.js +305 -0
  77. package/dist/esm/push/pb/index.js +25 -0
  78. package/dist/esm/push/proto-message.js +121 -0
  79. package/dist/esm/push/push-client.js +260 -165
  80. package/dist/esm/push/push-message.js +3 -56
  81. package/dist/esm/push/varint.js +72 -0
  82. package/dist/esm/quote/index.js +2 -2
  83. package/dist/esm/quote/quote-client.js +127 -94
  84. package/dist/esm/signer/signer.js +10 -10
  85. package/dist/esm/trade/trade-client.js +56 -62
  86. package/dist/types/client/api-request.d.ts +12 -11
  87. package/dist/types/client/api-response.d.ts +15 -7
  88. package/dist/types/client/case-convert.d.ts +16 -0
  89. package/dist/types/client/http-client.d.ts +26 -14
  90. package/dist/types/client/index.d.ts +3 -2
  91. package/dist/types/config/client-config.d.ts +29 -13
  92. package/dist/types/config/domain.d.ts +9 -0
  93. package/dist/types/config/index.d.ts +2 -2
  94. package/dist/types/index.d.ts +1 -1
  95. package/dist/types/model/contract.d.ts +13 -29
  96. package/dist/types/model/order-helpers.d.ts +21 -20
  97. package/dist/types/model/order.d.ts +101 -55
  98. package/dist/types/model/position.d.ts +26 -24
  99. package/dist/types/model/quote.d.ts +311 -0
  100. package/dist/types/model/trade.d.ts +113 -0
  101. package/dist/types/push/callbacks.d.ts +32 -51
  102. package/dist/types/push/index.d.ts +5 -3
  103. package/dist/types/push/pb/AssetData.d.ts +47 -0
  104. package/dist/types/push/pb/KlineData.d.ts +43 -0
  105. package/dist/types/push/pb/OptionTopData.d.ts +76 -0
  106. package/dist/types/push/pb/OrderStatusData.d.ts +96 -0
  107. package/dist/types/push/pb/OrderTransactionData.d.ts +51 -0
  108. package/dist/types/push/pb/PositionData.d.ts +63 -0
  109. package/dist/types/push/pb/PushData.d.ts +46 -0
  110. package/dist/types/push/pb/QuoteBBOData.d.ts +35 -0
  111. package/dist/types/push/pb/QuoteBasicData.d.ts +66 -0
  112. package/dist/types/push/pb/QuoteData.d.ts +94 -0
  113. package/dist/types/push/pb/QuoteDepthData.d.ts +37 -0
  114. package/dist/types/push/pb/Request.d.ts +44 -0
  115. package/dist/types/push/pb/Response.d.ts +30 -0
  116. package/dist/types/push/pb/SocketCommon.d.ts +64 -0
  117. package/dist/types/push/pb/StockTopData.d.ts +37 -0
  118. package/dist/types/push/pb/TickData.d.ts +44 -0
  119. package/dist/types/push/pb/TradeTickData.d.ts +53 -0
  120. package/dist/types/push/pb/index.d.ts +39 -0
  121. package/dist/types/push/proto-message.d.ts +33 -0
  122. package/dist/types/push/push-client.d.ts +69 -68
  123. package/dist/types/push/push-message.d.ts +3 -147
  124. package/dist/types/push/varint.d.ts +23 -0
  125. package/dist/types/quote/index.d.ts +2 -2
  126. package/dist/types/quote/quote-client.d.ts +41 -47
  127. package/dist/types/signer/signer.d.ts +6 -6
  128. package/dist/types/trade/trade-client.d.ts +34 -38
  129. package/package.json +11 -6
package/README.md CHANGED
@@ -1,70 +1,390 @@
1
1
  # Tiger OpenAPI TypeScript SDK
2
2
 
3
- 老虎证券 OpenAPI 的 TypeScript SDK,提供行情查询、交易下单、账户管理和实时推送等功能。
3
+ TypeScript SDK for Tiger Brokers OpenAPI — market data, trading, account management, and real-time push notifications.
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/@tigeropenapi/tigeropen.svg)](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
7
 
8
- ## 安装
8
+ ## Features
9
+
10
+ - Market data queries (quotes, klines, depth, options, futures)
11
+ - Order placement, modification, cancellation, and account queries
12
+ - Real-time push via TCP + TLS + Protobuf (quotes, trades, account events)
13
+ - RSA request signing and Tiger public key response verification
14
+ - Auto-discovery of configuration from properties files
15
+ - ESM and CommonJS dual-package support
16
+
17
+ ## Installation
9
18
 
10
19
  ```bash
11
20
  npm install @tigeropenapi/tigeropen
12
- # 或
21
+ # or
13
22
  yarn add @tigeropenapi/tigeropen
14
- # 或
23
+ # or
15
24
  pnpm add @tigeropenapi/tigeropen
16
25
  ```
17
26
 
18
- 要求 Node.js >= 16.0.0。
27
+ Requires Node.js >= 16.0.0.
19
28
 
20
29
  ## Quick Start
21
30
 
22
- 以下是一个完整的示例,从配置到查询行情:
23
-
24
31
  ```typescript
25
32
  import { createClientConfig } from '@tigeropenapi/tigeropen';
26
33
  import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client';
27
34
  import { QuoteClient } from '@tigeropenapi/tigeropen/quote/quote-client';
28
35
 
29
- // 1. 创建配置(从 properties 文件加载)
36
+ // Auto-discovers ~/.tigeropen/tiger_openapi_config.properties
37
+ const config = createClientConfig();
38
+
39
+ const httpClient = new HttpClient(config);
40
+ const qc = new QuoteClient(httpClient);
41
+
42
+ const states = await qc.marketState('US');
43
+ console.log('Market state:', states);
44
+ ```
45
+
46
+ ## Configuration
47
+
48
+ Configuration values are resolved with the following priority:
49
+
50
+ **environment variables > code options > config file > defaults**
51
+
52
+ ### Method 1: Properties file (explicit path)
53
+
54
+ Point directly to a properties file:
55
+
56
+ ```typescript
57
+ import { createClientConfig } from '@tigeropenapi/tigeropen';
58
+
30
59
  const config = createClientConfig({
31
- propertiesFilePath: 'tiger_openapi_config.properties',
60
+ propertiesFilePath: '/path/to/tiger_openapi_config.properties',
32
61
  });
62
+ ```
33
63
 
34
- // 2. 创建 HTTP 客户端
35
- const httpClient = new HttpClient(config);
64
+ File format:
36
65
 
37
- // 3. 创建行情客户端并查询
38
- const qc = new QuoteClient(httpClient);
39
- const states = await qc.getMarketState('US');
40
- console.log('美股市场状态:', states);
66
+ ```properties
67
+ tiger_id=your_developer_id
68
+ private_key=your_rsa_private_key
69
+ account=your_trading_account
41
70
  ```
42
71
 
43
- ## 配置
72
+ ### Method 2: Auto-discovery
73
+
74
+ When called with no arguments, `createClientConfig()` automatically searches for `tiger_openapi_config.properties` in these locations (in order):
75
+
76
+ 1. `./tiger_openapi_config.properties` (current working directory)
77
+ 2. `~/.tigeropen/tiger_openapi_config.properties` (home directory)
78
+
79
+ ```typescript
80
+ import { createClientConfig } from '@tigeropenapi/tigeropen';
81
+
82
+ // No arguments — auto-discovers config from well-known locations
83
+ const config = createClientConfig();
84
+ ```
44
85
 
45
- SDK 支持三种配置方式,优先级:**环境变量 > 代码设置(含配置文件) > 默认值**。
86
+ This is the simplest approach — just place a `tiger_openapi_config.properties` file in your project root or `~/.tigeropen/` directory.
46
87
 
47
- ### 方式一:代码直接设置
88
+ ### Method 3: Code options
89
+
90
+ Pass configuration values directly:
48
91
 
49
92
  ```typescript
50
93
  import { createClientConfig } from '@tigeropenapi/tigeropen';
51
94
 
52
95
  const config = createClientConfig({
53
- tigerId: '你的 tiger_id',
54
- privateKey: '你的 RSA 私钥',
55
- account: '你的交易账户',
96
+ tigerId: 'your_tiger_id',
97
+ privateKey: 'your_rsa_private_key',
98
+ account: 'your_account',
56
99
  });
57
100
  ```
58
101
 
59
- ### 方式二:从 properties 配置文件加载
102
+ ### Method 4: Environment variables
103
+
104
+ Set the following environment variables:
105
+
106
+ ```bash
107
+ export TIGEROPEN_TIGER_ID=your_developer_id
108
+ export TIGEROPEN_PRIVATE_KEY=your_rsa_private_key
109
+ export TIGEROPEN_ACCOUNT=your_trading_account
110
+ ```
111
+
112
+ Then create the config without any arguments:
113
+
114
+ ```typescript
115
+ const config = createClientConfig();
116
+ ```
117
+
118
+ Environment variables always take the highest priority and override values from code or config files.
119
+
120
+ ### Config options
121
+
122
+ | Field | Description | Required | Default |
123
+ |-------|-------------|----------|---------|
124
+ | `tigerId` | Developer ID | Yes | — |
125
+ | `privateKey` | RSA private key | Yes | — |
126
+ | `account` | Trading account | No | — |
127
+ | `language` | Language (`zh_CN` / `en_US`) | No | `zh_CN` |
128
+ | `timeout` | Request timeout (seconds) | No | `15` |
129
+
130
+ ### Response signature verification
131
+
132
+ The SDK includes the Tiger public key (`tigerPublicKey` field in `ClientConfig`) for verifying that API responses originate from Tiger's servers and have not been tampered with.
133
+
134
+ ## Market Data
60
135
 
61
136
  ```typescript
137
+ import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client';
138
+ import { QuoteClient } from '@tigeropenapi/tigeropen/quote/quote-client';
139
+
140
+ const httpClient = new HttpClient(config);
141
+ const qc = new QuoteClient(httpClient);
142
+
143
+ // Market status
144
+ const states = await qc.marketState('US');
145
+
146
+ // Real-time quotes
147
+ const quotes = await qc.quoteRealTime(['AAPL', 'TSLA']);
148
+
149
+ // K-line data
150
+ const klines = await qc.kline('AAPL', 'day');
151
+
152
+ // Intraday timeline
153
+ const timeline = await qc.timeline(['AAPL']);
154
+
155
+ // Order book depth
156
+ const depth = await qc.quoteDepth('AAPL');
157
+
158
+ // Option expirations
159
+ const expiry = await qc.optionExpiration('AAPL');
160
+
161
+ // Option chain
162
+ const chain = await qc.optionChain('AAPL', '2024-01-19');
163
+
164
+ // Futures exchanges
165
+ const exchanges = await qc.futureExchange();
166
+ ```
167
+
168
+ ## Trading
169
+
170
+ ```typescript
171
+ import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client';
172
+ import { TradeClient } from '@tigeropenapi/tigeropen/trade/trade-client';
173
+
174
+ const httpClient = new HttpClient(config);
175
+ const tc = new TradeClient(httpClient, config.account);
176
+
177
+ // Build a limit order
178
+ const order = {
179
+ symbol: 'AAPL',
180
+ secType: 'STK',
181
+ action: 'BUY',
182
+ orderType: 'LMT',
183
+ totalQuantity: 100,
184
+ limitPrice: 150.0,
185
+ timeInForce: 'DAY',
186
+ };
187
+
188
+ // Place order
189
+ const result = await tc.placeOrder(order);
190
+
191
+ // Preview order (no actual submission)
192
+ const preview = await tc.previewOrder(order);
193
+
194
+ // Modify order
195
+ await tc.modifyOrder(orderId, { ...order, limitPrice: 155.0 });
196
+
197
+ // Cancel order
198
+ await tc.cancelOrder(orderId);
199
+
200
+ // Query orders
201
+ const orders = await tc.orders();
202
+
203
+ // Query positions
204
+ const positions = await tc.positions();
205
+
206
+ // Query assets
207
+ const assets = await tc.assets();
208
+ ```
209
+
210
+ ## Generic Execute
211
+
212
+ For APIs not yet wrapped by the SDK, use `HttpClient.execute` directly:
213
+
214
+ ```typescript
215
+ const httpClient = new HttpClient(config);
216
+
217
+ const resp = await httpClient.execute('market_state', JSON.stringify({ market: 'US' }));
218
+ console.log('Raw response:', resp);
219
+ ```
220
+
221
+ ## Real-Time Push
222
+
223
+ The push client connects to Tiger's push server over a **TCP + TLS** connection using varint32 length-prefixed **Protobuf** framing. It supports automatic reconnection and heartbeat keep-alive.
224
+
225
+ Callback data types are Protobuf-generated types (e.g. `QuoteData`, `OrderStatusData`, `AssetData`, `PositionData`).
226
+
227
+ ```typescript
228
+ import { PushClient } from '@tigeropenapi/tigeropen/push/push-client';
229
+ import type { QuoteData } from '@tigeropenapi/tigeropen/push/pb/QuoteData';
230
+ import type { OrderStatusData } from '@tigeropenapi/tigeropen/push/pb/OrderStatusData';
231
+ import type { AssetData } from '@tigeropenapi/tigeropen/push/pb/AssetData';
232
+ import type { PositionData } from '@tigeropenapi/tigeropen/push/pb/PositionData';
233
+
234
+ const pc = new PushClient(config);
235
+
236
+ pc.setCallbacks({
237
+ onQuote: (data: QuoteData) => {
238
+ console.log(`Quote: ${data.symbol} price=${data.latestPrice}`);
239
+ },
240
+ onOrder: (data: OrderStatusData) => {
241
+ console.log(`Order: ${data.symbol} status=${data.status}`);
242
+ },
243
+ onAsset: (data: AssetData) => console.log('Asset update'),
244
+ onPosition: (data: PositionData) => console.log('Position update'),
245
+ onConnect: () => console.log('Connected'),
246
+ onDisconnect: () => console.log('Disconnected'),
247
+ onError: (err) => console.error('Error:', err),
248
+ });
249
+
250
+ // Connect (authenticates over TLS)
251
+ await pc.connect();
252
+
253
+ // Subscribe to real-time quotes
254
+ pc.subscribeQuote(['AAPL', 'TSLA']);
255
+
256
+ // Subscribe to account events
257
+ pc.subscribeAsset();
258
+ pc.subscribeOrder();
259
+ pc.subscribePosition();
260
+
261
+ // Unsubscribe
262
+ pc.unsubscribeQuote(['TSLA']);
263
+
264
+ // Disconnect
265
+ pc.disconnect();
266
+ ```
267
+
268
+ ### Available subscriptions
269
+
270
+ | Method | Data type | Callback |
271
+ |--------|-----------|----------|
272
+ | `subscribeQuote(symbols)` | Stock quotes | `onQuote(QuoteData)` |
273
+ | `subscribeTick(symbols)` | Trade ticks | `onTick(TradeTickData)` |
274
+ | `subscribeDepth(symbols)` | Order book depth | `onDepth(QuoteDepthData)` |
275
+ | `subscribeOption(symbols)` | Option quotes | `onOption(QuoteData)` |
276
+ | `subscribeFuture(symbols)` | Futures quotes | `onFuture(QuoteData)` |
277
+ | `subscribeKline(symbols)` | K-line data | `onKline(KlineData)` |
278
+ | `subscribeAsset(account?)` | Asset changes | `onAsset(AssetData)` |
279
+ | `subscribePosition(account?)` | Position changes | `onPosition(PositionData)` |
280
+ | `subscribeOrder(account?)` | Order status | `onOrder(OrderStatusData)` |
281
+ | `subscribeTransaction(account?)` | Transactions | `onTransaction(OrderTransactionData)` |
282
+
283
+ ## Project Structure
284
+
285
+ ```
286
+ openapi-typescript-sdk/
287
+ ├── src/
288
+ │ ├── config/ # Configuration management
289
+ │ ├── signer/ # RSA request signing
290
+ │ ├── client/ # HTTP client
291
+ │ ├── model/ # Data models and order helpers
292
+ │ ├── quote/ # Quote client (market data)
293
+ │ ├── trade/ # Trade client (orders, positions, assets)
294
+ │ ├── push/ # TCP+TLS push client (Protobuf)
295
+ │ ├── logger/ # Logger
296
+ │ └── index.ts # Public exports
297
+ ├── proto/ # Protobuf schema definitions
298
+ ├── examples/ # Usage examples
299
+ └── tests/ # Unit tests
300
+ ```
301
+
302
+ ## ESM and CommonJS
303
+
304
+ ```typescript
305
+ // ESM
306
+ import { createClientConfig } from '@tigeropenapi/tigeropen';
307
+
308
+ // CommonJS
309
+ const { createClientConfig } = require('@tigeropenapi/tigeropen');
310
+ ```
311
+
312
+ ## References
313
+
314
+ - [TypeScript SDK Documentation](https://docs.itigerup.com/docs/typescript)
315
+ - [Tiger Brokers OpenAPI Docs](https://quant.itigerup.com/openapi/zh/python/overview/introduction.html)
316
+ - [npm package](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
317
+
318
+ ## License
319
+
320
+ [MIT License](LICENSE)
321
+
322
+ ---
323
+
324
+ # Tiger OpenAPI TypeScript SDK(中文)
325
+
326
+ 老虎证券 OpenAPI TypeScript SDK —— 行情查询、交易下单、账户管理和实时推送。
327
+
328
+ [![npm version](https://img.shields.io/npm/v/@tigeropenapi/tigeropen.svg)](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
329
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
330
+
331
+ ## 功能特性
332
+
333
+ - 行情查询(报价、K 线、深度、期权、期货)
334
+ - 订单操作(下单、改单、撤单)和账户查询
335
+ - 实时推送:TCP + TLS + Protobuf(行情、成交、账户事件)
336
+ - RSA 请求签名和老虎公钥响应验签
337
+ - 自动发现配置文件
338
+ - ESM 和 CommonJS 双模块支持
339
+
340
+ ## 安装
341
+
342
+ ```bash
343
+ npm install @tigeropenapi/tigeropen
344
+ # 或
345
+ yarn add @tigeropenapi/tigeropen
346
+ # 或
347
+ pnpm add @tigeropenapi/tigeropen
348
+ ```
349
+
350
+ 需要 Node.js >= 16.0.0。
351
+
352
+ ## 快速开始
353
+
354
+ ```typescript
355
+ import { createClientConfig } from '@tigeropenapi/tigeropen';
356
+ import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client';
357
+ import { QuoteClient } from '@tigeropenapi/tigeropen/quote/quote-client';
358
+
359
+ // 自动发现 ~/.tigeropen/tiger_openapi_config.properties
360
+ const config = createClientConfig();
361
+
362
+ const httpClient = new HttpClient(config);
363
+ const qc = new QuoteClient(httpClient);
364
+
365
+ const states = await qc.marketState('US');
366
+ console.log('市场状态:', states);
367
+ ```
368
+
369
+ ## 配置
370
+
371
+ 配置值按以下优先级解析:
372
+
373
+ **环境变量 > 代码参数 > 配置文件 > 默认值**
374
+
375
+ ### 方式一:配置文件(指定路径)
376
+
377
+ 直接指定配置文件路径:
378
+
379
+ ```typescript
380
+ import { createClientConfig } from '@tigeropenapi/tigeropen';
381
+
62
382
  const config = createClientConfig({
63
- propertiesFilePath: 'tiger_openapi_config.properties',
383
+ propertiesFilePath: '/path/to/tiger_openapi_config.properties',
64
384
  });
65
385
  ```
66
386
 
67
- 配置文件格式:
387
+ 文件格式:
68
388
 
69
389
  ```properties
70
390
  tiger_id=你的开发者ID
@@ -72,7 +392,39 @@ private_key=你的RSA私钥
72
392
  account=你的交易账户
73
393
  ```
74
394
 
75
- ### 方式三:环境变量
395
+ ### 方式二:自动发现
396
+
397
+ 不传任何参数时,`createClientConfig()` 会自动按以下顺序搜索 `tiger_openapi_config.properties`:
398
+
399
+ 1. `./tiger_openapi_config.properties`(当前工作目录)
400
+ 2. `~/.tigeropen/tiger_openapi_config.properties`(用户主目录)
401
+
402
+ ```typescript
403
+ import { createClientConfig } from '@tigeropenapi/tigeropen';
404
+
405
+ // 无参数 —— 自动从常见位置发现配置
406
+ const config = createClientConfig();
407
+ ```
408
+
409
+ 最简单的方式 —— 只需将 `tiger_openapi_config.properties` 文件放在项目根目录或 `~/.tigeropen/` 目录下。
410
+
411
+ ### 方式三:代码传参
412
+
413
+ 直接传入配置值:
414
+
415
+ ```typescript
416
+ import { createClientConfig } from '@tigeropenapi/tigeropen';
417
+
418
+ const config = createClientConfig({
419
+ tigerId: 'your_tiger_id',
420
+ privateKey: 'your_rsa_private_key',
421
+ account: 'your_account',
422
+ });
423
+ ```
424
+
425
+ ### 方式四:环境变量
426
+
427
+ 设置以下环境变量:
76
428
 
77
429
  ```bash
78
430
  export TIGEROPEN_TIGER_ID=你的开发者ID
@@ -80,16 +432,27 @@ export TIGEROPEN_PRIVATE_KEY=你的RSA私钥
80
432
  export TIGEROPEN_ACCOUNT=你的交易账户
81
433
  ```
82
434
 
83
- ### 配置项说明
435
+ 然后无参数创建配置:
84
436
 
85
- | 配置项 | 说明 | 必填 | 默认值 |
86
- |--------|------|------|--------|
87
- | tigerId | 开发者 ID | 是 | - |
88
- | privateKey | RSA 私钥 | 是 | - |
89
- | account | 交易账户 | 否 | - |
90
- | language | 语言(zh_CN/en_US) | 否 | zh_CN |
91
- | timeout | 请求超时(秒) | 否 | 15 |
92
- | sandboxDebug | 是否使用沙箱环境 | 否 | false |
437
+ ```typescript
438
+ const config = createClientConfig();
439
+ ```
440
+
441
+ 环境变量优先级最高,会覆盖代码参数和配置文件中的值。
442
+
443
+ ### 配置项
444
+
445
+ | 字段 | 说明 | 必填 | 默认值 |
446
+ |------|------|------|--------|
447
+ | `tigerId` | 开发者 ID | 是 | — |
448
+ | `privateKey` | RSA 私钥 | 是 | — |
449
+ | `account` | 交易账户 | 否 | — |
450
+ | `language` | 语言(`zh_CN` / `en_US`) | 否 | `zh_CN` |
451
+ | `timeout` | 请求超时(秒) | 否 | `15` |
452
+
453
+ ### 响应签名验证
454
+
455
+ SDK 内置了老虎公钥(`ClientConfig` 中的 `tigerPublicKey` 字段),用于验证 API 响应来自老虎服务器且未被篡改。
93
456
 
94
457
  ## 行情查询
95
458
 
@@ -100,32 +463,32 @@ import { QuoteClient } from '@tigeropenapi/tigeropen/quote/quote-client';
100
463
  const httpClient = new HttpClient(config);
101
464
  const qc = new QuoteClient(httpClient);
102
465
 
103
- // 获取市场状态
104
- const states = await qc.getMarketState('US');
466
+ // 市场状态
467
+ const states = await qc.marketState('US');
105
468
 
106
- // 获取实时报价
107
- const briefs = await qc.getBrief(['AAPL', 'TSLA']);
469
+ // 实时报价
470
+ const quotes = await qc.quoteRealTime(['AAPL', 'TSLA']);
108
471
 
109
- // 获取 K 线数据
110
- const klines = await qc.getKline('AAPL', 'day');
472
+ // K 线数据
473
+ const klines = await qc.kline('AAPL', 'day');
111
474
 
112
- // 获取分时数据
113
- const timeline = await qc.getTimeline(['AAPL']);
475
+ // 分时数据
476
+ const timeline = await qc.timeline(['AAPL']);
114
477
 
115
- // 获取深度行情
116
- const depth = await qc.getQuoteDepth('AAPL');
478
+ // 盘口深度
479
+ const depth = await qc.quoteDepth('AAPL');
117
480
 
118
- // 获取期权到期日
119
- const expiry = await qc.getOptionExpiration('AAPL');
481
+ // 期权到期日
482
+ const expiry = await qc.optionExpiration('AAPL');
120
483
 
121
- // 获取期权链
122
- const chain = await qc.getOptionChain('AAPL', '2024-01-19');
484
+ // 期权链
485
+ const chain = await qc.optionChain('AAPL', '2024-01-19');
123
486
 
124
- // 获取期货交易所列表
125
- const exchanges = await qc.getFutureExchange();
487
+ // 期货交易所
488
+ const exchanges = await qc.futureExchange();
126
489
  ```
127
490
 
128
- ## 交易操作
491
+ ## 交易
129
492
 
130
493
  ```typescript
131
494
  import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client';
@@ -134,7 +497,7 @@ import { TradeClient } from '@tigeropenapi/tigeropen/trade/trade-client';
134
497
  const httpClient = new HttpClient(config);
135
498
  const tc = new TradeClient(httpClient, config.account);
136
499
 
137
- // 构造限价单
500
+ // 构建限价单
138
501
  const order = {
139
502
  symbol: 'AAPL',
140
503
  secType: 'STK',
@@ -148,112 +511,119 @@ const order = {
148
511
  // 下单
149
512
  const result = await tc.placeOrder(order);
150
513
 
151
- // 预览订单(不实际下单)
514
+ // 预览订单(不实际提交)
152
515
  const preview = await tc.previewOrder(order);
153
516
 
154
- // 修改订单
155
- const modifiedOrder = { ...order, limitPrice: 155.0 };
156
- await tc.modifyOrder(orderId, modifiedOrder);
517
+ // 改单
518
+ await tc.modifyOrder(orderId, { ...order, limitPrice: 155.0 });
157
519
 
158
- // 取消订单
520
+ // 撤单
159
521
  await tc.cancelOrder(orderId);
160
522
 
161
- // 查询全部订单
162
- const orders = await tc.getOrders();
523
+ // 查询订单
524
+ const orders = await tc.orders();
163
525
 
164
526
  // 查询持仓
165
- const positions = await tc.getPositions();
527
+ const positions = await tc.positions();
166
528
 
167
529
  // 查询资产
168
- const assets = await tc.getAssets();
530
+ const assets = await tc.assets();
169
531
  ```
170
532
 
171
- ## 通用方法(execute)
533
+ ## 通用执行
172
534
 
173
- 当 SDK 尚未封装某个 API 时,可以使用 `HttpClient.execute` 直接调用:
535
+ 对于 SDK 尚未封装的 API,可直接使用 `HttpClient.execute`:
174
536
 
175
537
  ```typescript
176
538
  const httpClient = new HttpClient(config);
177
539
 
178
- // 直接传入 API 方法名和 JSON 参数
179
540
  const resp = await httpClient.execute('market_state', JSON.stringify({ market: 'US' }));
180
541
  console.log('原始响应:', resp);
181
542
  ```
182
543
 
183
544
  ## 实时推送
184
545
 
185
- 通过 WebSocket 长连接接收实时行情和账户推送,支持自动重连和心跳保活:
546
+ 推送客户端通过 **TCP + TLS** 连接到老虎推送服务器,使用 varint32 长度前缀 + **Protobuf** 二进制帧格式。支持自动重连和心跳保活。
547
+
548
+ 回调数据类型为 Protobuf 生成的类型(如 `QuoteData`、`OrderStatusData`、`AssetData`、`PositionData`)。
186
549
 
187
550
  ```typescript
188
551
  import { PushClient } from '@tigeropenapi/tigeropen/push/push-client';
552
+ import type { QuoteData } from '@tigeropenapi/tigeropen/push/pb/QuoteData';
553
+ import type { OrderStatusData } from '@tigeropenapi/tigeropen/push/pb/OrderStatusData';
554
+ import type { AssetData } from '@tigeropenapi/tigeropen/push/pb/AssetData';
555
+ import type { PositionData } from '@tigeropenapi/tigeropen/push/pb/PositionData';
189
556
 
190
557
  const pc = new PushClient(config);
191
558
 
192
- // 设置回调
193
559
  pc.setCallbacks({
194
- onQuote: (data) => {
195
- console.log(`行情推送: ${data.symbol} 最新价: ${data.latestPrice}`);
196
- },
197
- onOrder: (data) => {
198
- console.log(`订单推送: ${data.symbol} 状态: ${data.status}`);
560
+ onQuote: (data: QuoteData) => {
561
+ console.log(`行情: ${data.symbol} 价格=${data.latestPrice}`);
199
562
  },
200
- onAsset: (data) => {
201
- console.log('资产变动推送');
202
- },
203
- onPosition: (data) => {
204
- console.log('持仓变动推送');
205
- },
206
- onConnect: () => {
207
- console.log('推送连接成功');
208
- },
209
- onDisconnect: () => {
210
- console.log('推送连接断开');
211
- },
212
- onError: (err) => {
213
- console.error('推送错误:', err);
563
+ onOrder: (data: OrderStatusData) => {
564
+ console.log(`订单: ${data.symbol} 状态=${data.status}`);
214
565
  },
566
+ onAsset: (data: AssetData) => console.log('资产变动'),
567
+ onPosition: (data: PositionData) => console.log('持仓变动'),
568
+ onConnect: () => console.log('已连接'),
569
+ onDisconnect: () => console.log('已断开'),
570
+ onError: (err) => console.error('错误:', err),
215
571
  });
216
572
 
217
- // 连接
573
+ // 连接(通过 TLS 认证)
218
574
  await pc.connect();
219
575
 
220
- // 订阅行情
576
+ // 订阅实时行情
221
577
  pc.subscribeQuote(['AAPL', 'TSLA']);
222
578
 
223
- // 订阅账户推送
579
+ // 订阅账户事件
224
580
  pc.subscribeAsset();
225
581
  pc.subscribeOrder();
226
582
  pc.subscribePosition();
227
583
 
228
- // 退订
584
+ // 取消订阅
229
585
  pc.unsubscribeQuote(['TSLA']);
230
586
 
231
587
  // 断开连接
232
588
  pc.disconnect();
233
589
  ```
234
590
 
591
+ ### 可用订阅
592
+
593
+ | 方法 | 数据类型 | 回调 |
594
+ |------|----------|------|
595
+ | `subscribeQuote(symbols)` | 股票行情 | `onQuote(QuoteData)` |
596
+ | `subscribeTick(symbols)` | 逐笔成交 | `onTick(TradeTickData)` |
597
+ | `subscribeDepth(symbols)` | 盘口深度 | `onDepth(QuoteDepthData)` |
598
+ | `subscribeOption(symbols)` | 期权行情 | `onOption(QuoteData)` |
599
+ | `subscribeFuture(symbols)` | 期货行情 | `onFuture(QuoteData)` |
600
+ | `subscribeKline(symbols)` | K 线数据 | `onKline(KlineData)` |
601
+ | `subscribeAsset(account?)` | 资产变动 | `onAsset(AssetData)` |
602
+ | `subscribePosition(account?)` | 持仓变动 | `onPosition(PositionData)` |
603
+ | `subscribeOrder(account?)` | 订单状态 | `onOrder(OrderStatusData)` |
604
+ | `subscribeTransaction(account?)` | 成交明细 | `onTransaction(OrderTransactionData)` |
605
+
235
606
  ## 项目结构
236
607
 
237
608
  ```
238
609
  openapi-typescript-sdk/
239
610
  ├── src/
240
- │ ├── config/ # 配置管理(ClientConfig、ConfigParser、动态域名)
241
- │ ├── signer/ # RSA 签名
242
- │ ├── client/ # HTTP 客户端(请求/响应、重试策略、execute)
243
- │ ├── model/ # 数据模型(Order、Contract、Position、枚举)
244
- │ ├── quote/ # 行情查询客户端
245
- │ ├── trade/ # 交易客户端
246
- │ ├── push/ # WebSocket 实时推送客户端
247
- │ ├── logger/ # 日志模块
248
- │ └── index.ts # 统一导出
249
- ├── examples/ # 示例代码
250
- └── tests/ # 测试
611
+ │ ├── config/ # 配置管理
612
+ │ ├── signer/ # RSA 请求签名
613
+ │ ├── client/ # HTTP 客户端
614
+ │ ├── model/ # 数据模型和订单辅助函数
615
+ │ ├── quote/ # 行情客户端
616
+ │ ├── trade/ # 交易客户端(订单、持仓、资产)
617
+ │ ├── push/ # TCP+TLS 推送客户端(Protobuf)
618
+ │ ├── logger/ # 日志
619
+ │ └── index.ts # 公共导出
620
+ ├── proto/ # Protobuf 协议定义
621
+ ├── examples/ # 使用示例
622
+ └── tests/ # 单元测试
251
623
  ```
252
624
 
253
625
  ## ESM 和 CommonJS
254
626
 
255
- 本 SDK 同时支持 ESM 和 CommonJS:
256
-
257
627
  ```typescript
258
628
  // ESM
259
629
  import { createClientConfig } from '@tigeropenapi/tigeropen';
@@ -262,10 +632,11 @@ import { createClientConfig } from '@tigeropenapi/tigeropen';
262
632
  const { createClientConfig } = require('@tigeropenapi/tigeropen');
263
633
  ```
264
634
 
265
- ## API 参考
635
+ ## 参考链接
266
636
 
637
+ - [TypeScript SDK 文档](https://docs.itigerup.com/docs/typescript)
267
638
  - [老虎证券 OpenAPI 文档](https://quant.itigerup.com/openapi/zh/python/overview/introduction.html)
268
- - [npm 包主页](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
639
+ - [npm 包](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
269
640
 
270
641
  ## 许可证
271
642