-
Notifications
You must be signed in to change notification settings - Fork 0
/
mortgage_pricing_engine_ontherun.py
66 lines (46 loc) · 2.65 KB
/
mortgage_pricing_engine_ontherun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from QuantLib.QuantLib import HestonSLVFDMModel, IborIndex, Schedule
import QuantLib as ql
import numpy as np
import pandas as pd
from datetime import date
from pandas.core import base
def onTheRunSwapNPV(start_date, matDate, rateHedge, fytsBase, ytsBase):
calendar = ql.TARGET()
dayCounter = ql.Actual360()
conventionMD = ql.ModifiedFollowing
notionalsFlat = [1]
scheduler = ql.MakeSchedule(start_date, matDate, ql.Period('3m'), rule=ql.DateGeneration.Backward
, convention=conventionMD, calendar=ql.UnitedStates(ql.UnitedStates.Settlement))
iborIndexBase = ql.Libor('SK3MBase', ql.Period('3m'), 2, ql.USDCurrency(), ql.TARGET(), dayCounter
, fytsBase)
iborIndexBase.addFixing(ql.Date(2,7,2021), 0.01)
fixedLegHedgeBase = ql.FixedRateLeg(scheduler, dayCounter, notionalsFlat, [rateHedge])
floatLegHedgeBase = ql.IborLeg(notionalsFlat, scheduler, iborIndexBase, dayCounter, conventionMD, fixingDays=[2])
swapHedgeBase = ql.Swap(fixedLegHedgeBase, floatLegHedgeBase)
engineBase = ql.DiscountingSwapEngine(ytsBase)
swapHedgeBase.setPricingEngine(engineBase)
s1HedgeBase = swapHedgeBase.NPV()
return s1HedgeBase
def onTheRunSwapDelta(start_date, matDate, rateHedge, fytsBase, ytsBase, fyts, yts):
s1HedgeBase = onTheRunSwapNPV(start_date, matDate, rateHedge, fytsBase, ytsBase)
s1Hedge = onTheRunSwapNPV(start_date, matDate, rateHedge, fyts, yts)
return s1HedgeBase - s1Hedge
def onTheRunFutureNPV(maturity, rateHedge, fytsBase, ytsBase):
fraBase = ql.ForwardRateAgreement(maturity, maturity + ql.Period(3, ql.Months)
, ql.Position.Long, (100-rateHedge)/100, 1e6, ql.USDLibor(ql.Period(3, ql.Months), fytsBase), ytsBase)
fraNPVBase = fraBase.NPV()
return fraNPVBase
def onTheRunFutureDelta(maturity, rateHedge, fytsBase, ytsBase, fyts, yts):
fraHedgeBase = onTheRunFutureNPV(maturity, rateHedge, fytsBase, ytsBase)
fraHedge = onTheRunFutureNPV(maturity, rateHedge, fyts, yts)
return fraHedgeBase - fraHedge
def onTheRunDepositNPV(settlementDate, matDate, rateHedge, ytsBase):
depositBase = ql.FixedRateBond(2, ql.TARGET(), 1, settlementDate, matDate, ql.Period('1Y'), [rateHedge], ql.Actual360())
engineBase = ql.DiscountingBondEngine(ytsBase)
depositBase.setPricingEngine(engineBase)
depositNPVBase = depositBase.NPV()
return depositNPVBase
def onTheRunDepositDelta(settlementDate, matDate, rateHedge, ytsBase, yts):
depositHedgeBase = onTheRunDepositNPV(settlementDate, matDate, rateHedge, ytsBase)
depositHedge = onTheRunDepositNPV(settlementDate, matDate, rateHedge, yts)
return depositHedgeBase - depositHedge