Other Examples
Other Examples
Option Pricing Tools
The C++ SDK does not include an option-pricing library. Use its market data methods to retrieve option bid and ask prices, implied volatility, Greeks, and other inputs, then perform local calculations with a third-party library such as QuantLib.
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.
For locally calculated results, delta is sensitivity to changes in the underlying price; gamma is sensitivity of delta to the underlying price; theta is sensitivity to the passage of time; vega is sensitivity to implied-volatility changes; and rho is sensitivity to risk-free-rate changes.
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("your_config_directory_path"));
// 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;
}Get Option Chain Data
Use get_option_chain to get the complete option chain for a given underlying and expiration date. The current implementation does not serialize the option_filter parameter, so passing a filter object does not change the request.
#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("your_config_directory_path"));
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;
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("your_config_directory_path"));
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;
}Output
This example prints QuantLib calculations directly with std::cout and does not produce JSON. Output values depend on the inputs, QuantLib version, and pricing engine.
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 examples to the documentation and GitHub repository. If you have questions about using the SDK, use the documentation site's Ask AI feature or Contact Us.
Updated about 2 months ago
