Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _blsimpv(price, spot, strike, risk_free_rate, time,
option_type='Call', dividend=0.0):
spot = SimpleQuote(spot)
daycounter = ActualActual()
risk_free_ts = FlatForward(today(), risk_free_rate, daycounter)
dividend_ts = FlatForward(today(), dividend, daycounter)
volatility_ts = BlackConstantVol(today(), NullCalendar(),
.3, daycounter)
process = BlackScholesMertonProcess(spot, dividend_ts,
risk_free_ts, volatility_ts)
exercise_date = today() + Period(time * 365, Days)
exercise = EuropeanExercise(exercise_date)
payoff = PlainVanillaPayoff(option_type, strike)
option = EuropeanOption(payoff, exercise)
engine = AnalyticEuropeanEngine(process)
option.set_pricing_engine(engine)
accuracy = 0.001
def _blsprice(spot, strike, risk_free_rate, time, volatility,
option_type='Call', dividend=0.0, calc='price'):
"""
Black-Scholes option pricing model + greeks.
"""
_spot = SimpleQuote(spot)
daycounter = ActualActual(ISMA)
risk_free_ts = FlatForward(today(), risk_free_rate, daycounter)
dividend_ts = FlatForward(today(), dividend, daycounter)
volatility_ts = BlackConstantVol(today(), NullCalendar(),
volatility, daycounter)
process = BlackScholesMertonProcess(_spot, dividend_ts,
risk_free_ts, volatility_ts)
exercise_date = today() + Period(time * 365, Days)
exercise = EuropeanExercise(exercise_date)
payoff = PlainVanillaPayoff(option_type, strike)
option = EuropeanOption(payoff, exercise)
engine = AnalyticEuropeanEngine(process)
option.set_pricing_engine(engine)
if calc == 'price':
def _blsprice(spot, strike, risk_free_rate, time, volatility,
option_type='Call', dividend=0.0, calc='price'):
"""
Black-Scholes option pricing model + greeks.
"""
_spot = SimpleQuote(spot)
daycounter = ActualActual(ISMA)
risk_free_ts = FlatForward(today(), risk_free_rate, daycounter)
dividend_ts = FlatForward(today(), dividend, daycounter)
volatility_ts = BlackConstantVol(today(), NullCalendar(),
volatility, daycounter)
process = BlackScholesMertonProcess(_spot, dividend_ts,
risk_free_ts, volatility_ts)
exercise_date = today() + Period(time * 365, Days)
exercise = EuropeanExercise(exercise_date)
payoff = PlainVanillaPayoff(option_type, strike)
option = EuropeanOption(payoff, exercise)
engine = AnalyticEuropeanEngine(process)
option.set_pricing_engine(engine)
if __name__ == '__main__':
#*********************
#*** MARKET DATA ***
#*********************
calendar = TARGET()
todays_date = Date(15, May, 2007)
# must be a business day
todays_date = calendar.adjust(todays_date)
Settings.instance().evaluation_date = todays_date
# dummy curve
ts_curve = FlatForward(
reference_date=todays_date, forward=0.01, daycounter=Actual365Fixed()
)
# In Lehmans Brothers "guide to exotic credit derivatives"
# p. 32 there's a simple case, zero flat curve with a flat CDS
# curve with constant market spreads of 150 bp and RR = 50%
# corresponds to a flat 3% hazard rate. The implied 1-year
# survival probability is 97.04% and the 2-years is 94.18%
# market
recovery_rate = 0.5
quoted_spreads = [0.0150, 0.0150, 0.0150, 0.0150 ]
tenors = [Period(i, Months) for i in [3, 6, 12, 24]]
maturities = [
calendar.adjust(todays_date + tenors[i], Following) for i in range(4)
]
def flat_rate(forward, daycounter):
"""
Create a flat yield curve, with rate defined according
to the specified day-count convention.
Used mostly for unit tests and simple illustrations.
"""
return FlatForward(
forward=SimpleQuote(forward),
settlement_days=0,
calendar=NullCalendar(),
daycounter=daycounter
)
forward=0.06,
daycounter=Actual365Fixed()
)
# option parameters
exercise = AmericanExercise(
earliest_exercise_date=settlement_date,
latest_exercise_date=Date(17, May, 1999)
)
payoff = PlainVanillaPayoff(Put, 40.0)
# market data
underlying = SimpleQuote(36.0)
volatility = BlackConstantVol(todays_date, TARGET(), 0.20,
Actual365Fixed())
dividend_yield = FlatForward(
reference_date=settlement_date,
forward=0.00,
daycounter=Actual365Fixed()
)
# report
header = '%19s' % 'method' + ' |' + \
' |'.join(['%17s' % tag for tag in ['value',
'estimated error',
'actual error']])
print()
print(header)
print('-' * len(header))
refValue = None
from quantlib.termstructures.yields.api import FlatForward
from quantlib.time.api import Actual360, Date, NullCalendar, TARGET
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
calendar = TARGET()
date_today = Date(6,9,2011)
date_payment = Date(6,12,2000)
settlement_days = 2
quote = SimpleQuote(value=0.03)
term_structure = FlatForward(
settlement_days = settlement_days,
quote = quote,
calendar = NullCalendar(),
daycounter = Actual360()
)
try:
df_1 = term_structure.discount(date_payment)
print('rate: %f df_1: %f' % (quote.value, df_1))
except RuntimeError as exc:
logger.error('Evaluation date and discount date issue.')
logger.exception(exc)
bond = FixedRateBond(
settlement_days,
face_amount,
fixed_bond_schedule,
[coupon_rate],
cnt,
Following,
redemption,
issue_date
)
discounting_term_structure = YieldTermStructure(relinkable=True)
cnt_yield = DayCounter.from_name('Actual/Actual (Historical)')
flat_term_structure = FlatForward(
settlement_days=2,
forward=bond_yield,
calendar=NullCalendar(),
daycounter=cnt_yield,
compounding=Compounded,
frequency=_period)
discounting_term_structure.link_to(flat_term_structure)
engine = DiscountingBondEngine(discounting_term_structure)
bond.set_pricing_engine(engine)
price = bond.clean_price
ac = bond.accrued_amount(pydate_to_qldate(settlement_date))
)
issue_date = effective_date
bond = FixedRateBond(
settlement_days,
face_amount,
fixed_bond_schedule,
[coupon_rate],
ActualActual(ISMA),
Following,
redemption,
issue_date
)
discounting_term_structure = YieldTermStructure(relinkable=True)
flat_term_structure = FlatForward(
settlement_days = 1,
forward = 0.044,
calendar = NullCalendar(),
daycounter = Actual365Fixed(),
compounding = Continuous,
frequency = Annual)
discounting_term_structure.link_to(flat_term_structure)
pricing_engine = DiscountingBondEngine(discounting_term_structure)
bond.set_pricing_engine(pricing_engine)
print('Settlement date: ', bond.settlement_date())
print('Maturity date:', bond.maturity_date)
print('Accrued amount: ', bond.accrued_amount(bond.settlement_date()))
print('Clean price:', bond.clean_price)