Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def zero_curve(ts):
days = range(10, 365*20, 30)
dtMat = [calendar.advance(today(), d, Days) for d in days]
df = np.array([ts.discount(dt) for dt in dtMat])
dtMat = [QLDateTodate(dt) for dt in dtMat]
dtToday = QLDateTodate(today())
dt = np.array([(d-dtToday).days/365.0 for d in dtMat])
zc = -np.log(df) / dt
return (dtMat, zc)
def zero_curve(ts, days, dtObs):
calendar = TARGET()
dtMat = [calendar.advance(dateToDate(dtObs), d, Days) for d in days]
df = np.array([ts.discount(dt) for dt in dtMat])
dtMat = [QLDateTodate(dt) for dt in dtMat]
dtToday = QLDateTodate(dtObs)
dt = np.array([(d-dtToday).days/365.0 for d in dtMat])
zc = -np.log(df) / dt
return (dtMat, zc)
rate = df_libor.get_value(dtObs, label)
helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor,
settlement_days,
calendar, ModifiedFollowing,
end_of_month,
Actual360())
rate_helpers.append(helper)
liborIndex = Libor('USD Libor', Period(3, Months),
settlement_days,
USDCurrency(), calendar,
Actual360())
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
for m, period, label in swapData:
rate = df_libor.get_value(dtObs, label)
helper = SwapRateHelper.from_tenor(
SimpleQuote(rate / 100.0),
Period(m, Years),
calendar, Semiannual,
ModifiedFollowing, Thirty360(),
liborIndex, spread, fwdStart)
rate_helpers.append(helper)
ts_day_counter = ActualActual(ISDA)
tolerance = 1.0e-15
ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount,
def zero_rate(term_structure, days, dt_settlement, calendar=TARGET()):
"""
Compute zero-coupon rate, continuous ACT/365 from settlement date to given
maturity expressed in calendar days
Return
- array of maturity dates
- array of zero-coupon rates
"""
dtMat = [calendar.advance(pydate_to_qldate(dt_settlement), d, Days)
for d in days]
df = np.array([term_structure.discount(dt) for dt in dtMat])
dtMat = [qldate_to_pydate(dt) for dt in dtMat]
dtToday = qldate_to_pydate(dt_settlement)
dt = np.array([(d - dtToday).days / 365.0 for d in dtMat])
zc = -np.log(df) / dt
return (dtMat, zc)
def get_term_structure(df_libor, dtObs):
settings = Settings()
# Market information
calendar = TARGET()
# must be a business day
eval_date = calendar.adjust(Date.from_datetime(dtObs))
settings.evaluation_date = eval_date
settlement_days = 2
settlement_date = calendar.advance(eval_date, settlement_days, Days)
# must be a business day
settlement_date = calendar.adjust(settlement_date)
depositData = [[1, Months, 'Libor1M'],
[3, Months, 'Libor3M'],
[6, Months, 'Libor6M']]
swapData = [[1, Years, 'Swap1Y'],
[2, Years, 'Swap2Y'],
[3, Years, 'Swap3Y'],
[4, Years, 'Swap4Y'],
[5, Years, 'Swap5Y'],
[7, Years, 'Swap7Y'],
[10, Years, 'Swap10Y'],
[30, Years, 'Swap30Y']]
# loop through rows in option data frame, construct
# helpers for bid/ask
oneDay = datetime.timedelta(days=1)
dtExpiry = [dtTrade + int(t * 365) * oneDay for t in df_option['TTM']]
df_option['dtExpiry'] = dtExpiry
options = []
for index, row in df_option.T.iteritems():
strike = row['Strike']
if (strike / spot.value > 1.3) | (strike / spot.value < .7):
continue
days = int(365 * row['TTM'])
maturity = Period(days, Days)
options.append(HestonModelHelper(
maturity, calendar, spot.value,
strike, SimpleQuote(row['IVBid']),
risk_free_ts, dividend_ts,
ImpliedVolError))
options.append(HestonModelHelper(
maturity, calendar, spot.value,
strike, SimpleQuote(row['IVAsk']),
risk_free_ts, dividend_ts,
ImpliedVolError))
return {'options': options, 'spot': spot}
def get_term_structure(df_libor, dtObs):
settings = Settings()
# Market information
calendar = TARGET()
# must be a business day
eval_date = calendar.adjust(today())
settings.evaluation_date = eval_date
settlement_days = 2
settlement_date = calendar.advance(eval_date, settlement_days, Days)
# must be a business day
settlement_date = calendar.adjust(settlement_date);
depositData =[[1, Months, 'Libor1M'],
[3, Months, 'Libor3M'],
[6, Months, 'Libor6M']]
swapData = [[ 1, Years, 'Swap1Y'],
[ 2, Years, 'Swap2Y'],
[ 3, Years, 'Swap3Y'],
[ 4, Years, 'Swap4Y'],
[ 5, Years, 'Swap5Y'],
[ 7, Years, 'Swap7Y'],
[ 10, Years,'Swap10Y'],
[ 30, Years,'Swap30Y']]
def dividendOption():
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++ General Parameter for all the computation +++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# declaration of the today's date (date where the records are done)
todaysDate = Date(24 , Jan ,2012) # INPUT
Settings.instance().evaluation_date = todaysDate #!\ IMPORTANT COMMAND REQUIRED FOR ALL VALUATIONS
calendar = UnitedStates() # INPUT
settlement_days = 2 # INPUT
# Calcul of the settlement date : need to add a period of 2 days to the todays date
settlementDate = calendar.advance(
todaysDate, period=Period(settlement_days, Days)
)
dayCounter = Actual360() # INPUT
currency = USDCurrency() # INPUT
print("Date of the evaluation: ", todaysDate)
print("Calendar used: ", calendar.name)
print("Number of settlement Days: ", settlement_days)
print("Date of settlement: ", settlementDate)
print("Convention of day counter: ", dayCounter.name)
print("Currency of the actual context:\t\t", currency.name)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++ Description of the underlying +++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
underlying_name = "IBM"
settlement_days,
calendar, ModifiedFollowing, end_of_month,
Actual360())
rate_helpers.append(helper)
endOfMonth = True
liborIndex = Libor('USD Libor', Period(6, Months),
settlement_days,
USDCurrency(), calendar,
ModifiedFollowing,
endOfMonth, Actual360())
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
for m, period, label in swapData:
rate = df_libor.get_value(dtObs, label)
helper = SwapRateHelper(SimpleQuote(rate/100),
Period(m, Years),
calendar, Annual,
Unadjusted, Thirty360(),
liborIndex, spread, fwdStart)
rate_helpers.append(helper)
ts_day_counter = ActualActual(ISDA)
tolerance = 1.0e-15
ts = term_structure_factory(
'discount', 'loglinear', settlement_date, rate_helpers,