账户变动
资产变化的订阅与取消
订阅方法
virtual bool subscribe_asset(const std::string& account) = 0;
取消方法
virtual bool unsubscribe_asset(const std::string& account) = 0;
参数
Parameter | Type | Description |
---|---|---|
account | std::string& | 需要订阅的 account id,不传则订阅所有关联的 account |
返回
需要使用 push_client->set_asset_changed_callback
响应返回结果
回调数据字段含义
资产变动回调
字段 | 类型 | 描述 |
---|---|---|
account | std::string& | 资金账号 |
currency | std::string& | 币种。USD美元,HKD港币 |
segType | std::string& | 按交易品种划分的分类。S表示股票,C表示期货 |
availableFunds | double | 可用资金,隔夜剩余流动性 |
excessLiquidity | double | 当前剩余流动性 |
netLiquidation | double | 总资产(净清算值)。总资产就是我们账户的净清算现金余额和证券总市值之和 |
equityWithLoan | double | 含贷款价值总权益。等于总资产 - 美股期权 |
buyingPower | double | 购买力。仅适用于股票品种,即segType为S时有意义 |
cashBalance | double | 现金额。当前所有币种的现金余额之和 |
grossPositionValue | double | 证券总价值 |
initMarginReq | double | 初始保证金 |
maintMarginReq | double | 维持保证金 |
timestamp | uint64_t | 时间戳 |
示例
class TestPushClient {
private:
std::shared_ptr<IPushClient> push_client;
std::vector<std::string> symbols;
public:
TestPushClient(std::shared_ptr<IPushClient> client) : push_client(client) {
std::vector<std::string> hk_option_symbols = {"TCH.HK 20241230 410.00 CALL"};
std::vector<std::string> future_symbols = {"CL2412"};
symbols = future_symbols;
}
void connected_callback() {
ucout << "Connected to push server" << std::endl;
push_client->subscribe_asset(utility::conversions::to_utf8string(push_client->get_client_config().account));
}
void asset_changed_callback(const tigeropen::push::pb::AssetData& data) {
ucout << "Asset changed:" << std::endl;
ucout << "- cashbalance: " << data.cashbalance() << std::endl;
ucout << "- netliquidation: " << data.netliquidation() << std::endl;
}
void start_test(ClientConfig config) {
push_client->set_connected_callback(std::bind(&TestPushClient::connected_callback, this));
push_client->set_asset_changed_callback(std::bind(&TestPushClient::asset_changed_callback, this, std::placeholders::_1));
push_client->connect();
std::signal(SIGINT, signal_handler); //Ctrl+C
std::signal(SIGTERM, signal_handler); //kill
while (keep_running)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
push_client->unsubscribe_asset(utility::conversions::to_utf8string(config.account));
push_client->disconnect();
}
static void test_push_client(std::shared_ptr<IPushClient> push_client, ClientConfig config) {
TestPushClient test(push_client);
test.start_test(config);
}
int main(int argc, char *args[]) {
cout << "Tiger Api main" << endl;
/************************** set config **********************/
ClientConfig config = ClientConfig(true);
config.private_key = "-----BEGIN RSA PRIVATE KEY-----\n"
"xxxxxx private key xxxxxxxx"
"-----END RSA PRIVATE KEY-----";
config.tiger_id = "Tiger ID";
config.account = "Account ID";
auto push_client = IPushClient::create_push_client(config);
TestPushClient::test_push_client(push_client, config);
回调数据示例
{
"dataType":"Asset",
"assetData":
{
"account":"111111111111",
"segType":"S",
"availableFunds":797832.55572773679,
"excessLiquidity":826227.79308567208,
"netLiquidation":944515.55984458746,
"equityWithLoan":944494.85984458751,
"buyingPower":3191330.2229109472,
"cashBalance":656046.6059349241,
"grossPositionValue":288448.25390966347,
"initMarginReq":146662.30411685078,
"maintMarginReq":118287.76675891539,
"timestamp":"1732177851891"
}
}
持仓变化的订阅与取消
订阅方法
virtual bool subscribe_position(const std::string& account) = 0;
取消方法
virtual bool unsubscribe_position(const std::string& account) = 0;
参数
参数名 | 类型 | 描述 |
---|---|---|
account | std::string& | 需要订阅的 account id,不传则订阅所有关联的 account |
示例
class TestPushClient {
private:
std::shared_ptr<IPushClient> push_client;
std::vector<std::string> symbols;
public:
TestPushClient(std::shared_ptr<IPushClient> client) : push_client(client) {
std::vector<std::string> hk_option_symbols = {"TCH.HK 20241230 410.00 CALL"};
std::vector<std::string> future_symbols = {"CL2412"};
symbols = future_symbols;
}
void connected_callback() {
ucout << "Connected to push server" << std::endl;
push_client->subscribe_position(utility::conversions::to_utf8string(push_client->get_client_config().account));
}
void position_changed_callback(const tigeropen::push::pb::PositionData& data) {
ucout << "Position changed:" << std::endl;
ucout << "- symbol: " << utility::conversions::to_string_t(data.symbol()) << std::endl;
ucout << "- positionqty: " << data.positionqty() << std::endl;
}
void start_test(ClientConfig config) {
push_client->set_position_changed_callback(std::bind(&TestPushClient::position_changed_callback, this, std::placeholders::_1));
push_client->connect();
std::signal(SIGINT, signal_handler); //Ctrl+C
std::signal(SIGTERM, signal_handler); //kill
while (keep_running)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
push_client->unsubscribe_position(utility::conversions::to_utf8string(config.account));
push_client->disconnect();
}
static void test_push_client(std::shared_ptr<IPushClient> push_client, ClientConfig config) {
TestPushClient test(push_client);
test.start_test(config);
}
int main(int argc, char *args[]) {
cout << "Tiger Api main" << endl;
/************************** set config **********************/
ClientConfig config = ClientConfig(true);
config.private_key = "-----BEGIN RSA PRIVATE KEY-----\n"
"xxxxxx private key xxxxxxxx"
"-----END RSA PRIVATE KEY-----";
config.tiger_id = "Tiger ID";
config.account = "Account ID";
auto push_client = IPushClient::create_push_client(config);
TestPushClient::test_push_client(push_client, config);
返回
需要使用 push_client->set_position_changed_callback
响应返回结果
字段 | 类型 | 描述 |
---|---|---|
account | std::string& | 资金账号 |
symbol | std::string& | 持仓标的代码,如 'AAPL', '00700', 'ES', 'CN' |
expiry | std::string& | 仅支持期权、窝轮、牛熊证 |
strike | std::string& | 仅支持期权、窝轮、牛熊证 |
right | std::string& | 仅支持期权、窝轮、牛熊证 |
identifier | std::string& | 标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201' |
multiplier | uint32_t | 每手数量,仅限 futures, options, warrants, CBBC |
market | std::string& | 市场 |
currency | std::string& | 币种 |
segType | std::string& | 按交易品种划分的分类。S表示股票,C表示期货 |
secType | std::string& | STK Stocks, OPT Options, WAR Warrants, IOPT CBBC, CASH FOREX, FUT Futures, FOP Future Options |
position | int64_t | 持仓数量 |
positionScale | int32_t | 持仓数量的偏移量 |
averageCost | double | 持仓均价 |
latestPrice | double | 标的当前价格 |
marketValue | double | 持仓市值 |
unrealizedPnl | double | 持仓盈亏 |
name | std::string& | 标的名称 |
timestamp | uint64_t | 时间戳 |
saleable | int64_t | 可出售数量(适用于中国A股市场股票) |
positionQty | double | 总持仓数量 |
salableQty | double | 可卖数量 |
回调数据示例 股票持仓变化推送
{
"account": "1111111",
"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
}
订单变化的订阅和取消
订阅方法
virtual bool subscribe_order(const std::string& account) = 0;
取消方法
virtual bool unsubscribe_order(const std::string& account) = 0;
参数
参数名 | 类型 | 描述 |
---|---|---|
account | std::string& | 需要订阅的 account id,不传则订阅所有关联的 account |
示例
class TestPushClient {
private:
std::shared_ptr<IPushClient> push_client;
std::vector<std::string> symbols;
public:
TestPushClient(std::shared_ptr<IPushClient> client) : push_client(client) {
std::vector<std::string> hk_option_symbols = {"TCH.HK 20241230 410.00 CALL"};
std::vector<std::string> future_symbols = {"CL2412"};
symbols = future_symbols;
}
void connected_callback() {
ucout << "Connected to push server" << std::endl;
push_client->subscribe_order(utility::conversions::to_utf8string(push_client->get_client_config().account));
}
void order_changed_callback(const tigeropen::push::pb::OrderStatusData& data) {
ucout << "Order changed:" << std::endl;
ucout << "- id: " << data.id() << std::endl;
ucout << "- status: " << utility::conversions::to_string_t(data.status()) << std::endl;
ucout << "- avgfillprice: " << data.avgfillprice() << std::endl;
}
void start_test(ClientConfig config) {
push_client->set_order_changed_callback(std::bind(&TestPushClient::order_changed_callback, this, std::placeholders::_1));
push_client->connect();
std::signal(SIGINT, signal_handler); //Ctrl+C
std::signal(SIGTERM, signal_handler); //kill
while (keep_running)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
push_client->unsubscribe_order(utility::conversions::to_utf8string(config.account));
push_client->disconnect();
}
static void test_push_client(std::shared_ptr<IPushClient> push_client, ClientConfig config) {
TestPushClient test(push_client);
test.start_test(config);
}
int main(int argc, char *args[]) {
cout << "Tiger Api main" << endl;
/************************** set config **********************/
ClientConfig config = ClientConfig(true);
config.private_key = "-----BEGIN RSA PRIVATE KEY-----\n"
"xxxxxx private key xxxxxxxx"
"-----END RSA PRIVATE KEY-----";
config.tiger_id = "Tiger ID";
config.account = "Account ID";
auto push_client = IPushClient::create_push_client(config);
TestPushClient::test_push_client(push_client, config);
返回
需要使用 push_client->set_order_changed_callback
响应返回结果
Field | Type | 描述 |
---|---|---|
id | int64_t | 订单号 |
account | std::string& | 资金账号 |
symbol | std::string& | 持仓标的代码,如 'AAPL', '00700', 'ES', 'CN' |
expiry | std::string& | 仅支持期权、窝轮、牛熊证 |
strike | std::string& | 仅支持期权、窝轮、牛熊证 |
right | std::string& | 仅支持期权、窝轮、牛熊证 |
identifier | std::string& | 标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201' |
multiplier | uint32_t | 每手数量,仅限 futures, options, warrants, CBBC |
action | std::string& | 买卖方向。BUY表示买入,SELL表示卖出。 |
market | std::string& | 市场。US、HK |
currency | std::string& | 币种。USD美元,HKD港币 |
segType | std::string& | 按交易品种划分的分类。S表示股票,C表示期货 |
secType | std::string& | STK Stocks, OPT Options, WAR Warrants, IOPT CBBC, CASH FOREX, FUT Futures, FOP Future Options |
orderType | std::string& | 订单类型。'MKT'市价单/'LMT'限价单/'STP'止损单/'STP_LMT'止损限价单/'TRAIL'跟踪止损单 |
isLong | bool | 是否多头持仓 |
totalQuantity | int64_t | 下单数量 |
totalQuantityScale | int32_t | 下单数量偏移量,如 totalQuantity=111, totalQuantityScale=2,那么真实 totalQuantity=111*10^(-2)=1.11 |
filledQuantity | int64_t | 成交总数量(订单分多笔成交的,filledQuantity为累计成交总数) |
filledQuantityScale | int32_t | 成交总数量偏移量 |
avgFillPrice | double | 成交均价 |
limitPrice | double | 限价单价格 |
stopPrice | double | 止损价格 |
realizedPnl | double | 已实现盈亏(只有综合账号有这个字段) |
status | std::string& | 订单状态 |
replaceStatus | std::string& | 订单变更状态 |
cancelStatus | std::string& | 订单取消状态 |
outsideRth | bool | 是否允许盘前盘后交易,仅适用于美股 |
canModify | bool | 是否能修改 |
canCancel | bool | 是否能取消 |
liquidation | bool | 是否为平仓订单 |
name | std::string& | 标的名称 |
source | std::string& | 订单来源(from 'OpenApi', or other) |
errorMsg | std::string& | 错误信息 |
attrDesc | std::string& | 订单描述信息 |
commissionAndFee | float | 佣金费用总计 |
openTime | uint64_t | 下单时间 |
timestamp | uint64_t | 订单状态最后更新时间 |
userMark | std::string& | 订单备注 |
totalCashAmount | double | 订单总金额 |
filledCashAmount | double | 成交总金额 |
回调数据示例 股票订单推送示例
{
"dataType":"OrderStatus",
"orderStatusData":
{
"id":"37129891133115200",
"account":"123123123",
"symbol":"PDD",
"identifier":"PDD",
"multiplier":1,
"action":"BUY",
"market":"US",
"currency":"USD",
"segType":"S",
"secType":"STK",
"orderType":"LMT",
"isLong":true,
"totalQuantity":"1",
"limitPrice":50,
"status":"PendingSubmit",
"replaceStatus":"NONE",
"cancelStatus":"NONE",
"outsideRth":true,
"canModify":true,
"canCancel":true,
"name":"PDD Holdings",
"source":"openapi",
"openTime":"1732177851000",
"timestamp":"1732177851874"
}
}
订单执行明细订阅和取消
订阅方法
virtual bool subscribe_transaction(const std::string& account) = 0;
取消方法
virtual bool unsubscribe_transaction(const std::string& account) = 0;
参数
参数名 | 类型 | 描述 |
---|---|---|
account | std::string& | 需要订阅的 account id,不传则订阅所有关联的 account |
返回
需要使用 push_client->set_transaction_changed_callback
响应返回结果
回调数据字段含义
字段 | 类型 | 描述 |
---|---|---|
id | int64_t | 订单执行ID |
order_id | int64_t | 订单号 |
account | std::string& | 资金账号 |
symbol | std::string& | 持仓标的代码,如 'AAPL', '00700', 'ES', 'CN' |
identifier | std::string& | 标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201' |
multiplier | uint32_t | 每手数量(期权、期货专有) |
action | std::string& | 买卖方向。BUY表示买入,SELL表示卖出。 |
market | std::string& | 市场 |
currency | std::string& | 币种 |
segType | std::string& | 按交易品种划分的分类。S表示股票,C表示期货 |
secType | std::string& | 交易品种,标的类型。STK表示股票,FUT表示期货 |
filled_price | double | 价格 |
filled_quantity | int64_t | 成交数量 |
create_time | uint64_t | Create time |
update_time | uint64_t | Update time |
transact_time | uint64_t | 成交时间 |
timestamp | uint64_t | Timestamp |
示例
class TestPushClient {
private:
std::shared_ptr<IPushClient> push_client;
std::vector<std::string> symbols;
public:
TestPushClient(std::shared_ptr<IPushClient> client) : push_client(client) {
std::vector<std::string> hk_option_symbols = {"TCH.HK 20241230 410.00 CALL"};
std::vector<std::string> future_symbols = {"CL2412"};
symbols = future_symbols;
}
void connected_callback() {
ucout << "Connected to push server" << std::endl;
push_client->subscribe_transaction(utility::conversions::to_utf8string(push_client->get_client_config().account));
}
void transaction_changed_callback(const tigeropen::push::pb::OrderTransactionData& data) {
ucout << "Transaction changed:" << std::endl;
ucout << "- Transaction Id: " << data.id() << std::endl;
ucout << "- Transaction Time: " << data.transactTime() << std::endl;
}
void start_test(ClientConfig config) {
push_client->set_connected_callback(std::bind(&TestPushClient::connected_callback, this));
push_client->set_transaction_changed_callback(std::bind(&TestPushClient::transaction_changed_callback, this, std::placeholders::_1));
push_client->connect();
std::signal(SIGINT, signal_handler); //Ctrl+C
std::signal(SIGTERM, signal_handler); //kill
while (keep_running)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
push_client->unsubscribe_transaction(utility::conversions::to_utf8string(config.account));
push_client->disconnect();
}
static void test_push_client(std::shared_ptr<IPushClient> push_client, ClientConfig config) {
TestPushClient test(push_client);
test.start_test(config);
}
int main(int argc, char *args[]) {
cout << "Tiger Api main" << endl;
/************************** set config **********************/
ClientConfig config = ClientConfig(true);
config.private_key = "-----BEGIN RSA PRIVATE KEY-----\n"
"xxxxxx private key xxxxxxxx"
"-----END RSA PRIVATE KEY-----";
config.tiger_id = "Tiger ID";
config.account = "Account ID";
auto push_client = IPushClient::create_push_client(config);
TestPushClient::test_push_client(push_client, config);
回调数据示例
{
"id": 2999543887211111111,
"orderId": 29995438111111111,
"account": "11111111",
"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,
}