账户变动

大约 9 分钟

以下全部为异步API,需要指定一个方法响应返回的结果

push_client = PushClient(host, port, use_ssl=(protocol == 'ssl'))

此外需注意,如果订阅时没有指定账户,回调时会推送关联到本 tiger_id 的多个账户的数据,在处理回调时需要判断数据属于哪个账户

资产变化的订阅与取消

订阅方法

PushClient.subscribe_asset(account)

取消方法

PushClient.unsubscribe_asset()

参数

参数名类型描述
accountstr需要订阅的 account id,不传则订阅所有关联的 account. 注意,如果订阅时没有指定账户,回调时会推送多个账户的数据,在处理回调时需要判断数据属于哪个账户

返回

需要使用 PushClient.asset_changed 响应返回结果。返回结果为 tigeropen.push.pb.AssetData_pb2.AssetData 对象

字段含义可对照参考获取资产接口 get_prime_assetsget_assets

详细字段解释参考对象:
PortfolioAccount 综合/模拟资产
Segment 综合/模拟分品种资产

PortfolioAccount 环球资产
SecuritySegment 环球股票资产
CommoditySegment 环球期货资产

回调数据字段含义
资产变动回调

字段类型描述
accountstr资金账号
currencystr币种。USD美元,HKD港币
segTypestr按交易品种划分的分类。S表示股票,C表示期货
availableFundsfloat可用资金,隔夜剩余流动性
excessLiquidityfloat当前剩余流动性
netLiquidationfloat总资产(净清算值)。总资产就是我们账户的净清算现金余额和证券总市值之和
equityWithLoanfloat含贷款价值总权益。等于总资产 - 美股期权
buyingPowerfloat购买力。仅适用于股票品种,即segment为S时有意义
cashBalancefloat现金额。当前所有币种的现金余额之和
grossPositionValuefloat证券总价值
initMarginReqfloat初始保证金
maintMarginReqfloat维持保证金
timestampint时间戳

示例

from tigeropen.push.pb.AssetData_pb2 import AssetData
from tigeropen.push.push_client import PushClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')

protocol, host, port = client_config.socket_host_port
push_client = PushClient(host, port, use_ssl=(protocol == 'ssl'))

# 定义回调方法
def on_asset_changed(frame: AssetData):
    """可进行自定义处理,此处仅打印"""
    print(f'asset change. {frame}')
    # 查看可用资金
    print(frame.availableFunds)
    # 查看持仓市值
    print(frame.grossPositionValue)

# 绑定回调方法
push_client.asset_changed = on_asset_changed
# 连接
push_client.connect(client_config.tiger_id, client_config.private_key)

# 订阅 
push_client.subscribe_asset(account=client_config.account)


# 取消订阅资产变化
# push_client.unsubscribe_asset()

回调数据示例

account: "111111"
currency: "USD"
segType: "S"
availableFunds: 1593.1191893
excessLiquidity: 1730.5666908
netLiquidation: 2856.1016998
equityWithLoan: 2858.1016998
buyingPower: 6372.4767571
cashBalance: 484.1516697
grossPositionValue: 2373.95003
initMarginReq: 1264.9825105
maintMarginReq: 1127.535009
timestamp: 1677745420121


持仓变化的订阅与取消

订阅方法

PushClient.subscribe_position(account)

取消方法

PushClient.unsubscribe_position()

参数

参数名类型描述
accountstr需要订阅的 account id,不传则订阅所有关联的 account. 注意,如果订阅时没有指定账户,回调时会推送多个账户的数据,在处理回调时需要判断数据属于哪个账户

示例

from tigeropen.push.pb.PositionData_pb2 import PositionData
from tigeropen.push.push_client import PushClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')

protocol, host, port = client_config.socket_host_port
push_client = PushClient(host, port, use_ssl=(protocol == 'ssl'))

# 定义回调方法
def on_position_changed(frame: PositionData):
    print(f'position change. {frame}')
    # 持仓标的
    print(frame.symbol)
    # 持仓成本
    print(frame.averageCost)
    
# 绑定回调方法
push_client.position_changed = on_position_changed
# 连接
push_client.connect(client_config.tiger_id, client_config.private_key)

# 订阅持仓的变化
push_client.subscribe_position(account=client_config.account)
# 取消订阅持仓的变化
push_client.unsubscribe_position()

返回
需要使用 PushClient.position_changed 响应返回结果,数据类型为 tigeropen.push.pb.PositionData_pb2.PositionData 。 列表项中的各字段可对照参考 get_position 中的 Position 对象。

字段类型描述
accountstr资金账号
symbolstr持仓标的代码,如 'AAPL', '00700', 'ES', 'CN'
expirystr仅支持期权、窝轮、牛熊证
strikestr仅支持期权、窝轮、牛熊证
rightstr仅支持期权、窝轮、牛熊证
identifierstr标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201'
multiplierint每手数量,仅限 futures, options, warrants, CBBC
marketstr市场。US, HK
currencystr币种。USD美元,HKD港币
segTypestr按交易品种划分的分类。S表示股票,C表示期货
secTypestrSTK Stocks, OPT Options, WAR Warrants, IOPT CBBC, CASH FOREX, FUT Futures, FOP Future Options
positionint持仓数量
positionScaleint持仓数量的偏移量
averageCostfloat持仓均价
latestPricefloat标的当前价格
marketValuefloat持仓市值
unrealizedPnlfloat持仓盈亏
namestr标的名称
timestampint时间戳

回调数据示例 股票持仓变化推送

account: "111111"
symbol: "BILI"
identifier: "BILI"
multiplier: 1
market: "US"
currency: "USD"
segType: "S"
secType: "STK"
position: 100
averageCost: 80
latestPrice: 19.83
marketValue: 1983
unrealizedPnl: -6017
timestamp: 1677745420121

订单变化的订阅和取消

订阅方法

PushClient.subscribe_order(account=None)

取消方法

PushClient.unsubscribe_order()

参数

参数名类型描述
accountstr需要订阅的 account id,不传则订阅所有关联的 account. 注意,如果订阅时没有指定账户,回调时会推送多个账户的数据,在处理回调时需要判断数据属于哪个账户

示例

from tigeropen.push.pb.OrderStatusData_pb2 import OrderStatusData
from tigeropen.push.push_client import PushClient
from tigeropen.tiger_open_config import get_client_config
from tigeropen.common.consts import OrderStatus
from tigeropen.common.util.order_utils import get_order_status
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')

protocol, host, port = client_config.socket_host_port
push_client = PushClient(host, port, use_ssl=(protocol == 'ssl'))

# 定义回调方法
def on_order_changed(frame: OrderStatusData):
    print(f'order changed: {frame}')
    print(f'order status: {frame.status}')
    # 将订单状态值转换为枚举类型
    status_enum = OrderStatus[frame.status]
    # 或 
    # status_enum = get_order_status(frame.status)
    
# 绑定回调方法
push_client.order_changed = on_order_changed
# 连接
push_client.connect(client_config.tiger_id, client_config.private_key)

# 订阅订单变化
push_client.subscribe_order(account=client_config.account)
# 取消订阅订单变化
push_client.unsubscribe_order()

返回
需要使用 PushClient.order_changed 响应返回结果,数据类型为 tigeropen.push.pb.OrderStatusData_pb2.OrderStatusData

返回结果中的字段含义可对照参考 get_order 中的 Order 对象

字段类型描述
idint订单号
accountstr资金账号
symbolstr持仓标的代码,如 'AAPL', '00700', 'ES', 'CN'
expirystr仅支持期权、窝轮、牛熊证
strikestr仅支持期权、窝轮、牛熊证
rightstr仅支持期权、窝轮、牛熊证
identifierstr标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201'
multiplierint每手数量,仅限 futures, options, warrants, CBBC
actionstr买卖方向。BUY表示买入,SELL表示卖出。
marketstr市场。US、HK
currencystr币种。USD美元,HKD港币
segTypestr按交易品种划分的分类。S表示股票,C表示期货
secTypestrSTK Stocks, OPT Options, WAR Warrants, IOPT CBBC, CASH FOREX, FUT Futures, FOP Future Options
orderTypestr订单类型。'MKT'市价单/'LMT'限价单/'STP'止损单/'STP_LMT'止损限价单/'TRAIL'跟踪止损单
isLongboolean是否多头持仓
totalQuantityint下单数量
totalQuantityScaleint下单数量偏移量,如 totalQuantity=111, totalQuantityScale=2,那么真实 totalQuantity=111*10^(-2)=1.11
filledQuantityint成交总数量(订单分多笔成交的,filledQuantity为累计成交总数)
filledQuantityScaleint成交总数量偏移量
avgFillPricefloat成交均价
limitPricefloat限价单价格
stopPricefloat止损价格
realizedPnlfloat已实现盈亏(只有综合账号有这个字段)
statusstr订单状态。注意,status的值为 OrderStatus的枚举名称, 可使用 OrderStatus[value] 转换为枚举类型
replaceStatusstr订单改单状态
cancelStatusstr订单撤单状态
outsideRthbool是否允许盘前盘后交易,仅适用于美股
canModifybool是否能修改
canCancelbool是否能取消
liquidationbool是否为平仓订单
namestr标的名称
sourcestr订单来源(from 'OpenApi', or other)
errorMsgstr错误信息
attrDescstr订单描述信息
commissionAndFeefloat佣金费用总计
openTimeint下单时间
timestampint订单状态最后更新时间
userMarkstr订单备注
totalCashAmountfloat订单总金额
filledCashAmountfloat成交总金额
attrListlist[str]订单属性列表,各属性含义如下: LIQUIDATION 强平, FRACTIONAL_SHARE 碎股订单(非整股), EXERCISE 行权, EXPIRE 过期, ASSIGNMENT 被动行权分配, CASH_SETTLE 现金交割, KNOCK_OUT 敲出, RECALL 召回订单, ODD_LOT 碎股订单(非整手), DEALER 交易员下单, GREY_MARKET 港股暗盘订单, BLOCK_TRADE 大宗交易, ATTACHED_ORDER 附加订单, OCA OCA订单
timeInForcestr订单有效时间。DAY: 当日有效,GTC: 撤销前有效,GTD: 有效至指定日期。

回调数据示例 股票订单推送示例

期货订单推送示例

{"id":"28875370355884032","account":"736845","symbol":"CL","identifier":"CL2312","multiplier":1000,"action":"BUY",
"market":"US","currency":"USD","segment":"C","secType":"FUT","orderType":"LMT","isLong":true,"totalQuantity":"1",
"filledQuantity":"1","avgFillPrice":77.76,"limitPrice":77.76,"status":"FILLED","outsideRth":true,"name":"WTI原油2312",
"source":"android","commissionAndFee":4.0,"openTime":"1669200792000","timestamp":"1669200782221"}

订单执行明细订阅和取消

订阅方法

PushClient.subscribe_transaction(account=client_config.account)

取消方法

PushClient.unsubscribe_transaction()

参数

参数名类型描述
accountstr需要订阅的 account id,不传则订阅所有关联的 account. 注意,如果订阅时没有指定账户,回调时会推送多个账户的数据,在处理回调时需要判断数据属于哪个账户

示例

from tigeropen.push.pb.OrderTransactionData_pb2 import OrderTransactionData
from tigeropen.push.push_client import PushClient
from tigeropen.tiger_open_config import get_client_config
client_config = get_client_config(private_key_path='private key path', tiger_id='your tiger id', account='your account')

protocol, host, port = client_config.socket_host_port
push_client = PushClient(host, port, use_ssl=(protocol == 'ssl'))

# 定义回调方法
def on_transaction_changed(frame: OrderTransactionData):
    print(f'transaction changed: {frame}')
    
# 绑定回调方法
push_client.transaction_changed = on_transaction_changed
# 连接
push_client.connect(client_config.tiger_id, client_config.private_key)

# 订阅
pushClient.subscribe_transaction(account=client_config.account)
# 取消订阅
pushClient.unsubscribe_transaction()

返回
需要使用 PushClient.transaction_changed 响应返回结果,数据类型为 tigeropen.push.pb.OrderTransactionData_pb2.OrderTransactionData
返回结果中的字段含义可对照参考 get_transactions 中的 Transaction

字段类型描述
idint订单执行ID
orderIdint订单号
accountstr资金账号
symbolstr持仓标的代码,如 'AAPL', '00700', 'ES', 'CN'
identifierstr标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201'
multiplierint每手数量(期权、期货专有)
actionstr买卖方向。BUY表示买入,SELL表示卖出。
marketstr市场。US、HK
currencystr币种。USD美元,HKD港币
segTypestr按交易品种划分的分类。S表示股票,C表示期货
secTypestr交易品种,标的类型。STK表示股票,FUT表示期货
filledPricefloat价格
filledQuantityint成交数量
createTimeintcreate time
updateTimeintupdate time
transactTimeint成交时间
timestampinttimestamp

回调数据示例


id: 2999543887211111111
orderId: 29995438111111111
account: "1111111"
symbol: "ZC"
identifier: "ZC2305"
multiplier: 5000
action: "BUY"
market: "US"
currency: "USD"
segType: "C"
secType: "FUT"
filledPrice: 6.385
filledQuantity: 1
createTime: 1677746237303
updateTime: 1677746237303
transactTime: 1677746237289
timestamp: 1677746237313

上次编辑于: