账户变动

大约 8 分钟

资产变化的订阅与取消

订阅方法

virtual bool subscribe_asset(const std::string& account) = 0;

取消方法

virtual bool unsubscribe_asset(const std::string& account) = 0;

参数

ParameterTypeDescription
accountstd::string&需要订阅的 account id,不传则订阅所有关联的 account

返回

需要使用 push_client->set_asset_changed_callback 响应返回结果

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

字段类型描述
accountstd::string&资金账号
currencystd::string&币种。USD美元,HKD港币
segTypestd::string&按交易品种划分的分类。S表示股票,C表示期货
availableFundsdouble可用资金,隔夜剩余流动性
excessLiquiditydouble当前剩余流动性
netLiquidationdouble总资产(净清算值)。总资产就是我们账户的净清算现金余额和证券总市值之和
equityWithLoandouble含贷款价值总权益。等于总资产 - 美股期权
buyingPowerdouble购买力。仅适用于股票品种,即segType为S时有意义
cashBalancedouble现金额。当前所有币种的现金余额之和
grossPositionValuedouble证券总价值
initMarginReqdouble初始保证金
maintMarginReqdouble维持保证金
timestampuint64_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;

参数

参数名类型描述
accountstd::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 响应返回结果

字段类型描述
accountstd::string&资金账号
symbolstd::string&持仓标的代码,如 'AAPL', '00700', 'ES', 'CN'
expirystd::string&仅支持期权、窝轮、牛熊证
strikestd::string&仅支持期权、窝轮、牛熊证
rightstd::string&仅支持期权、窝轮、牛熊证
identifierstd::string&标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201'
multiplieruint32_t每手数量,仅限 futures, options, warrants, CBBC
marketstd::string&市场
currencystd::string&币种
segTypestd::string&按交易品种划分的分类。S表示股票,C表示期货
secTypestd::string&STK Stocks, OPT Options, WAR Warrants, IOPT CBBC, CASH FOREX, FUT Futures, FOP Future Options
positionint64_t持仓数量
positionScaleint32_t持仓数量的偏移量
averageCostdouble持仓均价
latestPricedouble标的当前价格
marketValuedouble持仓市值
unrealizedPnldouble持仓盈亏
namestd::string&标的名称
timestampuint64_t时间戳
saleableint64_t可出售数量(适用于中国A股市场股票)
positionQtydouble总持仓数量
salableQtydouble可卖数量

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

{
  "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;

参数

参数名类型描述
accountstd::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 响应返回结果

FieldType描述
idint64_t订单号
accountstd::string&资金账号
symbolstd::string&持仓标的代码,如 'AAPL', '00700', 'ES', 'CN'
expirystd::string&仅支持期权、窝轮、牛熊证
strikestd::string&仅支持期权、窝轮、牛熊证
rightstd::string&仅支持期权、窝轮、牛熊证
identifierstd::string&标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201'
multiplieruint32_t每手数量,仅限 futures, options, warrants, CBBC
actionstd::string&买卖方向。BUY表示买入,SELL表示卖出。
marketstd::string&市场。US、HK
currencystd::string&币种。USD美元,HKD港币
segTypestd::string&按交易品种划分的分类。S表示股票,C表示期货
secTypestd::string&STK Stocks, OPT Options, WAR Warrants, IOPT CBBC, CASH FOREX, FUT Futures, FOP Future Options
orderTypestd::string&订单类型。'MKT'市价单/'LMT'限价单/'STP'止损单/'STP_LMT'止损限价单/'TRAIL'跟踪止损单
isLongbool是否多头持仓
totalQuantityint64_t下单数量
totalQuantityScaleint32_t下单数量偏移量,如 totalQuantity=111, totalQuantityScale=2,那么真实 totalQuantity=111*10^(-2)=1.11
filledQuantityint64_t成交总数量(订单分多笔成交的,filledQuantity为累计成交总数)
filledQuantityScaleint32_t成交总数量偏移量
avgFillPricedouble成交均价
limitPricedouble限价单价格
stopPricedouble止损价格
realizedPnldouble已实现盈亏(只有综合账号有这个字段)
statusstd::string&订单状态
replaceStatusstd::string&订单变更状态
cancelStatusstd::string&订单取消状态
outsideRthbool是否允许盘前盘后交易,仅适用于美股
canModifybool是否能修改
canCancelbool是否能取消
liquidationbool是否为平仓订单
namestd::string&标的名称
sourcestd::string&订单来源(from 'OpenApi', or other)
errorMsgstd::string&错误信息
attrDescstd::string&订单描述信息
commissionAndFeefloat佣金费用总计
openTimeuint64_t下单时间
timestampuint64_t订单状态最后更新时间
userMarkstd::string&订单备注
totalCashAmountdouble订单总金额
filledCashAmountdouble成交总金额

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

{
  "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;

参数

参数名类型描述
accountstd::string&需要订阅的 account id,不传则订阅所有关联的 account

返回

需要使用 push_client->set_transaction_changed_callback 响应返回结果

回调数据字段含义

字段类型描述
idint64_t订单执行ID
order_idint64_t订单号
accountstd::string&资金账号
symbolstd::string&持仓标的代码,如 'AAPL', '00700', 'ES', 'CN'
identifierstd::string&标的标识符。股票的identifier与symbol相同。期货的会带有合约月份,如 'CN2201'
multiplieruint32_t每手数量(期权、期货专有)
actionstd::string&买卖方向。BUY表示买入,SELL表示卖出。
marketstd::string&市场
currencystd::string&币种
segTypestd::string&按交易品种划分的分类。S表示股票,C表示期货
secTypestd::string&交易品种,标的类型。STK表示股票,FUT表示期货
filled_pricedouble价格
filled_quantityint64_t成交数量
create_timeuint64_tCreate time
update_timeuint64_tUpdate time
transact_timeuint64_t成交时间
timestampuint64_tTimestamp

示例

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,
}

上次编辑于: