行情订阅推送

行情订阅推送

C++ SDK 通过 IPushClient 实现行情的订阅推送功能。推送使用 Protobuf 协议进行数据传输,回调函数异步触发。

初始化推送客户端

#include "tigerapi/push_client.h"
#include "tigerapi/client_config.h"

using namespace TIGER_API;

ClientConfig config(false, U("your_config_directory_path"));
auto push_client = IPushClient::create_push_client(config);

// 设置连接/断开回调
push_client->set_connected_callback([]() {
    std::cout << "Connected" << std::endl;
});

push_client->set_disconnected_callback([]() {
    std::cout << "Disconnected" << std::endl;
});

// 设置错误回调
push_client->set_inner_error_callback([](std::string err) {
    std::cout << "Error: " << err << std::endl;
});

// 建立连接
push_client->connect();

订阅股票行情

unsigned int IPushClient::subscribe_quote(
    const std::vector<std::string> &symbols)

说明

订阅股票的实时行情推送

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes标的代码列表

回调设置

使用 set_quote_changed_callback 设置行情变动回调:

push_client->set_quote_changed_callback([](const tigeropen::push::pb::QuoteBasicData& data) {
    std::cout << "Symbol: " << data.symbol()
              << " Price: " << data.latestprice()
              << " Volume: " << data.volume()
              << std::endl;
});

示例

std::vector<std::string> symbols = {"AAPL", "TSLA"};
push_client->subscribe_quote(symbols);

回调数据示例

基本行情(BASIC):

{
    "symbol": "AAPL",
    "type": "BASIC",
    "timestamp": "1684766012120",
    "serverTimestamp": "1684766012129",
    "avgPrice": 174.1721,
    "latestPrice": 174.175,
    "latestPriceTimestamp": "1684766011918",
    "latestTime": "05-22 10:33:31 EDT",
    "preClose": 175.16,
    "volume": "12314802",
    "amount": 2144365591.41,
    "open": 173.98,
    "high": 174.71,
    "low": 173.45,
    "marketStatus": "Trading",
    "mi": {
        "p": 174.175,
        "a": 174.1721,
        "t": "1684765980000",
        "v": "57641",
        "o": 174.21,
        "h": 174.22,
        "l": 174.14
    }
}

最优报价(BBO):

{
    "symbol": "AAPL",
    "type": "BBO",
    "timestamp": "1676992715509",
    "askPrice": 149.96,
    "askSize": "200",
    "askTimestamp": "1676992715367",
    "bidPrice": 149.94,
    "bidSize": "700",
    "bidTimestamp": "1676992715367"
}

取消订阅股票行情

unsigned int IPushClient::unsubscribe_quote(
    const std::vector<std::string> &symbols)

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes标的代码列表

订阅期货行情

unsigned int IPushClient::subscribe_future_quote(
    const std::vector<std::string> &symbols)

说明

订阅期货合约的实时行情推送,使用与股票行情相同的 set_quote_changed_callback 回调

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes期货合约代码列表,如 {"CL2312"}

回调数据示例

{
    "symbol": "ESmain",
    "type": "BASIC",
    "timestamp": "1684766824130",
    "avgPrice": 4206.476,
    "latestPrice": 4202.5,
    "preClose": 4204.75,
    "volume": "557570",
    "open": 4189,
    "high": 4221.75,
    "low": 4186.5,
    "marketStatus": "Trading",
    "preSettlement": 4204.75,
    "minTick": 0.25
}

订阅期权行情

unsigned int IPushClient::subscribe_option_quote(
    const std::vector<std::string> &symbols)

说明

订阅期权合约的实时行情推送

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes期权标识符列表

回调数据示例

{
    "symbol": "AAPL 20230317 150.0 CALL",
    "type": "BASIC",
    "timestamp": "1676994444927",
    "latestPrice": 4.83,
    "preClose": 6.21,
    "volume": "3181",
    "amount": 939117.01,
    "open": 4.85,
    "high": 5.6,
    "low": 4.64,
    "identifier": "AAPL  230317C00150000",
    "openInt": "82677"
}

订阅深度行情

unsigned int IPushClient::subscribe_quote_depth(
    const std::vector<std::string> &symbols)

说明

订阅深度行情(买卖盘口)推送

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes标的代码列表

回调设置

push_client->set_quote_depth_changed_callback([](const tigeropen::push::pb::QuoteDepthData& data) {
    std::cout << "Depth data received" << std::endl;
});

回调数据示例

{
    "symbol": "AAPL",
    "timestamp": "1676993368405",
    "ask": {
        "price": [149.69, 149.70, 149.71],
        "volume": ["100", "200", "185"]
    },
    "bid": {
        "price": [149.68, 149.67, 149.66],
        "volume": ["84", "100", "100"]
    }
}

取消订阅深度行情

unsigned int IPushClient::unsubscribe_quote_depth(
    const std::vector<std::string> &symbols)

订阅 K 线

unsigned int IPushClient::subscribe_kline(
    const std::vector<std::string> &symbols)

说明

订阅 K 线数据推送

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes标的代码列表

回调设置

push_client->set_kline_changed_callback([](const tigeropen::push::pb::KlineData& data) {
    std::cout << "Kline data received" << std::endl;
});

回调数据示例

{
    "symbol": "AAPL",
    "time": "1712584560000",
    "open": 168.9779,
    "high": 169.0015,
    "low": 168.9752,
    "close": 169.0,
    "avg": 168.778,
    "volume": "3664",
    "count": 114,
    "amount": 617820.6508,
    "serverTimestamp": "1712584569746"
}

取消订阅 K 线

unsigned int IPushClient::unsubscribe_kline(
    const std::vector<std::string> &symbols)

订阅逐笔成交

unsigned int IPushClient::subscribe_tick(
    const std::vector<std::string> &symbols)

说明

订阅逐笔成交数据推送

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes标的代码列表

回调设置

// 使用 TradeTick 对象回调
push_client->set_tick_changed_callback([](const TradeTick& data) {
    std::cout << "Symbol: " << data.symbol << " ticks: " << data.ticks.size() << std::endl;
});

// 或使用完整 Protobuf TickData 回调
push_client->set_full_tick_changed_callback([](const tigeropen::push::pb::TickData& data) {
    std::cout << "Full tick data received" << std::endl;
});

回调数据示例

{
    "symbol": "AAPL",
    "secType": "STK",
    "quoteLevel": "usQuoteBasic",
    "timestamp": 1676993925700,
    "ticks": [
        {
            "sn": 116202,
            "volume": 50,
            "tickType": "*",
            "price": 149.665,
            "time": 1676993924289,
            "cond": "US_REGULAR_SALE"
        }
    ]
}

取消订阅逐笔成交

unsigned int IPushClient::unsubscribe_tick(
    const std::vector<std::string> &symbols)

订阅整个市场

unsigned int IPushClient::subscribe_market(const std::string &market)

说明

订阅整个市场的行情推送

参数

参数名类型是否必填描述
marketstringYes市场,如 "US"、"HK"

取消订阅整个市场

unsigned int IPushClient::unsubscribe_market(const std::string &market)


订阅股票排行榜

unsigned int IPushClient::subscribe_stock_top(
    const std::string &market,
    const std::vector<std::string> &indicators = {})

说明

订阅股票排行榜数据推送

参数

参数名类型是否必填描述
marketstringYes市场,如 "US"、"HK"
indicatorsstd::vector of std::stringNo排行指标列表,默认空

回调设置

push_client->set_stock_top_changed_callback([](const tigeropen::push::pb::StockTopData& data) {
    std::cout << "Stock top data received" << std::endl;
});

回调数据示例

{
    "market": "US",
    "timestamp": "1687271010482",
    "topData": [
        {
            "targetName": "changeRate",
            "item": [
                {"symbol": "ICAD", "latestPrice": 1.63, "targetValue": 0.393162}
            ]
        },
        {
            "targetName": "volume",
            "item": [
                {"symbol": "TSLA", "latestPrice": 263.21, "targetValue": 40190416}
            ]
        }
    ]
}

订阅期权排行榜

unsigned int IPushClient::subscribe_option_top(
    const std::string &market,
    const std::vector<std::string> &indicators = {})

说明

订阅期权排行榜数据推送

参数

参数名类型是否必填描述
marketstringYes市场
indicatorsstd::vector of std::stringNo排行指标列表

回调设置

push_client->set_option_top_changed_callback([](const tigeropen::push::pb::OptionTopData& data) {
    std::cout << "Option top data received" << std::endl;
});

回调数据示例

{
    "market": "US",
    "timestamp": "1687277160445",
    "topData": [
        {
            "targetName": "volume",
            "item": [
                {
                    "symbol": "SPY",
                    "expiry": "20230620",
                    "strike": "435.0",
                    "right": "PUT",
                    "totalVolume": 212478
                }
            ]
        }
    ]
}

订阅数字货币行情

unsigned int IPushClient::subscribe_cc(
    const std::vector<std::string> &symbols)

说明

订阅数字货币的实时行情推送

参数

参数名类型是否必填描述
symbolsstd::vector of std::stringYes数字货币代码列表

取消订阅数字货币行情

unsigned int IPushClient::unsubscribe_cc(
    const std::vector<std::string> &symbols)

查询已订阅标的

unsigned int IPushClient::query_subscribed_symbols()

说明

查询当前已订阅的所有标的列表

回调设置

push_client->set_query_subscribed_symbols_changed_callback([](const tigeropen::push::pb::Response& resp) {
    std::cout << "Subscribed symbols: " << resp.DebugString() << std::endl;
});

push_client->query_subscribed_symbols();

完整示例

#include <iostream>
#include <thread>
#include <chrono>
#include "tigerapi/push_client.h"
#include "tigerapi/client_config.h"

using namespace TIGER_API;

int main() {
    ClientConfig config(false, U("your_config_directory_path"));
    auto push_client = IPushClient::create_push_client(config);

    // 连接回调
    push_client->set_connected_callback([]() {
        std::cout << "Connected" << std::endl;
    });

    push_client->set_disconnected_callback([]() {
        std::cout << "Disconnected" << std::endl;
    });

    // 订阅成功/失败回调
    push_client->set_subscribe_callback([](const tigeropen::push::pb::Response& resp) {
        std::cout << "Subscribe result: " << resp.DebugString() << std::endl;
    });

    // 行情变动回调
    push_client->set_quote_changed_callback([](const tigeropen::push::pb::QuoteBasicData& data) {
        std::cout << "Symbol: " << data.symbol()
                  << " Price: " << data.latestprice()
                  << std::endl;
    });

    // 逐笔成交回调
    push_client->set_tick_changed_callback([](const TradeTick& data) {
        std::cout << "Tick: " << data.symbol << " count: " << data.ticks.size() << std::endl;
    });

    // 连接
    push_client->connect();

    // 订阅
    std::vector<std::string> symbols = {"AAPL", "TSLA"};
    push_client->subscribe_quote(symbols);
    push_client->subscribe_tick(symbols);

    // 等待推送
    std::this_thread::sleep_for(std::chrono::seconds(60));

    // 取消订阅并断开
    push_client->unsubscribe_quote(symbols);
    push_client->unsubscribe_tick(symbols);
    push_client->disconnect();

    return 0;
}

请求 ID 与订阅结果

所有 subscribe_*unsubscribe_*query_subscribed_symbols 方法立即返回 unsigned int 请求 ID。通过 set_subscribe_callbackset_unsubscribe_callback 或查询回调接收最终结果,并以 Response.id() 关联;Response.code()Response.msg() 表示服务端结果。详见其他订阅事件

行情数据回调签名

set_quote_bbo_changed_callback 设置最优买卖价回调

void IPushClient::set_quote_bbo_changed_callback(
    const std::function<void(const tigeropen::push::pb::QuoteBBOData&)> &cb)

说明

注册最优买价/卖价(BBO)变动回调。该方法只设置处理函数,不发起订阅;BBO 数据由对应行情订阅和账户权限决定。

参数

参数名类型是否必填描述
cbstd::function callbackYes接收 const tigeropen::push::pb::QuoteBBOData&;返回 void

返回

void。根据生成的 QuoteBBOData.pb.h,模型包含 symboltimestampaskPriceaskSizebidPricebidSize 等 Protobuf 字段;以当前生成头文件为准。

示例

push_client->set_quote_bbo_changed_callback(
    [](const tigeropen::push::pb::QuoteBBOData& data) {
        std::cout << data.symbol() << " "
                  << data.bidprice() << " / " << data.askprice() << '\n';
    });

取消股票榜单

unsigned int IPushClient::unsubscribe_stock_top(
    const std::string &market,
    const std::vector<std::string> &indicators = {})

传入与订阅相同的市场和指标;返回请求 ID。

const unsigned int request_id = push_client->unsubscribe_stock_top("US", {"changeRate"});

取消期权榜单

unsigned int IPushClient::unsubscribe_option_top(
    const std::string &market,
    const std::vector<std::string> &indicators = {})

返回请求 ID,最终结果由取消订阅回调确认。

const unsigned int request_id = push_client->unsubscribe_option_top("US");

Did this page help you?