@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.
- package/README.md +475 -104
- package/dist/cjs/client/api-request.js +16 -10
- package/dist/cjs/client/api-response.js +23 -3
- package/dist/cjs/client/case-convert.js +35 -0
- package/dist/cjs/client/http-client.js +46 -20
- package/dist/cjs/client/index.js +6 -2
- package/dist/cjs/config/client-config.js +130 -49
- package/dist/cjs/config/domain.js +21 -2
- package/dist/cjs/config/index.js +3 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/model/contract.js +1 -2
- package/dist/cjs/model/order-helpers.js +9 -9
- package/dist/cjs/model/order.js +5 -2
- package/dist/cjs/model/position.js +1 -2
- package/dist/cjs/model/quote.js +7 -0
- package/dist/cjs/model/trade.js +2 -0
- package/dist/cjs/push/index.js +6 -3
- package/dist/cjs/push/pb/AssetData.js +208 -0
- package/dist/cjs/push/pb/KlineData.js +184 -0
- package/dist/cjs/push/pb/OptionTopData.js +427 -0
- package/dist/cjs/push/pb/OrderStatusData.js +568 -0
- package/dist/cjs/push/pb/OrderTransactionData.js +256 -0
- package/dist/cjs/push/pb/PositionData.js +316 -0
- package/dist/cjs/push/pb/PushData.js +219 -0
- package/dist/cjs/push/pb/QuoteBBOData.js +160 -0
- package/dist/cjs/push/pb/QuoteBasicData.js +329 -0
- package/dist/cjs/push/pb/QuoteData.js +507 -0
- package/dist/cjs/push/pb/QuoteDepthData.js +220 -0
- package/dist/cjs/push/pb/Request.js +278 -0
- package/dist/cjs/push/pb/Response.js +97 -0
- package/dist/cjs/push/pb/SocketCommon.js +86 -0
- package/dist/cjs/push/pb/StockTopData.js +199 -0
- package/dist/cjs/push/pb/TickData.js +202 -0
- package/dist/cjs/push/pb/TradeTickData.js +308 -0
- package/dist/cjs/push/pb/index.js +49 -0
- package/dist/cjs/push/proto-message.js +129 -0
- package/dist/cjs/push/push-client.js +292 -164
- package/dist/cjs/push/push-message.js +4 -60
- package/dist/cjs/push/varint.js +76 -0
- package/dist/cjs/quote/index.js +3 -2
- package/dist/cjs/quote/quote-client.js +128 -94
- package/dist/cjs/signer/signer.js +10 -10
- package/dist/cjs/trade/trade-client.js +56 -62
- package/dist/esm/client/api-request.js +14 -8
- package/dist/esm/client/api-response.js +22 -3
- package/dist/esm/client/case-convert.js +31 -0
- package/dist/esm/client/http-client.js +47 -21
- package/dist/esm/client/index.js +3 -2
- package/dist/esm/config/client-config.js +131 -51
- package/dist/esm/config/domain.js +20 -2
- package/dist/esm/config/index.js +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/model/contract.js +1 -2
- package/dist/esm/model/order-helpers.js +9 -9
- package/dist/esm/model/order.js +5 -2
- package/dist/esm/model/position.js +1 -2
- package/dist/esm/model/quote.js +6 -0
- package/dist/esm/model/trade.js +1 -0
- package/dist/esm/push/index.js +4 -2
- package/dist/esm/push/pb/AssetData.js +205 -0
- package/dist/esm/push/pb/KlineData.js +181 -0
- package/dist/esm/push/pb/OptionTopData.js +424 -0
- package/dist/esm/push/pb/OrderStatusData.js +565 -0
- package/dist/esm/push/pb/OrderTransactionData.js +253 -0
- package/dist/esm/push/pb/PositionData.js +313 -0
- package/dist/esm/push/pb/PushData.js +216 -0
- package/dist/esm/push/pb/QuoteBBOData.js +157 -0
- package/dist/esm/push/pb/QuoteBasicData.js +326 -0
- package/dist/esm/push/pb/QuoteData.js +504 -0
- package/dist/esm/push/pb/QuoteDepthData.js +217 -0
- package/dist/esm/push/pb/Request.js +275 -0
- package/dist/esm/push/pb/Response.js +94 -0
- package/dist/esm/push/pb/SocketCommon.js +83 -0
- package/dist/esm/push/pb/StockTopData.js +196 -0
- package/dist/esm/push/pb/TickData.js +199 -0
- package/dist/esm/push/pb/TradeTickData.js +305 -0
- package/dist/esm/push/pb/index.js +25 -0
- package/dist/esm/push/proto-message.js +121 -0
- package/dist/esm/push/push-client.js +260 -165
- package/dist/esm/push/push-message.js +3 -56
- package/dist/esm/push/varint.js +72 -0
- package/dist/esm/quote/index.js +2 -2
- package/dist/esm/quote/quote-client.js +127 -94
- package/dist/esm/signer/signer.js +10 -10
- package/dist/esm/trade/trade-client.js +56 -62
- package/dist/types/client/api-request.d.ts +12 -11
- package/dist/types/client/api-response.d.ts +15 -7
- package/dist/types/client/case-convert.d.ts +16 -0
- package/dist/types/client/http-client.d.ts +26 -14
- package/dist/types/client/index.d.ts +3 -2
- package/dist/types/config/client-config.d.ts +29 -13
- package/dist/types/config/domain.d.ts +9 -0
- package/dist/types/config/index.d.ts +2 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/model/contract.d.ts +13 -29
- package/dist/types/model/order-helpers.d.ts +21 -20
- package/dist/types/model/order.d.ts +101 -55
- package/dist/types/model/position.d.ts +26 -24
- package/dist/types/model/quote.d.ts +311 -0
- package/dist/types/model/trade.d.ts +113 -0
- package/dist/types/push/callbacks.d.ts +32 -51
- package/dist/types/push/index.d.ts +5 -3
- package/dist/types/push/pb/AssetData.d.ts +47 -0
- package/dist/types/push/pb/KlineData.d.ts +43 -0
- package/dist/types/push/pb/OptionTopData.d.ts +76 -0
- package/dist/types/push/pb/OrderStatusData.d.ts +96 -0
- package/dist/types/push/pb/OrderTransactionData.d.ts +51 -0
- package/dist/types/push/pb/PositionData.d.ts +63 -0
- package/dist/types/push/pb/PushData.d.ts +46 -0
- package/dist/types/push/pb/QuoteBBOData.d.ts +35 -0
- package/dist/types/push/pb/QuoteBasicData.d.ts +66 -0
- package/dist/types/push/pb/QuoteData.d.ts +94 -0
- package/dist/types/push/pb/QuoteDepthData.d.ts +37 -0
- package/dist/types/push/pb/Request.d.ts +44 -0
- package/dist/types/push/pb/Response.d.ts +30 -0
- package/dist/types/push/pb/SocketCommon.d.ts +64 -0
- package/dist/types/push/pb/StockTopData.d.ts +37 -0
- package/dist/types/push/pb/TickData.d.ts +44 -0
- package/dist/types/push/pb/TradeTickData.d.ts +53 -0
- package/dist/types/push/pb/index.d.ts +39 -0
- package/dist/types/push/proto-message.d.ts +33 -0
- package/dist/types/push/push-client.d.ts +69 -68
- package/dist/types/push/push-message.d.ts +3 -147
- package/dist/types/push/varint.d.ts +23 -0
- package/dist/types/quote/index.d.ts +2 -2
- package/dist/types/quote/quote-client.d.ts +41 -47
- package/dist/types/signer/signer.d.ts +6 -6
- package/dist/types/trade/trade-client.d.ts +34 -38
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -1,70 +1,390 @@
|
|
|
1
1
|
# Tiger OpenAPI TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript SDK for Tiger Brokers OpenAPI — market data, trading, account management, and real-time push notifications.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
|
|
6
6
|
[](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
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
35
|
-
const httpClient = new HttpClient(config);
|
|
64
|
+
File format:
|
|
36
65
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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: '
|
|
54
|
-
privateKey: '
|
|
55
|
-
account: '
|
|
96
|
+
tigerId: 'your_tiger_id',
|
|
97
|
+
privateKey: 'your_rsa_private_key',
|
|
98
|
+
account: 'your_account',
|
|
56
99
|
});
|
|
57
100
|
```
|
|
58
101
|
|
|
59
|
-
###
|
|
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
|
+
[](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
|
|
329
|
+
[](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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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.
|
|
466
|
+
// 市场状态
|
|
467
|
+
const states = await qc.marketState('US');
|
|
105
468
|
|
|
106
|
-
//
|
|
107
|
-
const
|
|
469
|
+
// 实时报价
|
|
470
|
+
const quotes = await qc.quoteRealTime(['AAPL', 'TSLA']);
|
|
108
471
|
|
|
109
|
-
//
|
|
110
|
-
const klines = await qc.
|
|
472
|
+
// K 线数据
|
|
473
|
+
const klines = await qc.kline('AAPL', 'day');
|
|
111
474
|
|
|
112
|
-
//
|
|
113
|
-
const timeline = await qc.
|
|
475
|
+
// 分时数据
|
|
476
|
+
const timeline = await qc.timeline(['AAPL']);
|
|
114
477
|
|
|
115
|
-
//
|
|
116
|
-
const depth = await qc.
|
|
478
|
+
// 盘口深度
|
|
479
|
+
const depth = await qc.quoteDepth('AAPL');
|
|
117
480
|
|
|
118
|
-
//
|
|
119
|
-
const expiry = await qc.
|
|
481
|
+
// 期权到期日
|
|
482
|
+
const expiry = await qc.optionExpiration('AAPL');
|
|
120
483
|
|
|
121
|
-
//
|
|
122
|
-
const chain = await qc.
|
|
484
|
+
// 期权链
|
|
485
|
+
const chain = await qc.optionChain('AAPL', '2024-01-19');
|
|
123
486
|
|
|
124
|
-
//
|
|
125
|
-
const exchanges = await qc.
|
|
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
|
-
|
|
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.
|
|
523
|
+
// 查询订单
|
|
524
|
+
const orders = await tc.orders();
|
|
163
525
|
|
|
164
526
|
// 查询持仓
|
|
165
|
-
const positions = await tc.
|
|
527
|
+
const positions = await tc.positions();
|
|
166
528
|
|
|
167
529
|
// 查询资产
|
|
168
|
-
const assets = await tc.
|
|
530
|
+
const assets = await tc.assets();
|
|
169
531
|
```
|
|
170
532
|
|
|
171
|
-
##
|
|
533
|
+
## 通用执行
|
|
172
534
|
|
|
173
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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/ #
|
|
241
|
-
│ ├── signer/ # RSA
|
|
242
|
-
│ ├── client/ # HTTP
|
|
243
|
-
│ ├── model/ #
|
|
244
|
-
│ ├── quote/ #
|
|
245
|
-
│ ├── trade/ #
|
|
246
|
-
│ ├── push/ #
|
|
247
|
-
│ ├── logger/ #
|
|
248
|
-
│ └── index.ts #
|
|
249
|
-
├──
|
|
250
|
-
|
|
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
|
-
##
|
|
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
|
|
639
|
+
- [npm 包](https://www.npmjs.com/package/@tigeropenapi/tigeropen)
|
|
269
640
|
|
|
270
641
|
## 许可证
|
|
271
642
|
|