-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyABBR.py
More file actions
201 lines (190 loc) · 7.31 KB
/
myABBR.py
File metadata and controls
201 lines (190 loc) · 7.31 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#---- A. ライブラリーインポート ----
import QuantLib as ql
import datetime as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import warnings #警告の非表示用(pandas ilocで止める)
from functools import singledispatch #関数オーバーロード用
#---- B. matplotlib初期設定 ----
# 日本語フォントとサイズ、グラフサイズ
plt.rcParams.update({"font.family" :"MS Gothic",
"font.size" :9,
"figure.figsize":[4.5,2.5]})
#---- C. numpy初期設定 5桁表示と配列短縮形, 切捨て ----
np.set_printoptions(precision=5,suppress=True)
def nSetP(dgt=5): # %5桁表示設定
fmt = '{:.' + str(dgt) + '%}'
np.set_printoptions(formatter={'float':fmt.format})
def nSetF(dgt=5): # float5桁表示設定
np.set_printoptions(precision=dgt,suppress=True)
def nA(LIST): return np.array(LIST)
def rD(xx,digits=0): return np.floor(xx * 10**digits)/10**digits #切捨て
def rU(xx,digits=0): return np.ceil (xx * 10**digits)/10**digits #切上げ
#---- D. pandas スタイル書式用変数, 日付列変換, df表示 ----
fmS = {'amount' :'{:,.2f}', 'atmFWD':'{:.6%}' , 'coupon':'{:.6%}' ,
'days' :'{:.0f}' , 'DF' :'{:.8f}' , 'fwdRT' :'{:.6%}' ,
'nominal':'{:,.2f}', 'NPV' :'{:,.2f}', 'matYR' :'{:,.4f}',
'parRT' :'{:.6%}' , 'rate' :'{:.6%}' , 'rfrDF' :'{:.8f}' ,
'shftRT' :'{:.6%}' , 'spread':'{:.3%}' , 'zeroRT':'{:.6%}' ,}
fmB = {'accruAMT':'{:,.4f}', 'amount' :'{:,.4f}', 'BPV' :'{:.4f}',
'CF' :'{:.5f}' , 'cleanPRC':'{:.4f}' ,'coupon':'{:.4%}',
'dirtyPRC':'{:.4f}' , 'gBASIS' :'{:.4f}' , 'yield':'{:.4f}', }
fmtSCF, fmtFUT = fmS, fmB # for old vari.
def isoDT(dateCOL):
return dateCOL.map(lambda x: x.ISO() if not pd.isna(x) else x)
def qlDT(dateCOL) :
return dateCOL.map(lambda x: iDT(x) if not pd.isna(x) else x)
def dfDSP(df, n=5, fm=fmS): # n: numbers of line, fm: format vari.
nRow = min(n, (len(df)+1)//2 )
sty = pd.concat([df.head(nRow),df.tail(nRow)]).style
sty = sty.format(fm); display(sty)
#---- E. 日付関連メソッドの短縮形 ----
# Days, Weeks, Months, Years
DD = ql.Days; WW = ql.Weeks; MM = ql.Months; YY = ql.Years
# euro日付
def eDT(dd,mm,yyyy): return ql.Date(dd,mm,yyyy)
# japan日付
def jDT(yyyy,mm,dd): return ql.Date(dd,mm,yyyy)
# us日付
def uDT(mm,dd,yyyy): return ql.Date(dd,mm,yyyy)
# datetimeクラスからQL Date
def dDT(dateTIME): return ql.Date().from_date(dateTIME)
# iso日付
def iDT(isoDT): return ql.Date(isoDT, '%Y-%m-%d')
# 曜日 day of week
def dWK(Date): return Date.to_date().strftime('%a')
# xxx.advance( , , DD)等
def adD(cal,dt,nn) : return cal.advance(dt, nn, DD)
def adW(cal,dt,nn) : return cal.advance(dt, nn, WW)
def adM(cal,dt,nn) : return cal.advance(dt, nn, MM)
def adY(cal,dt,nn) : return cal.advance(dt, nn, YY)
# 月初と月末の日付、月の日数
def bDTmm(d) : return ql.Date(1, d.month(), d.year())
def eDTmm(d) : return d.endOfMonth(d)
def dsMM(d) : return ql.Date.endOfMonth(d).dayOfMonth()
# SettingクラスevaluationDate設定、取得
def setEvDT(evaluationDT):
ql.Settings.instance().evaluationDate = evaluationDT
def getEvDT(): return ql.Settings.instance().evaluationDate
# Period 3種類の短縮形 (2番目はタプルが引数)
@singledispatch
def pD(pdSTR: str): return ql.Period(pdSTR) # Period('3M')
@pD.register(tuple)
def _(nnUNT): return ql.Period(*nnUNT) # Period((3,MM))
@pD.register(int)
def _(FRQ): return ql.Period(FRQ) # Period(freqQ)
#---- F. 短縮形リスト ----
# Calendar
calJP = ql.Japan()
calEU = ql.TARGET()
calUSf = ql.UnitedStates(ql.UnitedStates.FederalReserve)
calUSg = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
calUSs = ql.UnitedStates(ql.UnitedStates.SOFR)
calWK = ql.WeekendsOnly()
calNL = ql.NullCalendar()
# DayCounter
dcA365 = ql.Actual365Fixed()
dcA365n = ql.Actual365Fixed(ql.Actual365Fixed.NoLeap)
dcA360 = ql.Actual360() # includeLastDay=false
dcA360t = ql.Actual360(True) # for CDS
dc30 = ql.Thirty360(ql.Thirty360.BondBasis)
dc30e = ql.Thirty360(ql.Thirty360.European)
dcAA = ql.ActualActual(ql.ActualActual.ISDA)
dcAAb = ql.ActualActual(ql.ActualActual.Bond)
# T + Business days (settle days)
Tp0 = 0
Tp1 = 1
Tp2 = 2
Tp3 = 3
# freqency
frqA = ql.Annual # 1
frqSA = ql.Semiannual # 2
frqQ = ql.Quarterly # 4
frqM = ql.Monthly # 12
frqD = ql.Daily # 365
# OLD-freqency
freqA = ql.Annual # 1
freqSA = ql.Semiannual # 2
freqQ = ql.Quarterly # 4
freqM = ql.Monthly # 12
freqD = ql.Daily # 365
# tenor (period version for freq)
pDfrqA = ql.Period(ql.Annual) # 1Y
pDfrqSA= ql.Period(ql.Semiannual) # 6M
pDfrqQ = ql.Period(ql.Quarterly) # 3M
pDfrqM = ql.Period(ql.Monthly) # 1M
pDfrqD = ql.Period(ql.Daily) # 1D
# OLD-tenor
pdFreqA = ql.Period(ql.Annual) # 1Y
pdFreqSA= ql.Period(ql.Semiannual) # 6M
pdFreqQ = ql.Period(ql.Quarterly) # 3M
pdFreqM = ql.Period(ql.Monthly) # 1M
pdFreqD = ql.Period(ql.Daily) # 1D
# convension
mFLLW = ql.ModifiedFollowing
FLLW = ql.Following
unADJ = ql.Unadjusted
# date generation
dtGENb = ql.DateGeneration.Backward
dtGENf = ql.DateGeneration.Forward
dtGEN20 = ql.DateGeneration.TwentiethIMM
dtGENc = ql.DateGeneration.CDS
dtGEN15 = ql.DateGeneration.CDS2015
# end of month
EoMf = False
EoMt = True
# compound
CMP = ql.Compounded
CNT = ql.Continuous
SPL = ql.Simple
# OLD-compound
cmpdCMP = ql.Compounded
cmpdCNT = ql.Continuous
cmpdSPL = ql.Simple
# swap: pay/recieve, put/call
swPAY = ql.Swap.Payer # 1
swRCV = ql.Swap.Receiver # -1
cpnRT0 = 0.0
spdRT0 = 0.0
gr1 = 1.0 # gearing
# currency
jpyFX = ql.JPYCurrency()
usdFX = ql.USDCurrency()
eurFX = ql.EURCurrency()
# CDS : recovery rate / coupon
rcvRTz = 0.0 # zero
rcvRTj = 0.35 # Japan
rcvRTu = 0.40 # US
rcvRTs = 0.20 # subordinate
cpn025 = 0.0025
cpn100 = 0.01
cpn500 = 0.05
bP = ql.Protection.Buyer # 0
sP = ql.Protection.Seller # 1
# Lag
lag0d = 0
lag1d = 1
lag2d = 2
lag3M = ql.Period('3M')
lag0M = ql.Period('0M')
# bond, CPI, クリーン価格等
parPR = 100.0
parAMT = 100.0
cpiLNR = ql.CPI.Linear
cpiFLT = ql.CPI.Flat
ds9 = 9 # shift days for JGBI
gwOLY = False
reviseF = False
jpRegion = ql.CustomRegion("Japan", "JP")
usRegion = ql.CustomRegion("USA", "US")
def cP(prc): return ql.BondPrice(prc, ql.BondPrice.Clean)
def dP(prc): return ql.BondPrice(prc, ql.BondPrice.Dirty)
# percent and basis points
pct = 1e-2
bps = 1e-4
# シンプルクォート、シンプルキャッシュフロー
def sQ(xx): return ql.SimpleQuote(xx)
def sQH(xx): return ql.QuoteHandle(sQ(xx))
def sCF(amt,date): return ql.SimpleCashFlow(amt, date)