Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def flat_rate(forward, daycounter):
return FlatForward(
quote = SimpleQuote(forward),
settlement_days = 0,
calendar = NullCalendar(),
daycounter = daycounter
)
from quantlib.termstructures.yields.zero_curve import ZeroCurve
def flat_rate(forward, daycounter):
return FlatForward(
quote = SimpleQuote(forward),
settlement_days = 0,
calendar = NullCalendar(),
daycounter = daycounter
)
DtSettlement = today()
settings = Settings()
settings.evaluation_date = DtSettlement
daycounter = ActualActual()
calendar = NullCalendar()
iRate = .1
iDiv = .04
risk_free_ts = flat_rate(iRate, daycounter)
dividend_ts = flat_rate(iDiv, daycounter)
s0 = SimpleQuote(32.0)
# Bates model
v0 = 0.05
kappa = 5.0;
theta = 0.05;
sigma = 1.0e-4;
def flat_rate(forward, daycounter):
return FlatForward(
quote = SimpleQuote(forward),
settlement_days = 0,
calendar = NullCalendar(),
daycounter = daycounter
)
DtSettlement = today()
settings = Settings()
settings.evaluation_date = DtSettlement
daycounter = ActualActual()
calendar = NullCalendar()
iRate = .1
iDiv = .04
risk_free_ts = flat_rate(iRate, daycounter)
dividend_ts = flat_rate(iDiv, daycounter)
s0 = SimpleQuote(32.0)
# Bates model
v0 = 0.05
kappa = 5.0;
theta = 0.05;
sigma = 1.0e-4;
rho = 0.0;
def flat_rate(forward, daycounter):
return FlatForward(
quote = SimpleQuote(forward),
settlement_days = 0,
calendar = NullCalendar(),
daycounter = daycounter
)
helper = DepositRateHelper(float(rate/100), tenor,
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(
df_tmp = DataFrame.filter(df_option, items=['dtExpiry', 'IR', 'IDIV'])
grouped = df_tmp.groupby('dtExpiry')
df_rates = grouped.agg(lambda x: x[0])
# convert data frame (date/value) into zero curve
# expect the index to be a date, and 1 column of values
risk_free_ts = df_to_zero_curve(df_rates['R'], dtTrade)
dividend_ts = df_to_zero_curve(df_rates['D'], dtTrade)
# back out the spot from any forward
iRate = df_option['R'][0]
iDiv = df_option['D'][0]
TTM = df_option['T'][0]
Fwd = df_option['F'][0]
spot = SimpleQuote(Fwd * np.exp(-(iRate - iDiv) * TTM))
print('Spot: %f risk-free rate: %f div. yield: %f' % \
(spot.value, iRate, iDiv))
# 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['T']]
df_option['dtExpiry'] = dtExpiry
options = []
for index, row in df_option.T.iteritems():
strike = row['K']
if (strike / spot.value > 1.3) | (strike / spot.value < .7):
continue
def make_rate_helper(market, quote, reference_date=None):
"""
Wrapper for deposit and swaps rate helpers makers
TODO: class method of RateHelper?
"""
rate_type, tenor, quote_value = quote
if rate_type == 'SWAP':
libor_index = market._floating_rate_index
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
helper = SwapRateHelper.from_tenor(
quote_value,
Period(tenor),
market._floating_rate_index.fixing_calendar,
Period(market._params.fixed_leg_period).frequency,
BusinessDayConvention.from_name(
market._params.fixed_leg_convention),
DayCounter.from_name(market._params.fixed_leg_daycount),
libor_index, spread, fwdStart)
elif rate_type == 'DEP':
end_of_month = True
helper = DepositRateHelper(
quote_value,
Period(tenor),
market._params.settlement_days,
calendar = JointCalendar(UnitedStates(), UnitedKingdom())
# must be a business day
eval_date = calendar.adjust(dt_obs)
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)
end_of_month = True
if((rate_type == 'SWAP') & (period == 'Y')):
liborIndex = Libor(
'USD Libor', Period(6, Months), settlement_days,
USDCurrency(), calendar, Actual360()
)
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
helper = SwapRateHelper.from_tenor(
SimpleQuote(rate),
Period(tenor, Years),
calendar, Annual,
Unadjusted, Thirty360(),
liborIndex, spread, fwdStart)
elif((rate_type == 'LIBOR') & (period == 'M')):
helper = DepositRateHelper(SimpleQuote(rate),
Period(tenor, Months),
settlement_days,
calendar,
ModifiedFollowing,
end_of_month,
Actual360())
else:
def aggregate(serie):
return serie[serie.index[0]]
df_rates = grouped.agg(aggregate)
# Get first index:
first_index = 0
dtTrade = df_option['dtTrade'][first_index]
# back out the spot from any forward
iRate = df_option['iRate'][first_index]
iDiv = df_option['iDiv'][first_index]
TTM = df_option['TTM'][first_index]
Fwd = df_option['Fwd'][first_index]
spot = SimpleQuote(Fwd * np.exp(-(iRate - iDiv) * TTM))
print('Spot: %f risk-free rate: %f div. yield: %f' % (spot.value,
iRate, iDiv))
# build array of option helpers
hh = heston_helpers(spot, df_option, dtTrade, df_rates)
risk_free_ts = dfToZeroCurve(df_rates['iRate'], dtTrade)
dividend_ts = dfToZeroCurve(df_rates['iDiv'], dtTrade)
return {'options': hh['options'], 'spot': spot,
'risk_free_rate': risk_free_ts,
'dividend_rate': dividend_ts}
# Create schedule based on market and bond parameters.
index = market._floating_rate_index
schedule = Schedule.from_rule(
issue_date,
maturity,
Period(tenor),
index.fixing_calendar,
index.business_day_convention,
index.business_day_convention,
Rule.Backward, # Date generation rule
index.end_of_month,
)
daycounter = DayCounter.from_name("Actual/Actual (Bond)")
helper = FixedRateBondHelper(
SimpleQuote(clean_price),
market._params.settlement_days,
100.0,
schedule,
coupons,
daycounter,
Following, # Payment convention
100.0,
issue_date)
return helper