# C++ API 参考 C++ API 提供 RAII 包装器,具有现代 C++ 的便利性。 ## 头文件 ```cpp #include ``` ## 类 ### Indicator 基类 ```cpp namespace techkit { class Indicator { public: // Disable copy, enable move Indicator(const Indicator&) = delete; Indicator& operator=(const Indicator&) = delete; Indicator(Indicator&& other) noexcept; Indicator& operator=(Indicator&& other) noexcept; ~Indicator(); // Automatic cleanup // Update methods tk_result update(double value); tk_result update(const tk_ohlcv& bar); // Batch calculation std::vector calculate(const double* data, size_t len); // State management void reset(); bool isReady() const; int lookback() const; std::string name() const; // Access raw C handle tk_indicator raw() const; }; } // namespace techkit ``` ### 具体指标 ```cpp namespace techkit { // Moving Averages class SMA : public Indicator { public: explicit SMA(int period); }; class EMA : public Indicator { public: explicit EMA(int period); }; // Momentum class RSI : public Indicator { public: explicit RSI(int period = 14); }; class MACD : public Indicator { public: MACD(int fast = 12, int slow = 26, int signal = 9); tk_macd_result update_macd(double value); }; // Volatility class BollingerBands : public Indicator { public: BollingerBands(int period = 20, double std_dev = 2.0); tk_bbands_result update_bbands(double value); }; class ATR : public Indicator { public: explicit ATR(int period = 14); }; // ... all other indicators } // namespace techkit ``` ## 指标链 ```cpp #include namespace techkit { class Chain { public: Chain& then(std::unique_ptr ind); tk_result update(double value); void reset(); bool isReady() const; }; // 流畅 API 的工厂函数 template Chain chain(Args&&... args); } // namespace techkit ``` ### 用法 ```cpp // RSI smoothed by EMA auto chain = techkit::Chain(); chain.then(std::make_unique(14)) .then(std::make_unique(9)); for (double price : prices) { auto result = chain.update(price); if (result.valid) { std::cout << "Smoothed RSI: " << result.value << std::endl; } } ``` ## 示例 ```cpp #include #include #include int main() { // RAII - automatic cleanup techkit::SMA sma(20); techkit::RSI rsi(14); techkit::MACD macd(12, 26, 9); std::vector prices = {100.0, 101.5, 102.3, /* ... */}; for (double price : prices) { auto sma_r = sma.update(price); auto rsi_r = rsi.update(price); auto macd_r = macd.update_macd(price); if (sma_r.valid) { std::cout << "SMA: " << sma_r.value << ", RSI: " << rsi_r.value << ", MACD: " << macd_r.macd << std::endl; } } // Automatic cleanup when variables go out of scope return 0; } ```