Other Examples
Note: The Option Calculator content on this page has been consolidated into the Options quote page. See Option Calculator for the most up-to-date documentation.
Python SDK examples are updated continuously in the GitHub repository. If you have questions about using the SDK, see Contact Us.
Option Calculation Tools
The SDK includes option calculation tools at tigeropen/examples/option_helpers/helpers.py for calculating Greeks, option prices, and implied volatility.
These tools use the quantlib library. Install it first with pip install quantlib==1.40.
Usage Method 1: Import in Code
Use FDAmericanDividendOptionHelper for American-style options, including US and Hong Kong stock options and ETF options.
Use FDEuropeanDividendOptionHelper for European-style options, including index options.
import QuantLib as ql
from tigeropen.examples.option_helpers.helpers import FDAmericanDividendOptionHelper
# Calculate implied volatility from an option price
ql.Settings.instance().evaluationDate = ql.Date(19, 4, 2022)
helper = FDAmericanDividendOptionHelper(option_type=ql.Option.Call,
underlying=985,
strike=990,
risk_free_rate=0.017,
dividend_rate=0,
volatility=0, # Use 0 until implied volatility is calculated
settlement_date=ql.Date(14, 4, 2022),
expiration_date=ql.Date(22, 4, 2022))
# Calculate implied volatility from the option price, such as the midpoint of the bid and ask
volatility = helper.implied_volatility(33.6148)
helper.update_implied_volatility(volatility)
print(f'implied volatility:{volatility}')
print(f'value:{helper.NPV()}')
print(f'delta:{helper.delta()}')
print(f'gamma:{helper.gamma()}')
print(f'theta:{helper.theta()}')
print(f'vega:{helper.vega()}')
print(f'rho:{helper.rho()}')
# Calculate the option price from implied volatility
ql.Settings.instance().evaluationDate = ql.Date(19, 4, 2022)
helper = FDAmericanDividendOptionHelper(option_type=ql.Option.Call, # PUT/CALL
underlying=985, # Stock price on settlement date
strike=990, # Strike price
risk_free_rate=0.017, # Risk-free rate
dividend_rate=0, # Dividend rate
volatility=0.6153, # Implied volatility
settlement_date=ql.Date(14, 4, 2022), # Settlement date
expiration_date=ql.Date(22, 4, 2022)) # Option expiration date
print(f'value:{helper.NPV()}')
print(f'delta:{helper.delta()}')
print(f'gamma:{helper.gamma()}')
print(f'theta:{helper.theta()}')
print(f'vega:{helper.vega()}')
print(f'rho:{helper.rho()}')
Usage Method 2: Run as a Script
The following commands assume that tigeropen/examples/option_helpers/helpers.py is in the current directory.
# Calculate option price
python helpers.py -t PUT -e '2022-05-20' -s 2022-04-24 -p 215 -u 215.52 -r 0.0078 -v 0.5919
# Calculate implied volatility from an option price; -n specifies the price
python helpers.py -t CALL -e '2022-04-22' -s 2022-04-14 -p 990 -u 985 -r 0.017 -n 33.6148
# Calculate implied volatility from the midpoint of the bid and ask
python helpers.py -t CALL -e '2022-04-22' -s 2022-04-14 -p 990 -u 985 -r 0.017 -a 35 -b 36
# View command help
python helpers.py -h
Option Metrics Utility Tool
This utility wraps SDK requests. Pass option identifiers to calculate Greeks, probability of profit for a purchase, annualized yield for a sale, and other metrics.
Code path: tigeropen/examples/option_helpers/util.py
Example:
import QuantLib as ql
from tigeropen.tiger_open_config import TigerOpenClientConfig
from tigeropen.quote.quote_client import QuoteClient
from tigeropen.trade.trade_client import TradeClient
from tigeropen.examples.option_helpers.util import OptionUtil
client_config = TigerOpenClientConfig(props_path='.config/')
quote_client = QuoteClient(client_config)
trade_client = TradeClient(client_config)
option_util = OptionUtil(quote_client, trade_client)
# Calculate metrics for specific options
identifiers = ['TSLA 260220C00385000']
# Example 1: Return as DataFrame
print("Example 1: Return as DataFrame")
metrics_df = option_util.get_option_metrics(identifiers, return_type='dataframe')
print(f"\n{metrics_df}")
# Example 2: Return as List of OptionMetric objects
print("Example 2: Return as List of OptionMetric objects")
metrics_list = option_util.get_option_metrics(identifiers, return_type='list')
for metric in metrics_list:
print(metric)
print(f" Greeks: delta={metric.delta}, gamma={metric.gamma}, "
f"theta={metric.theta}, vega={metric.vega}")
print(f" Risk: implied_vol={metric.implied_vol}, leverage={metric.leverage_ratio}")
print(f" Probability: profit_prob={metric.profit_probability}")Updated 4 days ago
