|
| 1 | +import datetime as dt |
| 2 | +from dateutil.relativedelta import relativedelta |
| 3 | +from scipy.interpolate import interp1d |
| 4 | +import math |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +#yield curve loading |
| 8 | +data = open("Yield_Curve.txt","r").readlines() |
| 9 | +data = [a.rstrip("\n").split("\t") for a in data] |
| 10 | +days, yields, dates = [], [], [] |
| 11 | +for d, y in data: |
| 12 | + date = dt.datetime.strptime(d,"%Y-%m-%d") |
| 13 | + dates.append(date) |
| 14 | + days.append((date-dates[0]).days) |
| 15 | + yields.append(float(y)) |
| 16 | +today = dates[0] |
| 17 | + |
| 18 | +#functions |
| 19 | +def act365(date1, date2): |
| 20 | + return (date2-date1).days / 365 |
| 21 | +def act360(date1, date2): |
| 22 | + return (date2-date1).days / 360 |
| 23 | +def spotRate(date, yieldCurve): |
| 24 | + days = (date-today).days |
| 25 | + return yieldCurve(days) |
| 26 | +def df(date, yieldCurve): |
| 27 | + return math.exp(-spotRate(date,yieldCurve) |
| 28 | + * act365(today,date)) |
| 29 | +def fwdRate(date1, date2, daycounter, yieldCurve): |
| 30 | + return 1/daycounter(date1,date2)*(df(date1,yieldCurve) / df(date2,yieldCurve) -1) |
| 31 | + |
| 32 | +def irs_pricing(today, swaptype, notional, effectiveDate, terminationDate, |
| 33 | + fixedRate, fixedTenor, fixedDC, |
| 34 | + indexTenor, spread, floatingTenor, floatingDC, lastFixing, |
| 35 | + yieldCurve): |
| 36 | + c = 1 if swaptype.upper()=="PAY" else -1 |
| 37 | + #fixed leg |
| 38 | + d = effectiveDate |
| 39 | + d1 = d + fixedTenor |
| 40 | + fixedCF = [] |
| 41 | + npv = 0 |
| 42 | + while d1<=terminationDate: |
| 43 | + if d1>today: |
| 44 | + cf = notional * fixedDC(d,d1) * fixedRate |
| 45 | + fixedCF.append([d1, cf]) |
| 46 | + npv -= cf * df(d1, yieldCurve) |
| 47 | + d = d1 |
| 48 | + d1 = d + fixedTenor |
| 49 | + #floating leg |
| 50 | + d = effectiveDate |
| 51 | + d1 = d + floatingTenor |
| 52 | + floatingCF = [] |
| 53 | + while d1<=terminationDate: |
| 54 | + if d1>today: |
| 55 | + rate = lastFixing if d<today else fwdRate(d,d1,floatingDC,yieldCurve) |
| 56 | + cf = notional * floatingDC(d,d1) * rate |
| 57 | + floatingCF.append([d1, cf]) |
| 58 | + npv += cf * df(d1, yieldCurve) |
| 59 | + d = d1 |
| 60 | + d1 = d + floatingTenor |
| 61 | + return (c*npv, fixedCF, floatingCF) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +#input dates should be converted to the number of days from evaluation date |
| 67 | +#a fake curve |
| 68 | +yieldCurve = interp1d(days, yields) |
| 69 | + |
| 70 | +df_vec = np.vectorize(df) |
| 71 | +dfs = df_vec(dates,yieldCurve) |
| 72 | +#yield curve plotting |
| 73 | +''' |
| 74 | +import matplotlib.pyplot as plt |
| 75 | +fig, ax = plt.subplots(2,1,figsize=(8,8)) |
| 76 | +ax[0].plot(dates,yields,"s-") |
| 77 | +ax[0].set_title("Yield Curve") |
| 78 | +ax[1].plot(dates,dfs,"d-") |
| 79 | +ax[1].set_title("Discount Factor") |
| 80 | +fig.show() |
| 81 | +''' |
| 82 | + |
| 83 | + |
| 84 | +#swap sample pricing |
| 85 | +notional = 10000 |
| 86 | +swaptype = "Rec" |
| 87 | +today = dt.datetime(2016, 3, 16) |
| 88 | +effectiveDate = dt.datetime(2014, 1, 5) |
| 89 | +terminationDate = dt.datetime(2029, 1, 5) |
| 90 | +fixedRate = 0.02 |
| 91 | +fixedDC = act365 |
| 92 | +fixedTenor = relativedelta(months = 3) |
| 93 | +indexTenor = relativedelta(months = 6) |
| 94 | +spread = 0.001 |
| 95 | +floatingDC = act360 |
| 96 | +floatingTenor = relativedelta(months = 6) |
| 97 | +lastFixing = 0.005 |
| 98 | + |
| 99 | +price = irs_pricing(today, swaptype, notional, effectiveDate, terminationDate, |
| 100 | + fixedRate, fixedTenor, fixedDC, |
| 101 | + indexTenor, spread, floatingTenor, floatingDC, lastFixing, |
| 102 | + yieldCurve) |
| 103 | +print("price = %.3f" % price[0]) |
| 104 | + |
| 105 | +import matplotlib.pyplot as plt |
| 106 | +fig, ax = plt.subplots(2,1,figsize=(8,8)) |
| 107 | +fixed = np.array(price[1]) |
| 108 | +floating = np.array(price[2]) |
| 109 | +ax[0].bar(fixed[:,0],fixed[:,1], width=60) |
| 110 | +ax[1].bar(floating[:,0], floating[:,1], width=60) |
| 111 | +fig.show() |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
0 commit comments