Other Examples

About 2 min

Option Calculation Tool

Option calculation tools are available for option Greek value calculation, option prediction price, and implied volatility calculation. The related algorithms are based on the jquantlib library.

The tool does not support price forecasting for doomsday options.

Example of Option Indicator 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"; //Option underlying symbol
        Right optionRight = Right.PUT; //option right
        String strike = "185.0"; //option strike price
        String settlementDate = "2024-02-05"; //usually is current date
        String expiryDate = "2024-02-09"; option expiry date
        long expiryTimestamp = DateUtils.parseEpochMill(expiryDate, TimeZoneId.NewYork);

        //Find out about options' opening prices, as well as U.S. Treasury rates and more
        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);
            }
        }

        //Check the latest price of the underlying of the option
        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 the option expiration date are the same, you need to change the settlement date to the previous day before doing the calculation, for example, if the settlement date and the option expiration date are both 2024-02-09, you need to change the settlement date 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 and need to be calculated using 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));
    }
}

example 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
}

Other Usage

FDAmericanDividendOptionHelper is American Option Calculation Tool.

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 Test {
  public static void main(String[] args) {
    OptionFundamentals optionIndex = OptionCalcUtils.calcOptionIndex(
            Right.CALL,
            243.35, //stock price
            240,  //option strike price
            0.0241,  //risk free rate
            0,  //dividend rate
            0.4648, // Implied Volatility
            LocalDate.of(2022, 8, 12), //settlement date
            LocalDate.of(2022, 8, 19));  //option expiration date
    System.out.println("value: " + optionIndex.getPredictedValue()); //predicted option 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());
  }
}

example 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 documentation, we have also provided examples involving the use of other Open API features for your reference. We have updated this part of the code to the Github repository. https://github.com/tigerfintech/openapi-java-sdk-demoopen in new window

Last update: