中文

Other Examples

Other Examples

Option Calculation Tool

The option calculation tool can be used for calculating option Greeks, predicting option prices, and calculating implied volatility. Related algorithms are based on the jquantlib library.

⚠️

CAUTION

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

Complete Example of Option Metrics Calculation

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 for the option
        Right optionRight = Right.PUT; //Option direction
        String strike = "185.0"; //Strike price of the option
        String settlementDate = "2024-02-05"; //Generally the current date
        String expiryDate = "2024-02-09";
        long expiryTimestamp = DateUtils.parseEpochMill(expiryDate, TimeZoneId.NewYork);

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

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

        //TODO If the settlement date and option expiration date are the same, the settlement date needs to be changed to the previous day for calculation. For example, if both settlement date and expiration date are 2024-02-09, the settlement date should be changed to 2024-02-08
        OptionFundamentals optionFundamentals = OptionCalcUtils.calcOptionIndex(optionRight, latestPrice, Double.valueOf(strike),
                optionBrief.getRatesBonds(), 0, optionBrief.getAskPrice(),optionBrief.getBidPrice(),
                LocalDate.parse(settlementDate) , LocalDate.parse(expiryDate));

        //TODO Index options should all be European options, need to use calcEuropeanOptionIndex for calculation
//    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

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

In addition to the examples provided in this user documentation, we also offer usage examples involving other features of the Open API for user reference. This part of the code has been updated to our 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.