交易操作

TypeScript SDK 通过 TradeClient 提供全部交易接口,所有方法均返回 Promise。以下为完整可运行示例:

import { ClientConfig } from '@tigeropenapi/tigeropen';
import { TradeClient } from '@tigeropenapi/tigeropen';

async function main() {
  const config = ClientConfig.fromPropertiesFile('tiger_openapi_config.properties');
  const tc = new TradeClient(config);

  // 查询持仓
  const positions = await tc.positions();
  console.log('持仓:', positions);

  // 预览限价买单
  const preview = await tc.previewOrder({
    account: config.account,
    symbol: 'AAPL',
    secType: 'STK',
    action: 'BUY',
    orderType: 'LMT',
    totalQuantity: 10,
    limitPrice: 150.0,
  });
  console.log('预览结果:', preview);

  // 实际下单
  const result = await tc.placeOrder({
    account: config.account,
    symbol: 'AAPL',
    secType: 'STK',
    action: 'BUY',
    orderType: 'LMT',
    totalQuantity: 10,
    limitPrice: 150.0,
  });
  console.log('下单结果:', result);
}

main().catch(console.error);

合约查询

contract 查询单个合约

tc.contract(symbol: string, secType: string): Promise<unknown>

说明

查询单个合约的详细信息,包括名称、货币、最小交易单位等。

参数

参数名类型是否必填描述
symbolstring合约代码,如 'AAPL''00700'
secTypestring合约类型:'STK'(股票)/ 'OPT'(期权)/ 'FUT'(期货)/ 'WAR'(权证)/ 'IOPT'(牛熊证)

返回

Promise<unknown>

返回合约详情,包含 symbolnamecurrencyexchangelotSize 等字段。

示例

const contract = await tc.contract('AAPL', 'STK');
console.log(contract);

// 查询港股
const hkContract = await tc.contract('00700', 'STK');

contracts 批量查询合约

tc.contracts(symbols: string[], secType: string): Promise<unknown>

说明

批量查询多个合约信息,减少网络请求次数。

参数

参数名类型是否必填描述
symbolsstring[]合约代码列表
secTypestring合约类型,同 contract

返回

Promise<unknown>

返回合约信息数组,结构同 contract

示例

const contracts = await tc.contracts(['AAPL', 'TSLA', 'MSFT'], 'STK');
console.log(contracts);

quoteContract 查询衍生品合约

tc.quoteContract(symbol: string, secType: string): Promise<unknown>

说明

查询期权或期货等衍生品的合约明细,包含行权价、到期日、合约乘数等衍生品特有字段。

参数

参数名类型是否必填描述
symbolstring衍生品代码,期权格式:'AAPL 250117C00150000'
secTypestring合约类型,通常为 'OPT''FUT'

返回

Promise<unknown>

返回衍生品合约信息,包含 strikeexpiryrightPUT/CALL)、multiplier 等字段。

示例

const quoteContract = await tc.quoteContract('AAPL  250117C00150000', 'OPT');
console.log(quoteContract);

订单操作

Order 对象说明

下单时传入的订单对象包含以下关键字段:

字段类型描述
accountstring账户 ID
symbolstring合约代码
secTypestring合约类型:'STK' / 'OPT' / 'FUT'
actionstring买卖方向:'BUY'(买入)/ 'SELL'(卖出)
orderTypestring订单类型:'MKT'(市价)/ 'LMT'(限价)/ 'STP'(止损)/ 'STP_LMT'(止损限价)/ 'TRAIL'(追踪止损)
totalQuantitynumber委托数量
limitPricenumber限价价格(LMT / STP_LMT 必填)
auxPricenumber触发价格(STP / STP_LMT / TRAIL 必填)
timeInForcestring有效期:'DAY'(当日)/ 'GTC'(撤单前有效)/ 'GTD'(指定日期前有效)
outsideRthboolean是否允许盘前盘后交易(仅美股)
expirystring期权/期货到期日,格式 'YYYYMMDD'
strikenumber期权行权价
rightstring期权类型:'CALL' / 'PUT'

placeOrder 下单

tc.placeOrder(order: Order): Promise<unknown>

说明

提交订单到交易所。下单成功后返回订单 ID,可用于后续查询和操作。

参数

参数名类型是否必填描述
orderOrder订单对象,参见上方 Order 对象说明

返回

Promise<unknown>

返回下单结果,包含 id(订单 ID)、statussymbol 等字段。

示例

// 限价买入
const result = await tc.placeOrder({
  account: config.account,
  symbol: 'AAPL',
  secType: 'STK',
  action: 'BUY',
  orderType: 'LMT',
  totalQuantity: 10,
  limitPrice: 150.0,
});
console.log('订单ID:', result);

// 市价卖出
const sellResult = await tc.placeOrder({
  account: config.account,
  symbol: 'AAPL',
  secType: 'STK',
  action: 'SELL',
  orderType: 'MKT',
  totalQuantity: 10,
});

// 盘前/盘后交易
const extResult = await tc.placeOrder({
  account: config.account,
  symbol: 'AAPL',
  secType: 'STK',
  action: 'BUY',
  orderType: 'LMT',
  totalQuantity: 5,
  limitPrice: 148.0,
  outsideRth: true,
  timeInForce: 'DAY',
});

previewOrder 预览订单

tc.previewOrder(order: Order): Promise<unknown>

说明

在实际下单前预估订单的费用、影响和可行性,不会真正提交到交易所。

参数

参数名类型是否必填描述
orderOrder订单对象

返回

Promise<unknown>

返回预览信息,包含预估佣金、印花税、保证金占用等字段。

示例

const preview = await tc.previewOrder({
  account: config.account,
  symbol: 'AAPL',
  secType: 'STK',
  action: 'BUY',
  orderType: 'LMT',
  totalQuantity: 10,
  limitPrice: 150.0,
});
console.log('预估费用:', preview);

modifyOrder 修改订单

tc.modifyOrder(id: number, order: Order): Promise<unknown>

说明

修改已提交但未完全成交的订单,可修改价格、数量等参数。仅支持状态为待成交(pending)的订单。

参数

参数名类型是否必填描述
idnumber要修改的订单 ID
orderOrder包含新参数的订单对象

返回

Promise<unknown>

返回修改结果。

示例

// 将订单 12345 的限价价格从 150 改为 148
const modResult = await tc.modifyOrder(12345, {
  account: config.account,
  symbol: 'AAPL',
  secType: 'STK',
  action: 'BUY',
  orderType: 'LMT',
  totalQuantity: 10,
  limitPrice: 148.0,
});
console.log('修改结果:', modResult);

cancelOrder 取消订单

tc.cancelOrder(id: number): Promise<unknown>

说明

撤销指定订单。只能撤销状态为待成交(pending)的订单,已成交订单无法撤销。

参数

参数名类型是否必填描述
idnumber要撤销的订单 ID

返回

Promise<unknown>

返回撤单结果。

示例

const cancelResult = await tc.cancelOrder(12345);
console.log('撤单结果:', cancelResult);

订单查询

orders 查询全部订单

tc.orders(): Promise<unknown>

说明

查询当前账户的全部历史订单,包含各种状态。

参数

无参数。

返回

Promise<unknown>

返回订单列表,每条包含 idsymbolactionstatustotalQuantityfilledQuantitylimitPriceavgFillPricecreateTime 等字段。

示例

const orders = await tc.orders();
console.log(orders);

activeOrders 查询待成交订单

tc.activeOrders(): Promise<unknown>

说明

查询当前挂单中(未完全成交)的订单列表。

参数

无参数。

返回

Promise<unknown>

返回待成交订单列表,结构同 orders

示例

const activeOrders = await tc.activeOrders();
console.log(activeOrders);

inactiveOrders 查询已撤销订单

tc.inactiveOrders(): Promise<unknown>

说明

查询已撤销或已过期的订单列表。

参数

无参数。

返回

Promise<unknown>

返回已撤销订单列表,结构同 orders

示例

const inactiveOrders = await tc.inactiveOrders();
console.log(inactiveOrders);

filledOrders 查询已成交订单

tc.filledOrders(): Promise<unknown>

说明

查询所有已完全成交的订单列表。

参数

无参数。

返回

Promise<unknown>

返回已成交订单列表,每条包含 avgFillPricefilledQuantitycommission 等成交信息。

示例

const filledOrders = await tc.filledOrders();
console.log(filledOrders);

持仓与资产

positions 查询持仓

tc.positions(): Promise<unknown>

说明

查询当前账户的所有持仓信息,包括股票、期权、期货等各类合约。

参数

无参数。

返回

Promise<unknown>

返回持仓列表,每条包含 symbolquantityaverageCostmarketValueunrealizedPnlrealizedPnl 等字段。

示例

const positions = await tc.positions();
console.log(positions);

assets 查询资产

tc.assets(): Promise<unknown>

说明

查询账户的资产概览,包括账户净值、现金余额、保证金使用情况等。

参数

无参数。

返回

Promise<unknown>

返回资产信息,包含 netLiquidation(账户净值)、cashBalancebuyingPowergrossPositionValueunrealizedPnlrealizedPnl 等字段。

示例

const assets = await tc.assets();
console.log(assets);

primeAssets 查询综合账户资产

tc.primeAssets(): Promise<unknown>

说明

查询综合账户(Prime 账户)的资产信息,包含各子账户的汇总数据。

参数

无参数。

返回

Promise<unknown>

返回综合账户资产信息,结构同 assets,并含有子账户明细。

示例

const primeAssets = await tc.primeAssets();
console.log(primeAssets);

orderTransactions 查询成交明细

tc.orderTransactions(id: number): Promise<unknown>

说明

查询指定订单的逐笔成交明细,一个订单可能分多笔成交。

参数

参数名类型是否必填描述
idnumber订单 ID

返回

Promise<unknown>

返回成交明细列表,每条包含 filledQuantityfilledPricecommissionfilledTime 等字段。

示例

const transactions = await tc.orderTransactions(12345);
console.log(transactions);