Other Examples

Other Examples

Option Calculation Tool

The option calculation tool calculates Greeks, estimates option prices, and derives implied volatility using the jquantlib library.

⚠️

Caution

This tool does not support price prediction for options expiring on the same day.

Complete option-metrics example

package com.tigerbrokers.stock.openapi.demo.quote;

import com.alibaba.fastjson.JSONObject;
import com.tigerbrokers.stock.openapi.client.https.client.TigerHttpClient;
import com.tigerbrokers.stock.openapi.client.https.domain.option.item.OptionBriefItem;
import com.tigerbrokers.stock.openapi.client.https.domain.option.model.OptionCommonModel;
import com.tigerbrokers.stock.openapi.client.https.domain.quote.item.RealTimeQuoteItem;
import com.tigerbrokers.stock.openapi.client.https.request.option.OptionBriefQueryRequest;
import com.tigerbrokers.stock.openapi.client.https.request.quote.QuoteRealTimeQuoteRequest;
import com.tigerbrokers.stock.openapi.client.https.response.option.OptionBriefResponse;
import com.tigerbrokers.stock.openapi.client.https.response.quote.QuoteRealTimeQuoteResponse;
import com.tigerbrokers.stock.openapi.client.struct.OptionFundamentals;
import com.tigerbrokers.stock.openapi.client.struct.enums.Right;
import com.tigerbrokers.stock.openapi.client.struct.enums.TimeZoneId;
import com.tigerbrokers.stock.openapi.client.util.DateUtils;
import com.tigerbrokers.stock.openapi.client.util.OptionCalcUtils;
import com.tigerbrokers.stock.openapi.demo.TigerOpenClientConfig;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

public class OptionCalcTool {
    public static TigerHttpClient client = TigerHttpClient.getInstance().clientConfig(
            TigerOpenClientConfig.getDefaultClientConfig());

    public static void main(String[] args) {
        String symbol = "AAPL"; // Underlying symbol
        Right optionRight = Right.PUT; // Option right
        String strike = "185.0"; // Option strike price
        String settlementDate = "2024-02-05"; // Usually the current date
        String expiryDate = "2024-02-09";
        long expiryTimestamp = DateUtils.parseEpochMill(expiryDate, TimeZoneId.NewYork);

        // Query option bid and ask prices and US Treasury rates.
        OptionCommonModel model = new OptionCommonModel(symbol, optionRight.name(), strike, expiryTimestamp);
        OptionBriefResponse optionBriefResponse = client.execute(OptionBriefQueryRequest.of(model));
        if (optionBriefResponse == null || !optionBriefResponse.isSuccess()) {
            throw new RuntimeException("Failed to get the option brief of symbol: " + symbol);
        }
        List<OptionBriefItem> briefItems = optionBriefResponse.getOptionBriefItems();
        if (briefItems == null || briefItems.isEmpty()) {
            throw new RuntimeException("Failed to get the option brief of symbol: " + symbol);
        }
        OptionBriefItem optionBrief = briefItems.get(0);

        // Query the latest price of the underlying asset.
        QuoteRealTimeQuoteResponse quoteRealTimeQuoteResponse =
                client.execute(QuoteRealTimeQuoteRequest.newRequest(Arrays.asList(symbol)));
        if (quoteRealTimeQuoteResponse == null || !quoteRealTimeQuoteResponse.isSuccess()) {
            throw new RuntimeException("Failed to get the latest price of the stock.");
        }
        List<RealTimeQuoteItem> realTimeQuoteItems = quoteRealTimeQuoteResponse.getRealTimeQuoteItems();
        if (realTimeQuoteItems == null || realTimeQuoteItems.isEmpty()) {
            throw new RuntimeException("Failed to get the latest price of the stock.");
        }
        Double latestPrice = realTimeQuoteItems.get(0).getLatestPrice();
        if (latestPrice == null) {
            throw new RuntimeException("Failed to get the latest price of the stock.");
        }

        // If settlement and expiration are on the same date, use the previous day for settlement.
        // For example, when both dates are 2024-02-09, use 2024-02-08 as the settlement date.
        if (settlementDate.equals(expiryDate)) {
            settlementDate = LocalDate.parse(settlementDate).minusDays(1).toString();
        }

        OptionFundamentals optionFundamentals = OptionCalcUtils.calcOptionIndex(optionRight, latestPrice, Double.valueOf(strike),
                optionBrief.getRatesBonds(), 0, optionBrief.getAskPrice(),optionBrief.getBidPrice(),
                LocalDate.parse(settlementDate) , LocalDate.parse(expiryDate));

        // For index options (European options), use calcEuropeanOptionIndex method:
        // OptionFundamentals optionFundamentals = OptionCalcUtils.calcEuropeanOptionIndex(optionRight, latestPrice, Double.valueOf(strike),
        //         optionBrief.getRatesBonds(), 0, optionBrief.getAskPrice(), optionBrief.getBidPrice(),
        //         LocalDate.parse(settlementDate), LocalDate.parse(expiryDate));

        System.out.println(JSONObject.toJSONString(optionFundamentals));
    }
}

Sample output result:

{
	"delta": -0.4291523762730371,
	"gamma": 0.06867092999396703,
	"historyVolatility": 0.0,
	"insideValue": 0.0,
	"leverage": 0.0,
	"openInterest": 0.0,
	"predictedValue": 1.8448482568095044,
	"premiumRate": 0.0,
	"profitRate": 0.0,
	"rho": -0.007994670535451135,
	"theta": -0.27407985875145857,
	"timeValue": 0.0,
	"vega": 0.07649602025704422,
	"volatility": 0.0
}

Alternative Usage Methods

FDAmericanDividendOptionHelper is the American option calculation class. (The Java SDK uses OptionCalcUtils.calcOptionIndex for equivalent functionality.)

import com.tigerbrokers.stock.openapi.client.struct.OptionFundamentals;
import com.tigerbrokers.stock.openapi.client.struct.enums.Right;
import com.tigerbrokers.stock.openapi.client.util.OptionCalcUtils;
import java.time.LocalDate;

public class OptionTool {
  public static void main(String[] args) {
    OptionFundamentals optionIndex = OptionCalcUtils.calcOptionIndex(
            Right.CALL,
            243.35, //Price of the underlying asset
            240,  //Option strike price
            0.0241,  //Risk-free rate, here using US Treasury rate
            0,  //Dividend yield, most underlying assets have 0
            0.4648, // Implied volatility
            LocalDate.of(2022, 8, 12), //Date for price prediction, must be before option expiration
            LocalDate.of(2022, 8, 19));  //Option expiration date
    System.out.println("value: " + optionIndex.getPredictedValue()); //Calculated option predicted price
    System.out.println("delta: " + optionIndex.getDelta());
    System.out.println("gamma " + optionIndex.getGamma());
    System.out.println("theta " + optionIndex.getTheta());
    System.out.println("vega: " + optionIndex.getVega());
    System.out.println("rho: " + optionIndex.getRho());
  }
}

Sample output result:

value: 8.09628869758489
delta: 0.6005809882595446
gamma 0.024620648675961896
theta -0.4429512650168838
vega: 0.13035018339603335
rho: 0.026470369092588868

Additional Tiger OpenAPI examples are available in the GitHub repository:

https://github.com/tigerfintech/openapi-java-sdk-demo

We will continue to add more examples and update them in the user documentation and GitHub repository, so please stay tuned.


Did this page help you?