Skip to content

Latest commit

 

History

History
486 lines (368 loc) · 13.8 KB

File metadata and controls

486 lines (368 loc) · 13.8 KB

Tiger OpenAPI C++ SDK

Version License

Tiger OpenAPI C++ SDK is the official C++ client for Tiger Brokers Open Platform, providing trading, market data, and WebSocket push functionalities.

English | 中文

Features

  • 🚀 Complete trading API support (place orders, cancel orders, query positions, etc.)
  • 📊 Real-time market data (K-line, quotes, tick-by-tick trades, etc.)
  • 🔌 WebSocket push notifications (positions, orders, real-time quotes)
  • ♻️ Persistent HTTP client reuses its built-in connection pool
  • 🌐 Cross-platform support (macOS, Linux, Windows)
  • 🛡️ Type-safe C++ API design
  • 📦 Static and dynamic library compilation support

Requirements

Platform Compiler CMake Package Manager
macOS Clang (Xcode) 3.15+ Homebrew + Source
Linux GCC/G++ 3.15+ apt + Source
Windows MSVC 2019/2022 3.15+ vcpkg

Core Dependencies

  • Boost 1.86.0 (system, thread, log, program_options, chrono, filesystem)
  • OpenSSL 1.0.1+ (3.x recommended)
  • cpprestsdk Latest (HTTP client)
  • Protobuf 5.28.3
  • Abseil 20240722.0 (pinned C++14-compatible Protobuf runtime dependency)
  • C++ Standard: C++14 minimum build baseline; C++17 consumer projects are supported

Quick Start

One-Click Build (Recommended)

macOS/Linux

# Grant execute permission (first time only)
chmod +x scripts/build_linux_mac.sh

# Default build (Debug mode, auto-install dependencies and run demo)
./scripts/build_linux_mac.sh

# Release mode build
BUILD_TYPE=Release ./scripts/build_linux_mac.sh

# Build SDK only, skip demo
SKIP_DEMO=1 ./scripts/build_linux_mac.sh

The script will automatically:

  1. Install system dependencies (Homebrew/apt)
  2. Compile Boost, cpprestsdk, Abseil 20240722.0, and Protobuf
  3. Build SDK (Debug + Release)
  4. Build and run demo

Windows

# Build SDK + demo (x64 Release MD)
powershell -ExecutionPolicy Bypass -File scripts\build_windows.ps1 `
    -Triplet x64-windows -BuildType Release -Runtime MD

# Rebuild all 8 Windows precompiled zip packages
powershell -ExecutionPolicy Bypass -File scripts\package_windows.ps1

Using Pre-compiled Libraries

If you don't want to build from source, use the pre-compiled libraries in the output/ directory:

output/
├── Mac/
│   ├── Debug.zip      # macOS Debug static library + headers
│   └── Release.zip    # macOS Release static library + headers
├── Linux/
│   ├── Debug/
│   └── Release/
└── Windows/
    ├── readme.md
    ├── x64/
    │   ├── Debug-MD.zip
    │   ├── Debug-MT.zip
    │   ├── Release-MD.zip
    │   └── Release-MT.zip
    └── Win32/
        ├── Debug-MD.zip
        ├── Debug-MT.zip
        ├── Release-MD.zip
        └── Release-MT.zip

Manual Build

macOS

1. Install Basic Tools

brew install cmake wget automake libtool pkg-config openssl@3

# The one-click script builds Protobuf v5.28.3 and pinned Abseil 20240722.0
# from source. Do not install or use the latest Homebrew Abseil separately.

2. Install Boost

cd /usr/local
wget https://archives.boost.io/release/1.86.0/source/boost_1_86_0.tar.bz2
tar --bzip2 -xf boost_1_86_0.tar.bz2
cd boost_1_86_0
./bootstrap.sh
./b2 headers
./b2 -j $(sysctl -n hw.ncpu)

3. Build cpprestsdk

cd /tmp
git clone https://github.com/microsoft/cpprestsdk.git
cd cpprestsdk
git submodule update --init
cmake -S . -B build \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_INSTALL_PREFIX=/usr/local/opt/cpprestsdk \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_SAMPLES=OFF \
    -DBUILD_TESTS=OFF \
    -DBOOST_ROOT=/usr/local/boost_1_86_0 \
    -DOPENSSL_ROOT_DIR=$(brew --prefix openssl@3) \
    -DCMAKE_CXX_FLAGS="-Wno-error=null-pointer-subtraction -w"
cmake --build build -- -j $(sysctl -n hw.ncpu)
sudo cmake --install build

4. Build Abseil

cd /tmp
git clone --depth 1 --branch 20240722.0 https://github.com/abseil/abseil-cpp.git
cd abseil-cpp
cmake -S . -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_STANDARD=14 \
    -DCMAKE_INSTALL_PREFIX=/usr/local/opt/abseil-20240722.0 \
    -DABSL_BUILD_TESTING=OFF \
    -DABSL_PROPAGATE_CXX_STD=ON \
    -DBUILD_SHARED_LIBS=ON
cmake --build build -- -j $(sysctl -n hw.ncpu)
sudo cmake --install build

5. Build Protobuf

cd /tmp
curl -sL https://github.com/protocolbuffers/protobuf/releases/download/v28.3/protobuf-28.3.tar.gz -o protobuf-28.3.tar.gz
tar xzf protobuf-28.3.tar.gz
cd protobuf-28.3
cmake -S . -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr/local/opt/protobuf-v5.28.3 \
    -DCMAKE_CXX_STANDARD=14 \
    -Dprotobuf_BUILD_TESTS=OFF \
    -Dprotobuf_ABSL_PROVIDER=package \
    -Dabsl_DIR=/usr/local/opt/abseil-20240722.0/lib/cmake/absl \
    -DCMAKE_PREFIX_PATH=/usr/local/opt/abseil-20240722.0
cmake --build build -- -j $(sysctl -n hw.ncpu)
sudo cmake --install build

6. Build SDK

cd <project_root>

cmake -S . -B build/Debug \
    -DCMAKE_BUILD_TYPE=Debug \
    -DCMAKE_INSTALL_PREFIX=/usr/local/opt/tigerapi/Debug \
    -DBOOST_ROOT=/usr/local/boost_1_86_0 \
    -DCMAKE_PREFIX_PATH="/usr/local/opt/cpprestsdk;/usr/local/opt/abseil-20240722.0;/usr/local/opt/protobuf-v5.28.3" \
    -DOPENSSL_ROOT_DIR=$(brew --prefix openssl@3)
cmake --build build/Debug -- -j $(sysctl -n hw.ncpu)
sudo cmake --install build/Debug

7. Build Demo

cd demo
cmake -S . -B build \
    -DCMAKE_BUILD_TYPE=Debug \
    -DBOOST_ROOT=/usr/local/boost_1_86_0 \
    -DTIGERAPI_INCLUDE_DIR=/usr/local/opt/tigerapi/Debug/include \
    -DTIGERAPI_LIBRARY=/usr/local/opt/tigerapi/Debug/lib/libtigerapi.a \
    -DCPPREST_INCLUDE_DIR=/usr/local/opt/cpprestsdk/include \
    -DCPPREST_LIBRARY=/usr/local/opt/cpprestsdk/lib/libcpprest.dylib \
    -DProtobuf_INCLUDE_DIR=/usr/local/opt/protobuf-v5.28.3/include \
    -DProtobuf_LIBRARY=/usr/local/opt/protobuf-v5.28.3/lib/libprotobuf.dylib \
    -DOPENSSL_ROOT_DIR=$(brew --prefix openssl@3)
cmake --build build -- -j $(sysctl -n hw.ncpu)

# Run demo (set dynamic library path)
export DYLD_LIBRARY_PATH="/usr/local/opt/cpprestsdk/lib:/usr/local/opt/protobuf-v5.28.3/lib:$DYLD_LIBRARY_PATH"
./build/openapi_cpp_test

Linux (Ubuntu)

1. Install System Dependencies

sudo apt-get update
sudo apt-get install -y git wget bzip2 unzip gcc g++ libtool automake autoconf \
    build-essential cmake libssl-dev libabsl-dev zlib1g-dev pkg-config

2-6. Build Steps

Similar to macOS, main differences:

  • Remove OPENSSL_ROOT_DIR parameter (OpenSSL is in system path on Linux)
  • Use LD_LIBRARY_PATH instead of DYLD_LIBRARY_PATH
export LD_LIBRARY_PATH="/usr/local/opt/cpprestsdk/lib:/usr/local/opt/protobuf-v5.28.3/lib:$LD_LIBRARY_PATH"

Windows

Windows platform provides two build methods:

Method 1: Using Visual Studio (Recommended)

  1. Prerequisites

    • Visual Studio 2019 or 2022
    • Install "Desktop development with C++" workload
  2. Prepare Dependencies

    The project uses vcpkg for dependency management, supporting two approaches:

    Automatic Installation (Recommended):

    # vcpkg will automatically install dependencies based on vcpkg.json
    # Triggered automatically when opening the solution for the first time

    Manual Installation:

    git clone https://github.com/microsoft/vcpkg .vcpkg
    .\.vcpkg\bootstrap-vcpkg.bat -disableMetrics
    .\.vcpkg\vcpkg install boost:x64-windows openssl:x64-windows cpprestsdk:x64-windows protobuf:x64-windows
  3. Open and Build

    • Double-click to open openapi-cpp-sdk.sln
    • Select configuration (recommended: Release-MD|x64)
    • Right-click solution → Build Solution
  4. Configuration Options

    The solution provides 8 configuration combinations:

    Configuration Platform Runtime Library Description
    Debug-MD x64/Win32 /MDd Debug + Dynamic runtime
    Debug-MT x64/Win32 /MTd Debug + Static runtime
    Release-MD x64/Win32 /MD Release + Dynamic runtime (Recommended)
    Release-MT x64/Win32 /MT Release + Static runtime
  5. Build Artifacts

    output/Windows/
    ├── x64/
    │   ├── Release-MD/
    │   │   ├── openapi-cpp-sdk.dll
    │   │   ├── openapi-cpp-sdk.lib
    │   │   └── openapi_cpp_test.exe
    │   └── Release-MT/
    └── Win32/
        └── ...
    
  6. Dependency Path Configuration

    Project files are pre-configured with dependency paths:

    • Boost: .deps/boost/boost_1_86_0 (can be overridden by BOOST_ROOT environment variable)
    • vcpkg dependencies: vcpkg_installed/x64-windows/ (automatically managed)
    • SDK headers: include/
  7. Package all Windows zips

    .\scripts\package_windows.bat
    
    # Re-compress existing output directories without rebuilding
    .\scripts\package_windows.bat -SkipBuild

Method 2: Using CMake Command Line

  1. Install vcpkg
git clone https://github.com/microsoft/vcpkg .vcpkg
.\.vcpkg\bootstrap-vcpkg.bat -disableMetrics
  1. Install Dependencies
.\.vcpkg\vcpkg install boost:x64-windows openssl:x64-windows cpprestsdk:x64-windows protobuf:x64-windows
  1. Build SDK
$env:VCPKG_ROOT = "$PWD\.vcpkg"
$toolchain = "$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake"

cmake -S . -B build\windows -A x64 `
    -DCMAKE_BUILD_TYPE=Release `
    -DCMAKE_TOOLCHAIN_FILE=$toolchain `
    -DVCPKG_TARGET_TRIPLET=x64-windows `
    -DCMAKE_INSTALL_PREFIX=output\Windows\x64\Release-MD

cmake --build build\windows --config Release
cmake --install build\windows --config Release
  1. Build Demo
cmake -S demo -B demo\build\windows -A x64 `
    -DCMAKE_BUILD_TYPE=Release `
    -DCMAKE_TOOLCHAIN_FILE=$toolchain `
    -DVCPKG_TARGET_TRIPLET=x64-windows `
    -DTIGERAPI_INCLUDE_DIR=output\Windows\x64\Release-MD\include `
    -DTIGERAPI_LIBRARY=output\Windows\x64\Release-MD\lib\tigerapi.lib

cmake --build demo\build\windows --config Release --target openapi_cpp_test

Usage Examples

HTTP Market Data API

#include "tigerapi/quote_client.h"

TigerClient client;
client.init("your_tiger_id", "your_private_key", "your_account");

// Get K-line data
std::vector<std::string> symbols = {"AAPL", "TSLA"};
ResponseModel response = client.grab_quote_kline(symbols, "day", "2024-01-01", "2024-12-31");

WebSocket Push Notifications

#include "tigerapi/push_client.h"

// Create push client
PushClient push_client(tiger_id, private_key, "your_language");

// Subscribe to position updates
push_client.subscribe_position();

// Subscribe to quote updates
push_client.subscribe_quote({"AAPL", "TSLA"});

// Connect and receive push notifications
push_client.connect();

For more examples, see demo/openapi_cpp_test/openapi_cpp_test.cpp.

Project Structure

openapi-cpp-sdk/
├── include/                    # Header files
│   ├── tigerapi/              # SDK public headers
│   ├── cpprest/               # cpprestsdk headers
│   ├── google/protobuf/       # Protobuf 5.28.3 headers
│   └── openapi_pb/            # Protobuf generated message definitions
├── src/                       # SDK source implementation
├── demo/                      # Example code
│   └── openapi_cpp_test/
├── scripts/                   # Build scripts
│   ├── build_linux_mac.sh    # macOS/Linux one-click build
│   ├── build_windows.ps1     # Windows CMake build
│   ├── package_windows.ps1   # Windows zip packaging
│   └── package_windows.bat   # Execution-policy-safe Windows packaging wrapper
├── output/                    # Build artifacts
│   ├── Mac/
│   ├── Linux/
│   └── Windows/
└── CMakeLists.txt            # CMake configuration

Troubleshooting

macOS

Q: OpenSSL not found

# Specify OpenSSL path
-DOPENSSL_ROOT_DIR=$(brew --prefix openssl@3)

Q: Architecture mismatch (arm64 vs x86_64)

Modify architecture parameter in CMakeLists.txt and demo/CMakeLists.txt:

# ARM Mac (default)
set(CMAKE_CXX_FLAGS "-arch arm64 -std=c++14 ...")

# Intel Mac
set(CMAKE_CXX_FLAGS "-arch x86_64 -std=c++14 ...")

Q: Dynamic library not found at runtime

export DYLD_LIBRARY_PATH="/usr/local/opt/cpprestsdk/lib:/usr/local/opt/protobuf-v5.28.3/lib:$DYLD_LIBRARY_PATH"

Linux

Q: .so file not found at runtime

export LD_LIBRARY_PATH="/usr/local/opt/cpprestsdk/lib:/usr/local/opt/protobuf-v5.28.3/lib:$LD_LIBRARY_PATH"

Windows

Q: vcpkg bootstrap failed

Ensure Visual Studio "Desktop development with C++" workload is installed.

Q: Runtime library mismatch (MT vs MD)

Ensure SDK and demo use the same -DCMAKE_MSVC_RUNTIME_LIBRARY setting.

All Platforms

Q: Protobuf version conflict

Use Protobuf 5.28.3 with pinned Abseil 20240722.0 to retain C++14 support.

Q: Warnings treated as errors

# Add compile flag to disable warnings
-DCMAKE_CXX_FLAGS="-w"

Changelog

See CHANGELOG.md for version history.

License

Apache License 2.0

Related Links

Support

For questions or issues: