行情订阅

行情订阅

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

connect() 是异步方法。连接回调在 TLS 握手完成且认证帧已发送后触发,但不等待服务端认证确认;当前 SDK 没有公开的认证完成回调。以下单项订阅示例均假设连接可用,返回的请求 ID 只表示请求已排队,最终结果必须解析订阅结果回调中的 msg()。

初始化推送客户端

#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 回调

限制:当前 C++ SDK 仅公开 subscribe_future_quote,没有对应的期货行情取消订阅方法。unsubscribe_quote 发送的是股票行情数据类型,不能替代期货退订。如需停止该流,请断开连接;这也会终止该连接上的其他订阅。

参数

参数名类型是否必填描述
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)

说明

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

限制:当前 C++ SDK 仅公开 subscribe_option_quote,没有对应的期权行情取消订阅方法。unsubscribe_quote 发送的是股票行情数据类型,不能替代期权退订。如需停止该流,请断开连接;这也会终止该连接上的其他订阅。

参数

参数名类型是否必填描述
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)

说明

订阅深度行情(买卖盘口)推送。美股深度行情推送频率为 300ms,港股深度行情推送频率为 2s。

参数

参数名类型是否必填描述
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"
}

取消订阅

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

订阅逐笔成交

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

说明

订阅逐笔成交数据推送。逐笔成交每 200ms 推送一次,采用快照模式,每次推送最新的 50 条逐笔记录。

参数

参数名类型是否必填描述
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"
        }
    ]
}

普通 TradeTick 回调中的 cond 是 SDK 解码后的名称,详见逐笔成交条件。完整 TickData 回调中的 cond 是单字符原始代码或空值,详见逐笔成交条件。


取消订阅

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 = {})

说明

订阅股票排行榜数据推送。服务端每 30 秒执行一次榜单推送任务,榜单条数可配置,默认 10 条。

参数

参数名类型是否必填描述
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::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::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::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");

订阅数字货币行情

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 "cpprest/json.h"
#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;
    });

    // code() 是响应类型 112;业务成功与否位于 msg() 的 JSON 中
    push_client->set_subscribe_callback([](const tigeropen::push::pb::Response& resp) {
        try {
            const auto result = web::json::value::parse(
                utility::conversions::to_string_t(resp.msg()));
            const bool success = result.has_field(U("code"))
                && result.at(U("code")).as_integer() == 0;
            std::cout << (success ? "Subscribe succeeded: " : "Subscribe failed: ")
                      << resp.msg() << std::endl;
        } catch (const std::exception&) {
            // Some failures return plain text instead of the normal JSON result.
            std::cout << "Subscribe failed: " << resp.msg() << 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;
    });

    // 连接是异步的;此 SDK 不公开服务端认证完成回调,参见本页说明
    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_callback、set_unsubscribe_callback 或查询回调接收响应,并以 Response.id() 关联。对于订阅和退订完成响应,外层 Response.code() 的 112/113 是响应类型标识,不是业务结果;正常情况下解析 Response.msg() 中的 JSON,并以其中的 code == 0 判断成功。异常路径可能返回纯文本,因此解析时应捕获异常并直接记录 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 包含 symbol、timestamp、askPrice、askSize、bidPrice、bidSize 等 Protobuf 字段。

示例

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

Did this page help you?