交易操作
Rust SDK 通过 TradeClient 提供全部交易接口,所有方法均为异步(async),返回 Result<Option<Value>, TigerError>。以下为完整可运行示例:
use tigeropen::config::ClientConfig;
use tigeropen::trade::TradeClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = ClientConfig::from_properties_file("tiger_openapi_config.properties")?;
let tc = TradeClient::new(config.clone());
// 查询持仓
let positions = tc.positions().await?;
println!("持仓: {:?}", positions);
// 预览限价买单
let order = serde_json::json!({
"account": config.account,
"symbol": "AAPL",
"sec_type": "STK",
"action": "BUY",
"order_type": "LMT",
"total_quantity": 10,
"limit_price": 150.0,
});
let preview = tc.preview_order(order.clone()).await?;
println!("预览结果: {:?}", preview);
// 实际下单
let result = tc.place_order(order).await?;
println!("下单结果: {:?}", result);
Ok(())
}合约查询
contract 查询单个合约
tc.contract(symbol: &str, sec_type: &str) -> Result<Option<Value>, TigerError>
说明
查询单个合约的详细信息,包括名称、货币、最小交易单位等。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| symbol | &str | 是 | 合约代码,如 "AAPL"、"00700" |
| sec_type | &str | 是 | 合约类型:"STK"(股票)/ "OPT"(期权)/ "FUT"(期货)/ "WAR"(权证)/ "IOPT"(牛熊证) |
返回
Result<Option<Value>, TigerError>
返回合约详情,包含 symbol、name、currency、exchange、lotSize 等字段。
示例
let contract = tc.contract("AAPL", "STK").await?;
println!("{:?}", contract);
// 查询港股
let hk_contract = tc.contract("00700", "STK").await?;contracts 批量查询合约
tc.contracts(symbols: &[&str], sec_type: &str) -> Result<Option<Value>, TigerError>
说明
批量查询多个合约信息,减少网络请求次数。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| symbols | &[&str] | 是 | 合约代码切片 |
| sec_type | &str | 是 | 合约类型,同 contract |
返回
Result<Option<Value>, TigerError>
返回合约信息数组,结构同 contract。
示例
let contracts = tc.contracts(&["AAPL", "TSLA", "MSFT"], "STK").await?;
println!("{:?}", contracts);quote_contract 查询衍生品合约
tc.quote_contract(symbol: &str, sec_type: &str) -> Result<Option<Value>, TigerError>
说明
查询期权或期货等衍生品的合约明细,包含行权价、到期日、合约乘数等衍生品特有字段。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| symbol | &str | 是 | 衍生品代码,期权格式:"AAPL 250117C00150000" |
| sec_type | &str | 是 | 合约类型,通常为 "OPT" 或 "FUT" |
返回
Result<Option<Value>, TigerError>
返回衍生品合约信息,包含 strike、expiry、right(PUT/CALL)、multiplier 等字段。
示例
let quote_contract = tc.quote_contract("AAPL 250117C00150000", "OPT").await?;
println!("{:?}", quote_contract);订单操作
订单 JSON 对象说明
Rust SDK 使用 serde_json::Value 构造订单对象,关键字段如下:
| 字段 | 类型 | 描述 |
|---|---|---|
| account | String | 账户 ID |
| symbol | String | 合约代码 |
| sec_type | String | 合约类型:"STK" / "OPT" / "FUT" |
| action | String | 买卖方向:"BUY"(买入)/ "SELL"(卖出) |
| order_type | String | 订单类型:"MKT"(市价)/ "LMT"(限价)/ "STP"(止损)/ "STP_LMT"(止损限价)/ "TRAIL"(追踪止损) |
| total_quantity | i64 | 委托数量 |
| limit_price | f64 | 限价价格(LMT / STP_LMT 必填) |
| aux_price | f64 | 触发价格(STP / STP_LMT / TRAIL 必填) |
| time_in_force | String | 有效期:"DAY"(当日)/ "GTC"(撤单前有效)/ "GTD"(指定日期前有效) |
| outside_rth | bool | 是否允许盘前盘后交易(仅美股) |
| expiry | String | 期权/期货到期日,格式 "YYYYMMDD" |
| strike | f64 | 期权行权价 |
| right | String | 期权类型:"CALL" / "PUT" |
// 构造限价单
let order = serde_json::json!({
"account": "YOUR_ACCOUNT",
"symbol": "AAPL",
"sec_type": "STK",
"action": "BUY",
"order_type": "LMT",
"total_quantity": 10_i64,
"limit_price": 150.0_f64,
"time_in_force": "DAY",
});
// 构造市价单
let market_order = serde_json::json!({
"account": "YOUR_ACCOUNT",
"symbol": "AAPL",
"sec_type": "STK",
"action": "SELL",
"order_type": "MKT",
"total_quantity": 10_i64,
});
// 构造期权订单
let option_order = serde_json::json!({
"account": "YOUR_ACCOUNT",
"symbol": "AAPL",
"sec_type": "OPT",
"action": "BUY",
"order_type": "LMT",
"total_quantity": 1_i64,
"limit_price": 5.0_f64,
"expiry": "20250117",
"strike": 150.0_f64,
"right": "CALL",
});place_order 下单
tc.place_order(order: serde_json::Value) -> Result<Option<Value>, TigerError>
说明
提交订单到交易所。下单成功后返回订单 ID,可用于后续查询和操作。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| order | serde_json::Value | 是 | 订单 JSON 对象,参见上方说明 |
返回
Result<Option<Value>, TigerError>
返回下单结果,包含 id(订单 ID)、status、symbol 等字段。
示例
let order = serde_json::json!({
"account": config.account,
"symbol": "AAPL",
"sec_type": "STK",
"action": "BUY",
"order_type": "LMT",
"total_quantity": 10_i64,
"limit_price": 150.0_f64,
});
let result = tc.place_order(order).await?;
println!("下单结果: {:?}", result);preview_order 预览订单
tc.preview_order(order: serde_json::Value) -> Result<Option<Value>, TigerError>
说明
在实际下单前预估订单的费用、影响和可行性,不会真正提交到交易所。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| order | serde_json::Value | 是 | 订单 JSON 对象 |
返回
Result<Option<Value>, TigerError>
返回预览信息,包含预估佣金、印花税、保证金占用等字段。
示例
let order = serde_json::json!({
"account": config.account,
"symbol": "AAPL",
"sec_type": "STK",
"action": "BUY",
"order_type": "LMT",
"total_quantity": 10_i64,
"limit_price": 150.0_f64,
});
let preview = tc.preview_order(order).await?;
println!("预估费用: {:?}", preview);modify_order 修改订单
tc.modify_order(id: i64, order: serde_json::Value) -> Result<Option<Value>, TigerError>
说明
修改已提交但未完全成交的订单,可修改价格、数量等参数。仅支持状态为待成交(pending)的订单。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| id | i64 | 是 | 要修改的订单 ID |
| order | serde_json::Value | 是 | 包含新参数的订单 JSON 对象 |
返回
Result<Option<Value>, TigerError>
返回修改结果。
示例
// 将订单 12345 的限价价格从 150 改为 148
let mod_order = serde_json::json!({
"account": config.account,
"symbol": "AAPL",
"sec_type": "STK",
"action": "BUY",
"order_type": "LMT",
"total_quantity": 10_i64,
"limit_price": 148.0_f64,
});
let mod_result = tc.modify_order(12345, mod_order).await?;
println!("修改结果: {:?}", mod_result);cancel_order 取消订单
tc.cancel_order(id: i64) -> Result<Option<Value>, TigerError>
说明
撤销指定订单。只能撤销状态为待成交(pending)的订单,已成交订单无法撤销。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| id | i64 | 是 | 要撤销的订单 ID |
返回
Result<Option<Value>, TigerError>
返回撤单结果。
示例
let cancel_result = tc.cancel_order(12345).await?;
println!("撤单结果: {:?}", cancel_result);订单查询
orders 查询全部订单
tc.orders() -> Result<Option<Value>, TigerError>
说明
查询当前账户的全部历史订单,包含各种状态。
参数
无参数。
返回
Result<Option<Value>, TigerError>
返回订单列表,每条包含 id、symbol、action、status、totalQuantity、filledQuantity、limitPrice、avgFillPrice、createTime 等字段。
示例
let orders = tc.orders().await?;
println!("{:?}", orders);active_orders 查询待成交订单
tc.active_orders() -> Result<Option<Value>, TigerError>
说明
查询当前挂单中(未完全成交)的订单列表。
参数
无参数。
返回
Result<Option<Value>, TigerError>
返回待成交订单列表,结构同 orders。
示例
let active_orders = tc.active_orders().await?;
println!("{:?}", active_orders);inactive_orders 查询已撤销订单
tc.inactive_orders() -> Result<Option<Value>, TigerError>
说明
查询已撤销或已过期的订单列表。
参数
无参数。
返回
Result<Option<Value>, TigerError>
返回已撤销订单列表,结构同 orders。
示例
let inactive_orders = tc.inactive_orders().await?;
println!("{:?}", inactive_orders);filled_orders 查询已成交订单
tc.filled_orders() -> Result<Option<Value>, TigerError>
说明
查询所有已完全成交的订单列表。
参数
无参数。
返回
Result<Option<Value>, TigerError>
返回已成交订单列表,每条包含 avgFillPrice、filledQuantity、commission 等成交信息。
示例
let filled_orders = tc.filled_orders().await?;
println!("{:?}", filled_orders);持仓与资产
positions 查询持仓
tc.positions() -> Result<Option<Value>, TigerError>
说明
查询当前账户的所有持仓信息,包括股票、期权、期货等各类合约。
参数
无参数。
返回
Result<Option<Value>, TigerError>
返回持仓列表,每条包含 symbol、quantity、averageCost、marketValue、unrealizedPnl、realizedPnl 等字段。
示例
let positions = tc.positions().await?;
println!("{:?}", positions);assets 查询资产
tc.assets() -> Result<Option<Value>, TigerError>
说明
查询账户的资产概览,包括账户净值、现金余额、保证金使用情况等。
参数
无参数。
返回
Result<Option<Value>, TigerError>
返回资产信息,包含 netLiquidation(账户净值)、cashBalance、buyingPower、grossPositionValue、unrealizedPnl、realizedPnl 等字段。
示例
let assets = tc.assets().await?;
println!("{:?}", assets);prime_assets 查询综合账户资产
tc.prime_assets() -> Result<Option<Value>, TigerError>
说明
查询综合账户(Prime 账户)的资产信息,包含各子账户的汇总数据。
参数
无参数。
返回
Result<Option<Value>, TigerError>
返回综合账户资产信息,结构同 assets,并含有子账户明细。
示例
let prime_assets = tc.prime_assets().await?;
println!("{:?}", prime_assets);order_transactions 查询成交明细
tc.order_transactions(id: i64) -> Result<Option<Value>, TigerError>
说明
查询指定订单的逐笔成交明细,一个订单可能分多笔成交。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| id | i64 | 是 | 订单 ID |
返回
Result<Option<Value>, TigerError>
返回成交明细列表,每条包含 filledQuantity、filledPrice、commission、filledTime 等字段。
示例
let transactions = tc.order_transactions(12345).await?;
println!("{:?}", transactions);Updated 8 days ago
