中文

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.32

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