Skip to content

Commit f0d2ed7

Browse files
committed
update examples
1 parent cb43b68 commit f0d2ed7

File tree

5 files changed

+89
-82
lines changed

5 files changed

+89
-82
lines changed

examples/american_option.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,52 @@
66
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
77
FOR A PARTICULAR PURPOSE. See the license for more details.
88
"""
9-
from __future__ import print_function
10-
11-
from quantlib.instruments.api import AmericanExercise, VanillaOption, Put
9+
from quantlib.instruments.api import AmericanExercise, VanillaOption, OptionType
1210
from quantlib.instruments.payoffs import PlainVanillaPayoff
1311
from quantlib.pricingengines.api import BaroneAdesiWhaleyApproximationEngine
14-
from quantlib.pricingengines.api import FDAmericanEngine
12+
from quantlib.pricingengines.api import FdBlackScholesVanillaEngine
1513
from quantlib.processes.black_scholes_process import BlackScholesMertonProcess
1614
from quantlib.quotes import SimpleQuote
1715
from quantlib.settings import Settings
1816
from quantlib.time.api import Actual365Fixed, Date, May, TARGET
19-
from quantlib.termstructures.volatility.equityfx.black_vol_term_structure \
20-
import BlackConstantVol
21-
from quantlib.termstructures.yields.api import FlatForward
22-
17+
from quantlib.termstructures.volatility.api import BlackConstantVol
18+
from quantlib.termstructures.yields.api import HandleYieldTermStructure, FlatForward
19+
from quantlib.methods.finitedifferences.solvers.fdmbackwardsolver \
20+
import FdmSchemeDesc
2321

2422
def main():
2523
# global data
2624
todays_date = Date(15, May, 1998)
2725
Settings.instance().evaluation_date = todays_date
2826
settlement_date = Date(17, May, 1998)
2927

30-
risk_free_rate = FlatForward(
31-
reference_date=settlement_date,
32-
forward=0.06,
33-
daycounter=Actual365Fixed()
28+
risk_free_rate = HandleYieldTermStructure()
29+
risk_free_rate.link_to(
30+
FlatForward(
31+
reference_date=settlement_date,
32+
forward=0.06,
33+
daycounter=Actual365Fixed()
34+
)
3435
)
3536

3637
# option parameters
3738
exercise = AmericanExercise(
3839
earliest_exercise_date=settlement_date,
3940
latest_exercise_date=Date(17, May, 1999)
4041
)
41-
payoff = PlainVanillaPayoff(Put, 40.0)
42+
payoff = PlainVanillaPayoff(OptionType.Put, 40.0)
4243

4344
# market data
4445
underlying = SimpleQuote(36.0)
4546
volatility = BlackConstantVol(todays_date, TARGET(), 0.20,
4647
Actual365Fixed())
47-
dividend_yield = FlatForward(
48-
reference_date=settlement_date,
49-
forward=0.00,
50-
daycounter=Actual365Fixed()
48+
dividend_yield = HandleYieldTermStructure()
49+
dividend_yield.link_to(
50+
FlatForward(
51+
reference_date=settlement_date,
52+
forward=0.00,
53+
daycounter=Actual365Fixed()
54+
)
5155
)
5256

5357
# report
@@ -91,8 +95,10 @@ def report(method, x, dx=None):
9195
time_steps = 801
9296
grid_points = 800
9397

94-
option.set_pricing_engine(FDAmericanEngine('CrankNicolson',
95-
process, time_steps, grid_points))
98+
option.set_pricing_engine(
99+
FdBlackScholesVanillaEngine(
100+
process, time_steps, grid_points, scheme=FdmSchemeDesc.CrankNicolson())
101+
)
96102
report('finite differences', option.net_present_value)
97103

98104
print('This is work in progress.')

examples/basic_example.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
""" Simple example pricing a European option
1+
""" Simple example pricing a European option
22
using a Black&Scholes Merton process."""
33
from __future__ import print_function
44

5-
from quantlib.instruments.api import (EuropeanExercise, PlainVanillaPayoff, Put,
5+
from quantlib.instruments.api import (EuropeanExercise, PlainVanillaPayoff, OptionType,
66
VanillaOption)
77
from quantlib.pricingengines.api import AnalyticEuropeanEngine
88
from quantlib.processes.black_scholes_process import BlackScholesMertonProcess
99
from quantlib.quotes import SimpleQuote
1010
from quantlib.settings import Settings
1111
from quantlib.time.api import TARGET, Actual365Fixed, today
12-
from quantlib.termstructures.yields.api import FlatForward
12+
from quantlib.termstructures.yields.api import FlatForward, HandleYieldTermStructure
1313
from quantlib.termstructures.volatility.api import BlackConstantVol
1414

1515

@@ -24,7 +24,7 @@
2424
settings.evaluation_date = todays_date
2525

2626
# options parameters
27-
option_type = Put
27+
option_type = OptionType.Put
2828
underlying = 36
2929
strike = 40
3030
dividend_yield = 0.00
@@ -36,16 +36,23 @@
3636
underlyingH = SimpleQuote(underlying)
3737

3838
# bootstrap the yield/dividend/vol curves
39-
flat_term_structure = FlatForward(
40-
reference_date=settlement_date,
41-
forward=risk_free_rate,
42-
daycounter=daycounter
39+
flat_term_structure = HandleYieldTermStructure()
40+
flat_dividend_ts = HandleYieldTermStructure()
41+
42+
flat_term_structure.link_to(
43+
FlatForward(
44+
reference_date=settlement_date,
45+
forward=risk_free_rate,
46+
daycounter=daycounter
47+
)
4348
)
4449

45-
flat_dividend_ts = FlatForward(
46-
reference_date=settlement_date,
47-
forward=dividend_yield,
48-
daycounter=daycounter
50+
flat_dividend_ts.link_to(
51+
FlatForward(
52+
reference_date=settlement_date,
53+
forward=dividend_yield,
54+
daycounter=daycounter
55+
)
4956
)
5057

5158
flat_vol_ts = BlackConstantVol(

examples/bonds.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,29 @@
33
This example is based on the QuantLib Excel bond demo.
44
55
"""
6-
from __future__ import print_function
7-
86
from quantlib.instruments.bonds import FixedRateBond
97
from quantlib.time.api import (
10-
TARGET, Unadjusted, ModifiedFollowing, Following, NullCalendar
8+
Date, Period, Annual, TARGET, Unadjusted, ModifiedFollowing, Following, NullCalendar, Years,
9+
DateGeneration
1110
)
1211
from quantlib.compounding import Continuous
1312
from quantlib.pricingengines.bond import DiscountingBondEngine
14-
from quantlib.time.date import Date, August, Period, Jul, Annual, Years
1513
from quantlib.time.daycounters.simple import Actual365Fixed
1614
from quantlib.time.daycounters.actual_actual import ActualActual, ISMA
17-
from quantlib.time.schedule import Schedule, Backward
15+
from quantlib.time.schedule import Schedule
1816
from quantlib.settings import Settings
1917
from quantlib.termstructures.yields.api import (
20-
FlatForward, YieldTermStructure
18+
FlatForward, HandleYieldTermStructure
2119
)
2220

23-
todays_date = Date(25, August, 2011)
21+
todays_date = Date(25, 8, 2011)
2422

2523

2624
settings = Settings.instance()
2725
settings.evaluation_date = todays_date
2826

2927
calendar = TARGET()
30-
effective_date = Date(10, Jul, 2006)
28+
effective_date = Date(10, 7, 2006)
3129
termination_date = calendar.advance(
3230
effective_date, 10, Years, convention=Unadjusted
3331
)
@@ -45,7 +43,7 @@
4543
calendar,
4644
ModifiedFollowing,
4745
ModifiedFollowing,
48-
Backward
46+
DateGeneration.Backward
4947
)
5048

5149
issue_date = effective_date
@@ -60,7 +58,7 @@
6058
issue_date
6159
)
6260

63-
discounting_term_structure = YieldTermStructure(relinkable=True)
61+
discounting_term_structure = HandleYieldTermStructure()
6462
flat_term_structure = FlatForward(
6563
settlement_days = 1,
6664
forward = 0.044,
@@ -76,6 +74,4 @@
7674
print('Settlement date: ', bond.settlement_date())
7775
print('Maturity date:', bond.maturity_date)
7876
print('Accrued amount: ', bond.accrued_amount(bond.settlement_date()))
79-
print('Clean price:', bond.clean_price)
80-
81-
77+
print('Clean price:', bond.clean_price())

examples/multicurve_bootstrapping.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
from tabulate import tabulate
22
from quantlib.time.api import (TARGET, Date, Weeks, Days, Months, Years,
3-
Actual360, Actual365Fixed, Following, Annual, Unadjusted, Thirty360, Schedule, ModifiedFollowing, Semiannual, Period)
4-
from quantlib.time.schedule import Forward
3+
Actual360, Actual365Fixed, Following, Annual, Unadjusted, Thirty360, Schedule, ModifiedFollowing, Semiannual, Period, DateGeneration)
54
from quantlib.time.daycounters.thirty360 import European
65
from quantlib.settings import Settings
76
from quantlib.quotes import SimpleQuote
87
from quantlib.termstructures.yields.rate_helpers import DepositRateHelper, FraRateHelper, SwapRateHelper
9-
from quantlib.termstructures.yields.ois_rate_helper import OISRateHelper, DatedOISRateHelper
8+
from quantlib.termstructures.yields.ois_rate_helper import OISRateHelper
109
from quantlib.termstructures.yields.piecewise_yield_curve import PiecewiseYieldCurve
1110
from quantlib.termstructures.yields.bootstraptraits import Discount
12-
from quantlib.termstructures.yields.api import YieldTermStructure
11+
from quantlib.termstructures.yields.api import HandleYieldTermStructure
1312
from quantlib.math.interpolation import Cubic
1413
from quantlib.indexes.ibor.eonia import Eonia
1514
from quantlib.indexes.api import Euribor6M
16-
from quantlib.instruments.swap import SwapType
15+
from quantlib.instruments.swap import Swap
1716
from quantlib.instruments.vanillaswap import VanillaSwap
1817
from quantlib.pricingengines.swap import DiscountingSwapEngine
1918

@@ -107,19 +106,19 @@
107106

108107

109108
# Dated OIS
110-
oisDated1 = DatedOISRateHelper(
109+
oisDated1 = OISRateHelper.from_dates(
111110
Date(16, 1, 2013), Date(13, 2, 2013),
112111
oisDated1Rate, eonia)
113-
oisDated2 = DatedOISRateHelper(
112+
oisDated2 = OISRateHelper.from_dates(
114113
Date(13, 2, 2013), Date(13, 3, 2013),
115114
oisDated2Rate, eonia)
116-
oisDated3 = DatedOISRateHelper(
115+
oisDated3 = OISRateHelper.from_dates(
117116
Date(13, 3, 2013), Date(10, 4, 2013),
118117
oisDated3Rate, eonia)
119-
oisDated4 = DatedOISRateHelper(
118+
oisDated4 = OISRateHelper.from_dates(
120119
Date(10, 4, 2013), Date(8, 5, 2013),
121120
oisDated4Rate, eonia)
122-
oisDated5 = DatedOISRateHelper(
121+
oisDated5 = OISRateHelper.from_dates(
123122
Date(8, 5, 2013), Date(12, 6, 2013),
124123
oisDated5Rate, eonia)
125124

@@ -166,9 +165,9 @@
166165

167166
# Term structures that will be used for pricing:
168167
# the one used for discounting cash flows
169-
discountingTermStructure = YieldTermStructure()
168+
discountingTermStructure = HandleYieldTermStructure()
170169
# the one used for forward rate forecasting
171-
forecastingTermStructure = YieldTermStructure()
170+
forecastingTermStructure = HandleYieldTermStructure()
172171

173172
discountingTermStructure.link_to(eonia_term_structure)
174173

@@ -248,19 +247,19 @@
248247
spread = 0.0
249248

250249
lengthInYears = 5
251-
swapType = SwapType.Payer
250+
swapType = Swap.Payer
252251

253252
maturity = settlement_date + lengthInYears*Years;
254253
fixedSchedule = Schedule.from_rule(settlement_date, maturity,
255254
Period(fixedLegFrequency),
256255
calendar, fixedLegConvention,
257256
fixedLegConvention,
258-
Forward, False)
257+
DateGeneration.Forward, False)
259258
floatSchedule = Schedule.from_rule(settlement_date, maturity,
260259
Period(floatingLegFrequency),
261260
calendar, floatingLegConvention,
262261
floatingLegConvention,
263-
Forward, False)
262+
DateGeneration.Forward, False)
264263
spot5YearSwap = VanillaSwap(swapType, nominal,
265264
fixedSchedule, fixedRate, fixedLegDayCounter,
266265
floatSchedule, euriborIndex, spread,
@@ -272,12 +271,12 @@
272271
Period(fixedLegFrequency),
273272
calendar, fixedLegConvention,
274273
fixedLegConvention,
275-
Forward, False)
274+
DateGeneration.Forward, False)
276275
fwdFloatSchedule = Schedule.from_rule(fwdStart, fwdMaturity,
277276
Period(floatingLegFrequency),
278277
calendar, floatingLegConvention,
279278
floatingLegConvention,
280-
Forward, False)
279+
DateGeneration.Forward, False)
281280
oneYearForward5YearSwap = VanillaSwap(swapType, nominal,
282281
fwdFixedSchedule, fixedRate, fixedLegDayCounter,
283282
fwdFloatSchedule, euriborIndex, spread,

0 commit comments

Comments
 (0)