Other Examples

Other Examples

Option Pricing Tools

The C++ SDK does not include a built-in option pricing library. However, you can use the SDK's market data interfaces to obtain option bid/ask prices, implied volatility, Greeks, and other data, and then use a third-party math library (such as QuantLib) for local option pricing calculations.

The following example demonstrates how to retrieve option market data using the C++ SDK and analyze the returned Greeks and implied volatility.

Note

For local option pricing and Greeks calculations, using the QuantLib C++ library is recommended. The local calculation portions in the examples below require QuantLib to be installed separately.

Get Option Quotes and Greeks via SDK

Use get_option_brief to get a real-time option quote snapshot. The returned data includes implied volatility and Greeks.

#include <iostream>
#include "tigerapi/quote_client.h"
#include "tigerapi/client_config.h"

using namespace TIGER_API;
using namespace web::json;

int main() {
    // Initialize configuration
    ClientConfig config(false, U("/path/to/your/properties/"));

    // Initialize quote client
    QuoteClient quote_client(config);

    // Get option quote snapshot (including Greeks)
    // Option identifier format: "Underlying  ExpiryDirectionStrike" (OCC format)
    value result = quote_client.get_option_brief(U("AAPL  240621C00190000"));
    ucout << result.serialize() << std::endl;

    return 0;
}

Return Example

{
  "askPrice": 35.5,
  "askSize": 10,
  "bidPrice": 35.0,
  "bidSize": 15,
  "latestPrice": 35.25,
  "volume": 1234,
  "openInterest": 5678,
  "impliedVol": 0.4648,
  "delta": 0.6006,
  "gamma": 0.0246,
  "theta": -0.4430,
  "vega": 0.1304,
  "rho": 0.0265
}

Get Option Chain Data

Use get_option_chain to get the complete option chain for a given underlying and expiration date, with optional filter conditions.

#include <iostream>
#include "tigerapi/quote_client.h"
#include "tigerapi/client_config.h"

using namespace TIGER_API;
using namespace web::json;

int main() {
    ClientConfig config(false, U("/path/to/your/properties/"));
    QuoteClient quote_client(config);

    // Get option expiration dates
    value symbols = value::array();
    symbols[0] = value::string(U("AAPL"));
    value expirations = quote_client.get_option_expiration(symbols);
    ucout << U("Expirations: ") << expirations.serialize() << std::endl;

    // Get option chain for a specific expiration date
    value chain = quote_client.get_option_chain(U("AAPL"), U("2024-06-21"));
    ucout << U("Option chain: ") << chain.serialize() << std::endl;

    // Get option chain with filter (in-the-money only)
    value filter = value::object();
    filter[U("in_the_money")] = value::boolean(true);
    value filtered_chain = quote_client.get_option_chain(U("AAPL"), U("2024-06-21"), filter);
    ucout << U("In-the-money options: ") << filtered_chain.serialize() << std::endl;

    return 0;
}

Local Option Pricing with QuantLib

If you need to perform local option pricing and Greeks calculations, you can use the QuantLib C++ library. The following example demonstrates calculating implied volatility and Greeks for American options using QuantLib.

QuantLib C++ library must be installed first. Install via vcpkg: vcpkg install quantlib, or via brew: brew install quantlib

#include <iostream>
#include <ql/quantlib.hpp>

using namespace QuantLib;

int main() {
    // Set evaluation date
    Date evaluationDate(19, April, 2022);
    Settings::instance().evaluationDate() = evaluationDate;

    // Option parameters
    Option::Type optionType = Option::Call;
    Real underlying = 985.0;       // Underlying asset price
    Real strike = 990.0;           // Strike price
    Rate riskFreeRate = 0.017;     // Risk-free rate
    Rate dividendRate = 0.0;       // Dividend rate
    Date settlementDate(14, April, 2022);   // Settlement date
    Date expirationDate(22, April, 2022);   // Option expiration date

    // Build exercise and underlying
    auto exercise = ext::make_shared<AmericanExercise>(settlementDate, expirationDate);
    auto payoff = ext::make_shared<PlainVanillaPayoff>(optionType, strike);
    VanillaOption option(payoff, exercise);

    // Market data handles
    auto spotHandle = ext::make_shared<SimpleQuote>(underlying);
    auto volHandle = ext::make_shared<SimpleQuote>(0.0); // Temporarily set to 0
    auto rateHandle = ext::make_shared<SimpleQuote>(riskFreeRate);
    auto divHandle = ext::make_shared<SimpleQuote>(dividendRate);

    DayCounter dayCounter = Actual365Fixed();
    Calendar calendar = UnitedStates(UnitedStates::NYSE);

    auto flatVol = ext::make_shared<BlackConstantVol>(
        evaluationDate, calendar,
        Handle<Quote>(volHandle), dayCounter);
    auto flatRate = ext::make_shared<FlatForward>(
        evaluationDate, Handle<Quote>(rateHandle), dayCounter);
    auto flatDiv = ext::make_shared<FlatForward>(
        evaluationDate, Handle<Quote>(divHandle), dayCounter);

    auto bsmProcess = ext::make_shared<BlackScholesMertonProcess>(
        Handle<Quote>(spotHandle),
        Handle<YieldTermStructure>(flatDiv),
        Handle<YieldTermStructure>(flatRate),
        Handle<BlackVolTermStructure>(flatVol));

    // Use finite difference pricing engine
    option.setPricingEngine(
        ext::make_shared<FdBlackScholesVanillaEngine>(bsmProcess, 100, 100));

    // Calculate implied volatility from option price
    Real optionPrice = 33.6148; // Option market price, can use (ask + bid) / 2
    Volatility impliedVol = option.impliedVolatility(optionPrice, bsmProcess);
    std::cout << "implied volatility: " << impliedVol << std::endl;

    // Update volatility and recalculate Greeks
    volHandle->setValue(impliedVol);

    std::cout << "value: " << option.NPV() << std::endl;
    std::cout << "delta: " << option.delta() << std::endl;
    std::cout << "gamma: " << option.gamma() << std::endl;
    std::cout << "theta: " << option.theta() << std::endl;
    std::cout << "vega: " << option.vega() << std::endl;
    std::cout << "rho: " << option.rho() << std::endl;

    std::cout << std::endl;

    // Calculate option price directly using known implied volatility
    Real knownVolatility = 0.6153;
    volHandle->setValue(knownVolatility);

    std::cout << "---- Calculate using known implied volatility ----" << std::endl;
    std::cout << "value: " << option.NPV() << std::endl;
    std::cout << "delta: " << option.delta() << std::endl;
    std::cout << "gamma: " << option.gamma() << std::endl;
    std::cout << "theta: " << option.theta() << std::endl;
    std::cout << "vega: " << option.vega() << std::endl;
    std::cout << "rho: " << option.rho() << std::endl;

    return 0;
}

Full Example: SDK Market Data + QuantLib Calculation

The following example combines Tiger Open API market data queries with QuantLib local calculations to implement a complete option analysis workflow.

#include <iostream>
#include <string>
#include "tigerapi/quote_client.h"
#include "tigerapi/client_config.h"
#include <ql/quantlib.hpp>

using namespace TIGER_API;
using namespace web::json;
using namespace QuantLib;

int main() {
    // 1. Get option market data via SDK
    ClientConfig config(false, U("/path/to/your/properties/"));
    QuoteClient quote_client(config);

    // Get real-time option quotes
    value brief = quote_client.get_option_brief(U("AAPL  240209P00185000"));
    ucout << U("Option brief: ") << brief.serialize() << std::endl;

    // Get underlying stock latest price
    value symbols = value::array();
    symbols[0] = value::string(U("AAPL"));
    value stock_brief = quote_client.get_brief(symbols);
    ucout << U("Stock brief: ") << stock_brief.serialize() << std::endl;

    // 2. Extract key parameters from returned data
    // Note: In actual code, you need to parse fields based on the returned JSON structure
    double askPrice = 2.50;   // From brief
    double bidPrice = 2.30;   // From brief
    double latestPrice = 185.0; // From stock_brief

    // 3. Perform local calculation using QuantLib
    Date evaluationDate(5, February, 2024);
    Settings::instance().evaluationDate() = evaluationDate;

    Option::Type optionType = Option::Put;
    Real underlying = latestPrice;
    Real strike = 185.0;
    Rate riskFreeRate = 0.0241;
    Rate dividendRate = 0.0;
    Date settlementDate(5, February, 2024);
    Date expirationDate(9, February, 2024);

    auto exercise = ext::make_shared<AmericanExercise>(settlementDate, expirationDate);
    auto payoff = ext::make_shared<PlainVanillaPayoff>(optionType, strike);
    VanillaOption option(payoff, exercise);

    auto spotHandle = ext::make_shared<SimpleQuote>(underlying);
    auto volHandle = ext::make_shared<SimpleQuote>(0.0);
    auto rateHandle = ext::make_shared<SimpleQuote>(riskFreeRate);
    auto divHandle = ext::make_shared<SimpleQuote>(dividendRate);

    DayCounter dayCounter = Actual365Fixed();
    Calendar calendar = UnitedStates(UnitedStates::NYSE);

    auto flatVol = ext::make_shared<BlackConstantVol>(
        evaluationDate, calendar,
        Handle<Quote>(volHandle), dayCounter);
    auto flatRate = ext::make_shared<FlatForward>(
        evaluationDate, Handle<Quote>(rateHandle), dayCounter);
    auto flatDiv = ext::make_shared<FlatForward>(
        evaluationDate, Handle<Quote>(divHandle), dayCounter);

    auto bsmProcess = ext::make_shared<BlackScholesMertonProcess>(
        Handle<Quote>(spotHandle),
        Handle<YieldTermStructure>(flatDiv),
        Handle<YieldTermStructure>(flatRate),
        Handle<BlackVolTermStructure>(flatVol));

    option.setPricingEngine(
        ext::make_shared<FdBlackScholesVanillaEngine>(bsmProcess, 100, 100));

    // Calculate implied volatility (using average of ask and bid)
    Real optionPrice = (askPrice + bidPrice) / 2.0;
    Volatility impliedVol = option.impliedVolatility(optionPrice, bsmProcess);
    volHandle->setValue(impliedVol);

    std::cout << "---- Option Analysis Results ----" << std::endl;
    std::cout << "implied volatility: " << impliedVol << std::endl;
    std::cout << "value: " << option.NPV() << std::endl;
    std::cout << "delta: " << option.delta() << std::endl;
    std::cout << "gamma: " << option.gamma() << std::endl;
    std::cout << "theta: " << option.theta() << std::endl;
    std::cout << "vega: " << option.vega() << std::endl;
    std::cout << "rho: " << option.rho() << std::endl;

    return 0;
}

Example Output

{
	"delta": -0.4291523762730371,
	"gamma": 0.06867092999396703,
	"predictedValue": 1.8448482568095044,
	"rho": -0.007994670535451135,
	"theta": -0.27407985875145857,
	"vega": 0.07649602025704422,
	"volatility": 0.5919
}

In addition to the examples provided in this documentation, more C++ SDK examples are being continuously updated. New examples will be synchronized to the GitHub repository.

https://github.com/tigerfintech/openapi-cpp-sdk

We will continue to add more examples and update them in the documentation and GitHub repository. If you have any questions about using the SDK, please Contact Us directly.