Other Examples
Other examples for the Python SDK are continuously being updated. If there are new examples, we will synchronize them promptly in the Github code repository. Meanwhile, if you have any questions about using the SDK, please contact us directly.
Option Calculation Tools
In the SDK code path tigeropen/examples/option_helpers/helpers.py, option calculation tools are provided that can be used for option Greeks calculation, option price calculation, and implied volatility calculation.
The related algorithms are based on the quantlib library, which needs to be installed first before use: pip install quantlib==1.40
Usage Method 1: Import in Code
FDAmericanDividendOptionHelper is the American option calculation class (including US stock options, Hong Kong stock options, and ETF options all use this class)
FDEuropeanDividendOptionHelper is the European option calculation class (index options use this class)
import quantlib as ql
from tigeropen.examples.option_helpers.helpers import FDAmericanDividendOptionHelper
# Calculate implied volatility based on 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, # Set implied volatility to 0 temporarily
settlement_date=ql.Date(14, 4, 2022),
expiration_date=ql.Date(22, 4, 2022))
# Calculate implied volatility, parameter is option price, can use market price (ask,bid) to calculate. (ask + bid) / 2
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()}')
# Directly use implied volatility to calculate option price:
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: Call as Script Command
Assuming tigeropen/examples/option_helpers/helpers.py is saved 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 based on option price. -n specifies option 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 based on option market data. (Use average of ask bid as option price)
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 tool wraps SDK requests. You can pass in option identifiers directly to request and calculate option Greeks, buy profit probability, sell annualized yield, 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 about 1 month ago
