Termstructure Spread Functions in Quantlib 1.4: Mauricio Bedoya February 2014

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

TermStructure Spread Functions in Quantlib 1.

4
Mauricio Bedoya [email protected] February 2014

11

13

15

17

19

21

23

25

27

29

/ mode : c++; tab width : 4 ; i n d e n t tabs mode : n i l ; cb a s i c o f f s e t : 4 / / This example s e t s up a c a l l a b l e f i x e d r a t e bond with a H u l l White p r i c i n g e n g i n e and compares t o Bloomberg s H u l l White p r i c e / y i e l d c a l c u l a t i o n s . / #i f d e f BOOST MSVC / Uncomment t h e f o l l o w i n g l i n e s t o unmask f l o a t i n g p o i n t e x c e p t i o n s . Warning : u n p r e d i c t a b l e r e s u l t s can a r i s e . . . See h t t p : / /www. w i l m o t t . com/ messageview . cfm ? c a t i d =10&t h r e a d i d =9481 I s t h e r e anyone with a d e f i n i t i v e word about t h i s ? / // #i n c l u d e < f l o a t . h> // namespace { u n s i g n e d i n t u = c o n t r o l f p ( EM INEXACT, MCW EM) ; } #e n d i f #i n c l u d e < q l / q u a n t l i b . hpp> #i n c l u d e < v e c t o r > #i n c l u d e <cmath> #i n c l u d e <iomanip > #i n c l u d e < i o s t r e a m > #i n c l u d e < b o o s t / t i m e r . hpp> #i n c l u d e < b o o s t / a s s i g n / s t d / v e c t o r . hpp> #i n c l u d e < c a s s e r t > u s i n g namespace s t d ; u s i n g namespace b o o s t : : a s s i g n ; u s i n g namespace QuantLib ; #i f d e f i n e d (QL ENABLE SESSIONS) namespace QuantLib { Integer sessionId () { return 0; } } #e n d i f i n t main ( i n t , c h a r [ ] ) {

31

try { / MARKET DATA / Calendar c a l e n d a r = N u l l C a l e n d a r ( ) ; Date S e t t l e m e n t D a t e ( 1 , October , 2 0 1 3 ) ; s t d : : v e c t o r <Rate> MarketRate ; MarketRate += 0 . 0 3 5 , 0 . 0 4 0 1 , 0 . 0 4 5 3 1 ; Handle <Quote> d1yRate ( new SimpleQuote ( MarketRate [ 0 ] ) ) ; Handle <Quote> d2yRate ( new SimpleQuote ( MarketRate [ 1 ] ) ) ; Handle <Quote> d3yRate ( new SimpleQuote ( MarketRate [ 2 ] ) ) ; // Swap Rate H e l p e r I n f o r m a t i o n Frequency swFixedLegFrequency = Annual ; BusinessDayConvention swFixedLegConvention = Unadjusted ; DayCounter swFixedLegDayCounter = T h i r t y 3 6 0 ( T h i r t y 3 6 0 : : European ) ; b o o s t : : s h a r e d p t r <I b o r I n d e x > s w F l o a t i n g L e g I n d e x ( new Euribor6M ) ; c o n s t P e r i o d f o r w a r d S t a r t ( 1 Days ) ; // Rate C o n s t r u c t o r b o o s t : : s h a r e d p t r <RateHelper > d1y ( new SwapRateHelper ( Handle <Quote >(d1yRate ) , 1 Years , c a l e n d a r , swFixedLegFrequency , swFixedLegConvention , swFixedLegDayCounter , s w F l o a t i n g L e g I n d e x , Handle <Quote > () , f o r w a r d S t a r t ) ) ; b o o s t : : s h a r e d p t r <RateHelper > d2y ( new SwapRateHelper ( Handle <Quote >(d2yRate ) , 2 Years , c a l e n d a r , swFixedLegFrequency , swFixedLegConvention , swFixedLegDayCounter , s w F l o a t i n g L e g I n d e x , Handle <Quote > () , f o r w a r d S t a r t ) ) ; b o o s t : : s h a r e d p t r <RateHelper > d3y ( newSwapRateHelper ( Handle <Quote >(d2yRate ) , 3 Years , c a l e n d a r , swFixedLegFrequency , swFixedLegConvention , swFixedLegDayCounter , s w F l o a t i n g L e g I n d e x , Handle <Quote > () , f o r w a r d S t a r t ) ) ; / TERM STRUCTOR CONSTRUCTORS ( Same Spread t o a l l m a t u r i t i e s ) / // TERM STRUCTURE HELPERS DayCounter TS DC = A c t u a l A c t u a l ( A c t u a l A c t u a l : : Bond ) ; s t d : : v e c t o r <b o o s t : : s h a r e d p t r <RateHelper > > I n s t r u m e n t ; I n s t r u m e n t += d1y , d2y , d3y ; // FLAT TERMSTRUCTURE b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > FLAT( new FlatForward ( SettlementDate , d1yRate , A c t u a l A c t u a l ( ) , Compounded , Annual ) ) ; Handle <Yi eld Ter mStr uct ure > Handle FLAT ( new FlatForward ( SettlementDate , d1yRate , A c t u a l A c t u a l ( ) , Compounded , Annual ) ) ;

10

12

14

16

18

20

22

24

26

28

30

32

34

36

38

40

// ADD AN SPREAD TO FLAT Spread s p r e a d = 0 . 0 5 ; Handle <Quote> spreadQuote ( new SimpleQuote ( s p r e a d ) ) ; b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > NEWFLAT( new ZeroSpreadedTermStructure ( Handle FLAT , spreadQuote ) ) ; Handle <Yi eld Ter mStr uct ure > Handle NEWFLAT( new ZeroSpreadedTermStructure ( Handle FLAT , spreadQuote ) ) ; // CUSTOM TERMSTRUCTURE ( u s e p i e c e w i s e y i e l d c u r v e ) b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > CUSTOM TS( new P i e c e w i s e Y i e l d C u r v e < Discount , L i n e a r >( SettlementDate , Instrument , TS DC) ) ; Handle <Yi eld Ter mStr uct ure > Handle CUSTOM TS ( new P i e c e w i s e Y i e l d C u r v e < Discount , L i n e a r >( SettlementDate , Instrument , TS DC) ) ; // ADD AN SPREAD TO CUSTOM TERMSTRUCTURE b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > NEW CUSTOM TS( new ZeroSpreadedTermStructure ( Handle CUSTOM TS , spreadQuote ) ) ; Handle <Yi eld Ter mStr uct ure > Handle NEW CUSTOM TS( new ZeroSpreadedTermStructure ( Handle CUSTOM TS , spreadQuote ) ) ; / Some O b j e c t s a r e implementes with Handle , Quotes , and s o on . Check type i n Q u a n t l i b Page / / TERM STRUCTOR CONSTRUCTORS ( Vector o f Dates / S p r e a d s ) / s t d : : v e c t o r <Date> S p r e a d D a t e s ; S p r e a d D a t e s += Date ( 1 , Oct , 2 0 1 4 ) , Date ( 1 , Oct , 2 0 1 5 ) , Date ( 1 , Oct , 2 0 1 6 ) ; s t d : : v e c t o r <Spread > S p r e a d v a l u e s ; S p r e a d v a l u e s += 0 . 0 0 5 5 , 0 . 0 0 1 2 , 0 . 0 0 8 7 ; Handle <Quote> s 1 ( new SimpleQuote ( S p r e a d v a l u e s [ 0 ] ) ) ; Handle <Quote> s 2 ( new SimpleQuote ( S p r e a d v a l u e s [ 1 ] ) ) ; Handle <Quote> s 3 ( new SimpleQuote ( S p r e a d v a l u e s [ 2 ] ) ) ; s t d : : v e c t o r <Handle <Quote> > S p r e a d V e c t o r Q u o t e s ; S p r e a d V e c t o r Q u o t e s += s1 , s2 , s 3 ; // ADD A NON CONSTANT SPREAD TO CUSTOM TERMSTRUCTURE b o o s t : : s h a r e d p t r <Yi eld Ter mStr uct ure > CUSTOM2 TS( new I n t e r p o l a t e d P i e c e w i s e Z e r o S p r e a d e d T e r m S t r u c t u r e <L i n e a r >(Handle CUSTOM TS , Spread Vector Quotes , Spread Dates ) ) ;

11

13

15

17

19

21

23

25

27

29

31

33

35

37

Handle <Yi eld Ter mStr uct ure > Handle CUSTOM2 TS ( new I n t e r p o l a t e d P i e c e w i s e Z e r o S p r e a d e d T e r m S t r u c t u r e <L i n e a r >(Handle CUSTOM TS , Spread Vector Quotes , Spread Dates ) ) ; return 0; } c a t c h ( s t d : : e x c e p t i o n& e ) { s t d : : c e r r << e . what ( ) << s t d : : e n d l ; return 1; } catch ( . . . ) { s t d : : c e r r << unknown e r r o r << s t d : : e n d l ; return 1; } }

11

Now you can use the new curve to price instruments.

You might also like