|
1 | | -// fpss_smoke.cpp -- C++ FPSS smoke test. |
| 1 | +// fpss_smoke.cpp -- C++ FPSS smoke test driven by the callback C ABI. |
2 | 2 | // |
3 | | -// The poll-based `tdx::FpssClient::next_event` API was removed in |
4 | | -// issue #482 (PR B) along with the underlying `tdx_fpss_next_event` |
5 | | -// C ABI symbol. This example will be rewritten to drive the new |
6 | | -// callback API (`tdx_fpss_set_callback` / `tdx_fpss_set_inline_callback`) |
7 | | -// when the C++ wrapper migration ships in PR E. |
8 | | -// |
9 | | -// Compiling this file in its current form is intentional only after |
10 | | -// the C++ wrapper migration lands; until then it is a static breakage |
11 | | -// signal so downstream consumers do not silently miss the API change. |
| 3 | +// Subscribes to a stock and an option contract, registers a queued |
| 4 | +// callback, prints every event for a few seconds, then exits cleanly. |
| 5 | + |
| 6 | +#include <atomic> |
| 7 | +#include <chrono> |
| 8 | +#include <iostream> |
| 9 | +#include <mutex> |
| 10 | +#include <stdexcept> |
| 11 | +#include <string> |
| 12 | +#include <thread> |
| 13 | + |
| 14 | +#include "thetadx.hpp" |
| 15 | + |
| 16 | +namespace { |
| 17 | + |
| 18 | +constexpr const char* kSymbol = "AAPL"; |
| 19 | +constexpr const char* kOptionSymbol = "SPY"; |
| 20 | +constexpr const char* kExpiration = "20260417"; |
| 21 | +constexpr const char* kStrike = "550"; |
| 22 | +constexpr const char* kRight = "C"; |
| 23 | + |
| 24 | +constexpr auto kCollectFor = std::chrono::seconds(5); |
| 25 | +constexpr int kMaxEventsPrinted = 25; |
| 26 | + |
| 27 | +const char* event_kind_name(tdx::FpssEventKind kind) { |
| 28 | + switch (kind) { |
| 29 | + case TDX_FPSS_QUOTE: return "quote"; |
| 30 | + case TDX_FPSS_TRADE: return "trade"; |
| 31 | + case TDX_FPSS_OPEN_INTEREST: return "open_interest"; |
| 32 | + case TDX_FPSS_OHLCVC: return "ohlcvc"; |
| 33 | + case TDX_FPSS_CONTROL: return "control"; |
| 34 | + case TDX_FPSS_RAW_DATA: return "raw_data"; |
| 35 | + } |
| 36 | + return "unknown"; |
| 37 | +} |
| 38 | + |
| 39 | +} // namespace |
| 40 | + |
| 41 | +int main(int argc, char** argv) { |
| 42 | + const std::string creds_path = (argc > 1) ? argv[1] : "creds.txt"; |
| 43 | + try { |
| 44 | + auto creds = tdx::Credentials::from_file(creds_path); |
| 45 | + auto config = tdx::Config::production(); |
| 46 | + |
| 47 | + tdx::FpssClient fpss(creds, config); |
| 48 | + |
| 49 | + std::atomic<int> total_events{0}; |
| 50 | + std::atomic<int> data_events{0}; |
| 51 | + std::mutex print_mtx; |
| 52 | + |
| 53 | + fpss.set_callback([&](const tdx::FpssEvent& event) { |
| 54 | + const int seq = total_events.fetch_add(1, std::memory_order_relaxed); |
| 55 | + if (event.kind != TDX_FPSS_CONTROL && event.kind != TDX_FPSS_RAW_DATA) { |
| 56 | + data_events.fetch_add(1, std::memory_order_relaxed); |
| 57 | + } |
| 58 | + if (seq >= kMaxEventsPrinted) return; |
| 59 | + std::lock_guard<std::mutex> guard(print_mtx); |
| 60 | + std::cout << "[" << seq << "] kind=" << event_kind_name(event.kind); |
| 61 | + switch (event.kind) { |
| 62 | + case TDX_FPSS_QUOTE: |
| 63 | + std::cout << " contract_id=" << event.quote.contract_id |
| 64 | + << " bid=" << event.quote.bid |
| 65 | + << " ask=" << event.quote.ask; |
| 66 | + break; |
| 67 | + case TDX_FPSS_TRADE: |
| 68 | + std::cout << " contract_id=" << event.trade.contract_id |
| 69 | + << " price=" << event.trade.price |
| 70 | + << " size=" << event.trade.size; |
| 71 | + break; |
| 72 | + case TDX_FPSS_OPEN_INTEREST: |
| 73 | + std::cout << " contract_id=" << event.open_interest.contract_id |
| 74 | + << " open_interest=" << event.open_interest.open_interest; |
| 75 | + break; |
| 76 | + case TDX_FPSS_OHLCVC: |
| 77 | + std::cout << " contract_id=" << event.ohlcvc.contract_id |
| 78 | + << " close=" << event.ohlcvc.close; |
| 79 | + break; |
| 80 | + case TDX_FPSS_CONTROL: |
| 81 | + std::cout << " control_kind=" << event.control.kind; |
| 82 | + if (event.control.detail) std::cout << " detail=" << event.control.detail; |
| 83 | + break; |
| 84 | + case TDX_FPSS_RAW_DATA: |
| 85 | + std::cout << " code=" << static_cast<int>(event.raw_data.code) |
| 86 | + << " len=" << event.raw_data.payload_len; |
| 87 | + break; |
| 88 | + } |
| 89 | + std::cout << std::endl; |
| 90 | + }); |
| 91 | + |
| 92 | + if (fpss.subscribe_quotes(kSymbol) < 0) { |
| 93 | + throw std::runtime_error("subscribe_quotes failed"); |
| 94 | + } |
| 95 | + if (fpss.subscribe_trades(kSymbol) < 0) { |
| 96 | + throw std::runtime_error("subscribe_trades failed"); |
| 97 | + } |
| 98 | + if (fpss.subscribe_option_quotes(kOptionSymbol, kExpiration, kStrike, kRight) < 0) { |
| 99 | + throw std::runtime_error("subscribe_option_quotes failed"); |
| 100 | + } |
| 101 | + |
| 102 | + std::this_thread::sleep_for(kCollectFor); |
| 103 | + |
| 104 | + const int total = total_events.load(std::memory_order_relaxed); |
| 105 | + const int data = data_events.load(std::memory_order_relaxed); |
| 106 | + const uint64_t dropped = fpss.dropped_events(); |
| 107 | + |
| 108 | + std::cout << "summary: total=" << total |
| 109 | + << " data=" << data |
| 110 | + << " dropped=" << dropped << std::endl; |
12 | 111 |
|
13 | | -#error "fpss_smoke.cpp depends on the removed `next_event` poll API. Re-enable when the C++ wrapper migrates to the callback C ABI in PR E (refs #482)." |
| 112 | + fpss.shutdown(); |
| 113 | + return data > 0 ? 0 : 1; |
| 114 | + } catch (const std::exception& e) { |
| 115 | + std::cerr << "fpss_smoke error: " << e.what() << std::endl; |
| 116 | + return 2; |
| 117 | + } |
| 118 | +} |
0 commit comments