### Quick Start: Fetch Market State Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Initialize the SDK, create an HTTP client, and fetch market states for a given region. Auto-discovers configuration from default locations. ```typescript import { createClientConfig } from '@tigeropenapi/tigeropen'; import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client'; import { QuoteClient } from '@tigeropenapi/tigeropen/quote/quote-client'; // Auto-discovers ~/.tigeropen/tiger_openapi_config.properties const config = createClientConfig(); const httpClient = new HttpClient(config); const qc = new QuoteClient(httpClient); const states = await qc.marketState('US'); console.log('Market state:', states); ``` -------------------------------- ### Install Tiger OpenAPI TypeScript SDK Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Install the SDK using npm, yarn, or pnpm. Requires Node.js version 16.0.0 or higher. ```bash npm install @tigeropenapi/tigeropen # or yarn add @tigeropenapi/tigeropen # or pnpm add @tigeropenapi/tigeropen ``` -------------------------------- ### Place and Manage Orders with TradeClient Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Illustrates how to use the TradeClient to build, place, preview, modify, and cancel orders. It also shows how to query existing orders, positions, and account assets. Requires TradeClient initialization with an account. ```typescript import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client'; import { TradeClient } from '@tigeropenapi/tigeropen/trade/trade-client'; const httpClient = new HttpClient(config); const tc = new TradeClient(httpClient, config.account); // Build a limit order const order = { symbol: 'AAPL', secType: 'STK', action: 'BUY', orderType: 'LMT', totalQuantity: 100, limitPrice: 150.0, timeInForce: 'DAY', }; // Place order const result = await tc.placeOrder(order); // Preview order (no actual submission) const preview = await tc.previewOrder(order); // Modify order await tc.modifyOrder(orderId, { ...order, limitPrice: 155.0 }); // Cancel order await tc.cancelOrder(orderId); // Query orders const orders = await tc.orders(); // Query positions const positions = await tc.positions(); // Query assets const assets = await tc.assets(); ``` -------------------------------- ### Configuration: Auto-discovery Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Initialize the SDK configuration by allowing it to auto-discover the `tiger_openapi_config.properties` file from well-known locations. ```typescript import { createClientConfig } from '@tigeropenapi/tigeropen'; // No arguments — auto-discovers config from well-known locations const config = createClientConfig(); ``` -------------------------------- ### Configuration: Code Options Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Configure the SDK by passing configuration values directly as options when creating the client config. This is useful for dynamic configuration. ```typescript import { createClientConfig } from '@tigeropenapi/tigeropen'; const config = createClientConfig({ tigerId: 'your_tiger_id', privateKey: 'your_rsa_private_key', account: 'your_account', }); ``` -------------------------------- ### Configuration: Environment Variables Usage Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md After setting environment variables, create the client configuration by calling `createClientConfig()` without arguments. Environment variables have the highest priority. ```typescript const config = createClientConfig(); ``` -------------------------------- ### Fetch Market Data with QuoteClient Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Demonstrates how to use the QuoteClient to retrieve various market data points including market status, real-time quotes, k-line data, order book depth, and option chain information. Ensure HttpClient and QuoteClient are initialized. ```typescript import { HttpClient } from '@tigeropenapi/tigeropen/client/http-client'; import { QuoteClient } from '@tigeropenapi/tigeropen/quote/quote-client'; const httpClient = new HttpClient(config); const qc = new QuoteClient(httpClient); // Market status const states = await qc.marketState('US'); // Real-time quotes const quotes = await qc.quoteRealTime(['AAPL', 'TSLA']); // K-line data const klines = await qc.kline('AAPL', 'day'); // Intraday timeline const timeline = await qc.timeline(['AAPL']); // Order book depth const depth = await qc.quoteDepth('AAPL'); // Option expirations const expiry = await qc.optionExpiration('AAPL'); // Option chain const chain = await qc.optionChain('AAPL', '2024-01-19'); // Futures exchanges const exchanges = await qc.futureExchange(); ``` -------------------------------- ### Configuration: Environment Variables Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Set environment variables for configuration parameters like `TIGEROPEN_TIGER_ID`, `TIGEROPEN_PRIVATE_KEY`, and `TIGEROPEN_ACCOUNT`. The SDK will automatically pick these up when `createClientConfig()` is called without arguments. ```bash export TIGEROPEN_TIGER_ID=your_developer_id export TIGEROPEN_PRIVATE_KEY=your_rsa_private_key export TIGEROPEN_ACCOUNT=your_trading_account ``` -------------------------------- ### Configuration: Explicit Properties File Path Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Configure the SDK by providing an explicit path to the properties file. This method allows for precise control over the configuration source. ```typescript import { createClientConfig } from '@tigeropenapi/tigeropen'; const config = createClientConfig({ propertiesFilePath: '/path/to/tiger_openapi_config.properties', }); ``` -------------------------------- ### Real-Time Data Subscription with PushClient Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Connect to the push server for real-time data like quotes, order status, assets, and positions. This client handles automatic reconnection and heartbeats. Ensure necessary Protobuf types are imported. ```typescript import { PushClient } from '@tigeropenapi/tigeropen/push/push-client'; import type { QuoteData } from '@tigeropenapi/tigeropen/push/pb/QuoteData'; import type { OrderStatusData } from '@tigeropenapi/tigeropen/push/pb/OrderStatusData'; import type { AssetData } from '@tigeropenapi/tigeropen/push/pb/AssetData'; import type { PositionData } from '@tigeropenapi/tigeropen/push/pb/PositionData'; const pc = new PushClient(config); pc.setCallbacks({ onQuote: (data: QuoteData) => { console.log(`Quote: ${data.symbol} price=${data.latestPrice}`); }, onOrder: (data: OrderStatusData) => { console.log(`Order: ${data.symbol} status=${data.status}`); }, onAsset: (data: AssetData) => console.log('Asset update'), onPosition: (data: PositionData) => console.log('Position update'), onConnect: () => console.log('Connected'), onDisconnect: () => console.log('Disconnected'), onError: (err) => console.error('Error:', err), }); // Connect (authenticates over TLS) await pc.connect(); // Subscribe to real-time quotes pc.subscribeQuote(['AAPL', 'TSLA']); // Subscribe to account events pc.subscribeAsset(); pc.subscribeOrder(); pc.subscribePosition(); // Unsubscribe pc.unsubscribeQuote(['TSLA']); // Disconnect pc.disconnect(); ``` -------------------------------- ### Real-Time Push Client Subscriptions Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md The `PushClient` enables real-time data subscriptions over a TCP + TLS connection using Protobuf framing. It supports automatic reconnection, heartbeats, and various data types including quotes, order status, assets, and positions. ```APIDOC ## Real-Time Push Client ### Description 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. Callback data types are Protobuf-generated types (e.g. `QuoteData`, `OrderStatusData`, `AssetData`, `PositionData`). ### Client Initialization ```typescript import { PushClient } from '@tigeropenapi/tigeropen/push/push-client'; const pc = new PushClient(config); ``` ### Callbacks Set callbacks for different data events: ```typescript pc.setCallbacks({ onQuote: (data: QuoteData) => { console.log(`Quote: ${data.symbol} price=${data.latestPrice}`); }, onOrder: (data: OrderStatusData) => { console.log(`Order: ${data.symbol} status=${data.status}`); }, onAsset: (data: AssetData) => console.log('Asset update'), onPosition: (data: PositionData) => console.log('Position update'), onConnect: () => console.log('Connected'), onDisconnect: () => console.log('Disconnected'), onError: (err) => console.error('Error:', err), }); ``` ### Connection Management * **connect()**: Establishes a connection to the push server. ```typescript await pc.connect(); ``` * **disconnect()**: Closes the connection to the push server. ```typescript pc.disconnect(); ``` ### Subscriptions #### Available Subscriptions | Method | Data type | Callback Signature | |---|---|---| | `subscribeQuote(symbols)` | Stock quotes | `onQuote(QuoteData)` | | `subscribeTick(symbols)` | Trade ticks | `onTick(TradeTickData)` | | `subscribeDepth(symbols)` | Order book depth | `onDepth(QuoteDepthData)` | | `subscribeOption(symbols)` | Option quotes | `onOption(QuoteData)` | | `subscribeFuture(symbols)` | Futures quotes | `onFuture(QuoteData)` | | `subscribeKline(symbols)` | K-line data | `onKline(KlineData)` | | `subscribeAsset(account?)` | Asset changes | `onAsset(AssetData)` | | `subscribePosition(account?)` | Position changes | `onPosition(PositionData)` | | `subscribeOrder(account?)` | Order status | `onOrder(OrderStatusData)` | | `subscribeTransaction(account?)` | Transactions | `onTransaction(OrderTransactionData)` | #### Example Subscriptions ```typescript // Subscribe to real-time quotes for AAPL and TSLA pc.subscribeQuote(['AAPL', 'TSLA']); // Subscribe to account events pc.subscribeAsset(); pc.subscribeOrder(); pc.subscribePosition(); ``` ### Unsubscriptions * **unsubscribeQuote(symbols)**: Unsubscribes from quote data for specified symbols. ```typescript pc.unsubscribeQuote(['TSLA']); ``` ``` -------------------------------- ### Properties File Format Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Defines the structure for the `tiger_openapi_config.properties` file, including developer ID, private key, and trading account. ```properties tiger_id=your_developer_id private_key=your_rsa_private_key account=your_trading_account ``` -------------------------------- ### Execute Generic API Request Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Use HttpClient.execute for APIs not yet directly supported by the SDK. Ensure you have initialized the HttpClient with configuration. ```typescript const httpClient = new HttpClient(config); const resp = await httpClient.execute('market_state', JSON.stringify({ market: 'US' })); console.log('Raw response:', resp); ``` -------------------------------- ### Generic Execute Source: https://github.com/tigerfintech/openapi-typescript-sdk/blob/main/README.md Use `HttpClient.execute` for APIs not yet integrated into the SDK. This method allows direct execution of API calls by providing the API name and a JSON string payload. ```APIDOC ## Generic Execute ### Description For APIs not yet wrapped by the SDK, use `HttpClient.execute` directly. This method allows for executing arbitrary API calls by specifying the API name and a JSON string payload. ### Method Signature `httpClient.execute(apiName: string, payload: string): Promise` ### Parameters * **apiName** (string) - Required - The name of the API endpoint to execute. * **payload** (string) - Required - A JSON string representing the request payload for the API. ### Request Example ```typescript const httpClient = new HttpClient(config); const resp = await httpClient.execute('market_state', JSON.stringify({ market: 'US' })); console.log('Raw response:', resp); ``` ### Response * **resp** (any) - The raw response from the executed API call. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.