實時成交明細推送
已訂閱的標的的實時逐筆成交明細推送。
SDK Links
Python  | longport.openapi.QuoteContext.set_on_trades | 
Rust  | longport::quote::QuoteContext#set_on_trades | 
Go  | QuoteContext.OnTrade | 
Node.js  | QuoteContext#setOnTrades | 
ℹ️Info
業務指令:104
數據格式 
Properties 
| Name | Type | Description | 
|---|---|---|
| symbol | string | 標的代碼,例如:AAPL.US | 
| sequence | int64 | 序列號 | 
| trades | object[] | 逐筆明細數據 | 
| ∟ price | string | 價格 | 
| ∟ volume | int64 | 成交量 | 
| ∟ timestamp | int64 | 成交時間 | 
| ∟ trade_type | string | 交易類型說明 | 
| ∟ direction | int32 | 交易方向  可选值: 0 - neutral1 - down2 - up | 
| ∟ trade_session | int32 | 交易時段,詳見 TradeSession | 
交易類型 
港股
*- 場外交易D- 碎股交易M- 非自動對盤P- 開市前成交盤U- 競價交易X- 同一券商非自動對盤Y- 同一券商自動對盤- 自動對盤
美股
- 自動對盤A- 收購B- 批量交易D- 分配F- 跨市掃盤單G- 批量賣出H- 離價交易I- 碎股交易K- 第 155 條交易(紐交所規則)M- 交易所收盤價P- 前參考價Q- 交易所開盤價S- 拆單交易V- 附屬交易W- 平均價成交X- 跨市場交易1- 停售股票(常規交易)
Protobuf 
protobuf
message PushTrade {
  string symbol = 1;
  int64 sequence = 2;
  repeated Trade trade = 3;
}
message Trade {
  string price = 1;
  int64 volume = 2;
  int64 timestamp = 3;
  string trade_type = 4;
  int32 direction = 5;
  TradeSession trade_session = 6;
}Example 
python
# 實時成交明細推送
# https://open.longportapp.com/docs/quote/push/push-trade
# 訂閱行情數據請檢查“開發者中心“ - “行情權限”是否正確
# https://open.longportapp.com/account
#
# - 港股 - BMP 基礎報價,無實時行情推送,無法用 WebSocket 訂閱
# - 美股 - LV1 納斯達克最優報價 (只限 OpenAPI)
#
# 運行前請訪問“開發者中心“確保賬戶有正確的行情權限。
# 如沒有開通行情權限,可以通過“LongPort”手機客戶端,並進入“我的 - 我的行情 - 行情商城”購買開通行情權限。
from time import sleep
from longport.openapi import QuoteContext, Config, SubType, PushTrades
def on_trades(symbol: str, event: PushTrades):
    print(symbol, event)
config = Config.from_env()
ctx = QuoteContext(config)
ctx.set_on_trades(on_trades)
ctx.subscribe(["700.HK", "AAPL.US"], [SubType.Trade])
sleep(30)JSON Example 
json
{
  "symbol": "700.HK",
  "sequence": 160808750000000,
  "trades": [
    {
      "price": "158.760",
      "volume": 1,
      "timestamp": 1651103979,
      "trade_type": "I",
      "direction": 0,
      "trade_session": 2
    },
    {
      "price": "158.745",
      "volume": 1,
      "timestamp": 1651103985,
      "trade_type": "I",
      "direction": 0,
      "trade_session": 2
    },
    {
      "price": "158.800",
      "volume": 1,
      "timestamp": 1651103995,
      "trade_type": "I",
      "direction": 0,
      "trade_session": 2
    }
  ]
}