Tiger OpenAPI Rust SDK
Rust SDK for Tiger Brokers OpenAPI. Provides market data queries, order execution, account management, and real-time push notifications.
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
= { = "1", = ["full"] }
Requires Rust 1.70 or later.
Quick Start
use ClientConfig;
use HttpClient;
use QuoteClient;
async
Configuration
The SDK supports multiple configuration methods. Priority: environment variables > builder setters (incl. properties file) > auto-discovered config file > defaults.
Method 1: Properties File
The most common approach. Create a tiger_openapi_config.properties file:
Load it explicitly:
let config = builder
.properties_file
.build?;
Method 2: Auto-Discovery
If you call build() without setting tiger_id or private_key, the builder automatically searches for a config file in this order:
./tiger_openapi_config.properties(current directory)~/.tigeropen/tiger_openapi_config.properties(home directory)
This means you can simply do:
let config = builder.build?; // auto-discovers config
Place your config file at ~/.tigeropen/tiger_openapi_config.properties and it will be picked up automatically across all your projects.
Method 3: Builder
Set values programmatically:
let config = builder
.tiger_id
.private_key
.account
.build?;
Method 4: Environment Variables
Environment variables have the highest priority and override all other methods.
Configuration Reference
| Field | Description | Required | Default |
|---|---|---|---|
| tiger_id | Developer ID | Yes | - |
| private_key | RSA private key | Yes | - |
| account | Trading account | No | - |
| language | Language (ZhCn/EnUs) | No | ZhCn |
| timeout | Request timeout | No | 15s |
Market Data
use HttpClient;
use QuoteClient;
let http = new;
let qc = new;
// Market state
let states = qc.market_state.await?;
// Real-time quotes
let quotes = qc.quote_real_time.await?;
// K-line data
let klines = qc.kline.await?;
// Timeline
let timeline = qc.timeline.await?;
// Depth quotes
let depth = qc.quote_depth.await?;
// Option expiration dates
let expiry = qc.option_expiration.await?;
// Option chain
let chain = qc.option_chain.await?;
// Futures exchange list
let exchanges = qc.future_exchange.await?;
Trading
use HttpClient;
use TradeClient;
use json;
let http = new;
let tc = new;
// Place a limit order
let order = json!;
let result = tc.place_order.await?;
// Preview order (no actual execution)
let preview = tc.preview_order.await?;
// Modify order
tc.modify_order.await?;
// Cancel order
tc.cancel_order.await?;
// Query orders, positions, assets
let orders = tc.orders.await?;
let positions = tc.positions.await?;
let assets = tc.assets.await?;
Generic API Call (execute)
When the SDK hasn't wrapped a specific API yet, use HttpClient::execute directly:
let http = new;
let resp = http.execute.await?;
println!;
Real-Time Push
The push client uses a TCP + TLS + Protobuf persistent connection for real-time market data and account notifications. It supports automatic reconnection and heartbeat keep-alive.
Callback parameters use Protobuf-generated types (pb::QuoteData, pb::OrderStatusData, pb::AssetData, etc.).
use Arc;
use ClientConfig;
use *;
async
Project Structure
openapi-rust-sdk/
├── src/
│ ├── config/ # Configuration (ClientConfig builder, config parser, dynamic domain)
│ ├── signer/ # RSA signing
│ ├── client/ # HTTP client (request/response, retry, execute)
│ ├── model/ # Data models (Order, Contract, Position, enums)
│ ├── quote/ # Market data query client
│ ├── trade/ # Trading client
│ ├── push/ # TCP+TLS push client (Protobuf binary protocol)
│ ├── error.rs # Error types
│ ├── logger.rs # Logging
│ └── lib.rs # Public exports
├── examples/ # Example code
└── tests/ # Tests
API Reference
License
Tiger OpenAPI Rust SDK(中文)
老虎证券 OpenAPI 的 Rust SDK,提供行情查询、交易下单、账户管理和实时推送等功能。
安装
在 Cargo.toml 中添加依赖:
[]
= "0.1.0"
= { = "1", = ["full"] }
要求 Rust 1.70 或更高版本。
快速开始
use ClientConfig;
use HttpClient;
use QuoteClient;
async
配置
SDK 支持多种配置方式,优先级:环境变量 > Builder 设置(含配置文件) > 自动发现的配置文件 > 默认值。
方式一:Properties 配置文件
最常用的方式。创建 tiger_openapi_config.properties 文件:
显式加载:
let config = builder
.properties_file
.build?;
方式二:自动发现
如果调用 build() 时未设置 tiger_id 或 private_key,Builder 会按以下顺序自动搜索配置文件:
./tiger_openapi_config.properties(当前目录)~/.tigeropen/tiger_openapi_config.properties(用户主目录)
因此你可以直接:
let config = builder.build?; // 自动发现配置
将配置文件放在 ~/.tigeropen/tiger_openapi_config.properties,所有项目都能自动加载。
方式三:Builder 模式
通过代码设置:
let config = builder
.tiger_id
.private_key
.account
.build?;
方式四:环境变量
环境变量优先级最高,会覆盖所有其他配置方式。
配置项说明
| 配置项 | 说明 | 必填 | 默认值 |
|---|---|---|---|
| tiger_id | 开发者 ID | 是 | - |
| private_key | RSA 私钥 | 是 | - |
| account | 交易账户 | 否 | - |
| language | 语言(ZhCn/EnUs) | 否 | ZhCn |
| timeout | 请求超时 | 否 | 15s |
行情查询
use HttpClient;
use QuoteClient;
let http = new;
let qc = new;
// 市场状态
let states = qc.market_state.await?;
// 实时报价
let quotes = qc.quote_real_time.await?;
// K 线数据
let klines = qc.kline.await?;
// 分时数据
let timeline = qc.timeline.await?;
// 深度行情
let depth = qc.quote_depth.await?;
// 期权到期日
let expiry = qc.option_expiration.await?;
// 期权链
let chain = qc.option_chain.await?;
// 期货交易所列表
let exchanges = qc.future_exchange.await?;
交易操作
use HttpClient;
use TradeClient;
use json;
let http = new;
let tc = new;
// 下限价单
let order = json!;
let result = tc.place_order.await?;
// 预览订单(不实际下单)
let preview = tc.preview_order.await?;
// 修改订单
tc.modify_order.await?;
// 取消订单
tc.cancel_order.await?;
// 查询订单、持仓、资产
let orders = tc.orders.await?;
let positions = tc.positions.await?;
let assets = tc.assets.await?;
通用方法(execute)
当 SDK 尚未封装某个 API 时,可以使用 HttpClient::execute 直接调用:
let http = new;
let resp = http.execute.await?;
println!;
实时推送
推送客户端使用 TCP + TLS + Protobuf 长连接接收实时行情和账户推送通知,支持自动重连和心跳保活。
回调参数使用 Protobuf 生成的类型(pb::QuoteData、pb::OrderStatusData、pb::AssetData 等)。
use Arc;
use ClientConfig;
use *;
async
项目结构
openapi-rust-sdk/
├── src/
│ ├── config/ # 配置管理(ClientConfig Builder、ConfigParser、动态域名)
│ ├── signer/ # RSA 签名
│ ├── client/ # HTTP 客户端(请求/响应、重试策略、execute)
│ ├── model/ # 数据模型(Order、Contract、Position、枚举)
│ ├── quote/ # 行情查询客户端
│ ├── trade/ # 交易客户端
│ ├── push/ # TCP+TLS 推送客户端(Protobuf 二进制协议)
│ ├── error.rs # 错误类型
│ ├── logger.rs # 日志模块
│ └── lib.rs # 统一导出
├── examples/ # 示例代码
└── tests/ # 测试