Sei sulla pagina 1di 6

TermStructure / Bonds in Quantlib 1.

4
Mauricio Bedoya javierma36@gmail.com February 2014
Flat Forward yield terms structure implementation. Valuations of a Bond, and vanilla swap. For more information visit:http://quantlib.org/reference/index.html.
1

#i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e

< q l / q u a n t l i b . hpp> < b o o s t / a s s i g n / s t d / v e c t o r . hpp> <i o s t r e a m > < b o o s t / f o r e a c h . hpp>

u s i n g namespace QuantLib ; 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 ; i n t main ( i n t , c h a r [ ] ) { t r y { Calendar c a l e n d a r = TARGET( ) ; Date r e f D a t e ( 1 3 , Nov , 2 0 1 3 ) ; Date S e t t l e m e n t = c a l e n d a r . advance ( r e f D a t e , 2 Days ) ; Settings : : instance () . evaluationDate () = refDate ; Rate F l a t R a t e 1 = 0 . 0 3 ; // PAR FORWARD YIELD TERM STRUCTURE // Via Handle Handle <Quote> r a t e L e v e l ( new SimpleQuote ( F l a t R a t e 1 ) ) ; DayCounter t s d c = A c t u a l 3 6 5 F i x e d ( ) ; Handle <Yi eldT erm Str uct ure > y t s ( new FlatForward ( S e t t l e m e n t , r a t e L e v e l , t s d c ) ) ; // With P o i n t e r s b o o s t : : s h a r e d p t r <Yi eldT erm Str uct ure > p o i n t e r y t m ( new FlatForward ( r e f D a t e , rateLevel , tsdc ) ) ; // Check p a r f o r w a r d d e f i n i t i o n i n g o o g l e

11

13

15

17

19

21

23

25

27

// Y i e l d T e r m S t r u c t u r e c o n s t r u c t o r Date d a t e 1 ( 1 5 , Nov , 2 0 1 7 ) ; Date d a t e 2 ( 1 3 , Nov , 2 0 1 8 ) ; Compounding compounding ( Simple ) ; Real DF = yts > d i s c o u n t ( d a t e 1 ) ; Rate z e r o Y i e l d = yts >z e r o R a t e ( date1 , t s d c , compounding ) ; Rate f r a = yts >forwardRate ( date1 , date2 , t s d c , compounding ) ; // P r i n t Y i e l d T e r m S t r u c t u r e member f u n c t i o n s s t d : : v e c t o r <s t d : : s t r i n g > YTS Headers ( 5 ) ; YTS Headers [ 0 ] = Maximum p o s s i b l e d a t e : ; YTS Headers [ 1 ] = D i s c o u n t f a c t o r : ; YTS Headers [ 2 ] = Zero Y i e l d : ; YTS Headers [ 3 ] = FRA Rate : ; YTS Headers [ 4 ] = Term S t r u c t u r e I n f o r m a t i o n ; S i z e YTS width = YTS Headers [ 0 ] . s i z e ( ) + YTS Headers [ 1 ] . s i z e ( ) + YTS Headers [ 2 ] . s i z e ( ) + YTS Headers [ 3 ] . s i z e ( ) ; s t r i n g d b r u l ( YTS width , = ) ; cout cout cout cout cout cout << << << << << << YTS Headers [ 4 ] d b r u l << e n d l ; YTS Headers [ 0 ] YTS Headers [ 1 ] YTS Headers [ 2 ] YTS Headers [ 3 ] << e n d l ; << << << << yts >maxDate ( ) << e n d l ; DF << e n d l ; z e r o Y i e l d << \ n ; f r a << \ n ;

11

13

15

17

19

21

23

25

27

c o u t << d b r u l << e n d l ; c o u t << \ n << \ n ; / BOND PRICING / // Bond Parameters Real FaceAmount = 1 0 0 . 0 ; N a t u r a l Se tt le me nt Day s = 3 ; Frequency couponFreq = Semiannual ; Date Maturity = S e t t l e m e n t + 5 Years ; s t d : : v e c t o r <Rate> coupons ( 1 , 0 . 1 ) ; BusinessDayConvention BondConv = Unadjusted ; DayCounter BondDC = T h i r t y 3 6 0 ( T h i r t y 3 6 0 : : European ) ; S c h e d u l e BondSch ( S e t t l e m e n t , Maturity , P e r i o d ( couponFreq ) , c a l e n d a r , BondConv , BondConv , DateGeneration : : Forward , f a l s e ) ;

29

31

33

35

37

39

41

43

// Bond Engine b o o s t : : s h a r e d p t r <P r i c i n g E n g i n e > BondEngine ( new DiscountingBondEngine ( y t s ) ) ; Schedule fixedBondSchedule ( Settlement , Maturity , P e r i o d ( Semiannual ) , calendar , Unadjusted , Unadjusted , DateGeneration : : Backward , false ) ; // Bond C o n s t r u c t o r FixedRateBond Bond1 ( SettlementDays , FaceAmount , fixedBondSchedule , s t d : : v e c t o r <Rate > (1 , 0 . 0 4 5 ) , 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 ) ) ; Bond1 . s e t P r i c i n g E n g i n e ( BondEngine ) ; // Bond member f u n c t i o n s Leg CF = Bond1 . c a s h f l o w s ( ) ; Rate Y i e l d = Bond1 . y i e l d (BondDC , Simple , couponFreq ) ; Real Duration = BondFunctions : : d u r a t i o n ( Bond1 , I n t e r e s t R a t e ( Y ield , BondDC , Simple , couponFreq ) , Duration : : Simple , S e t t l e m e n t ) ; // Simple , Macaulay , M o d i f i e d Real Convexity = BondFunctions : : c o n v e x i t y ( Bond1 , I n t e r e s t R a t e ( Y ield , BondDC , Simple , couponFreq ) ) ;

11

13

15

17

19

21

23

25

27

29

31

33

35

37

// P r i n t Bond member f u n c t i o n s r e s u l t s S i z e widths [ ] = { 1 0 , 5 , 7 } ; s t d : : v e c t o r <s t d : : s t r i n g > Headers ( 9 ) ; Headers [ 0 ] = Payment Date ; Headers [ 1 ] = | ; Headers [ 2 ] = Value ; Headers [ 3 ] = Clean P r i c e : ; Headers [ 4 ] = D i r t y P r i c e : ; Headers [ 5 ] = Y i e l d : ; Headers [ 6 ] = Convexity : ; Headers [ 7 ] = Duration : ; Headers [ 8 ] = Bond I n f o r m a t i o n ; S i z e width = w i d t h s [ 0 ] + w i d t h s [ 1 ] + w i d t h s [ 2 ] + Headers [ 0 ] . s i z e ( ) + Headers [ 1 ] . s i z e ( ) + Headers [ 2 ] . s i z e ( ) ; s t r i n g d b r u l e ( width , = ) ; c o u t << Headers [ 8 ] << e n d l ; c o u t << d b r u l e << e n d l ;

39

41

43

f o r ( auto i t = 0 ; i t !=CF . s i z e ( ) ; ++i t ) { c o u t << setw ( w i d t h s [ 0 ] ) << CF [ i t ]> d a t e ( ) << setw ( w i d t h s [ 1 ] ) << Headers [ 1 ] << setw ( w i d t h s [ 2 ] ) << CF [ i t ]> amount ( ) << e n d l ; } c o u t << d b r u l e << e n d l ; cout cout cout cout cout << << << << << Headers [ 3 ] Headers [ 4 ] Headers [ 5 ] Headers [ 6 ] Headers [ 7 ] << << << << << Bond1 . c l e a n P r i c e ( ) << e n d l ; Bond1 . d i r t y P r i c e ( ) << e n d l ; Y i e l d << e n d l ; Convexity << e n d l ; Duration << e n d l ;

11

13

/ SWAP / // Term s t r u c t u r e s t h a t w i l l be used f o r p r i c i n g : // t h e one used f o r d i s c o u n t i n g c a s h f l o w s R e l i n k a b l e H a n d l e <Yi eld Term Str uct ure > d i s c o u n t i n g T e r m S t r u c t u r e ; // t h e one used f o r f o r w a r d r a t e f o r e c a s t i n g R e l i n k a b l e H a n d l e <Yi eld Term Str uct ure > f o r e c a s t i n g T e r m S t r u c t u r e ; // Swap Parameters Real Snominal = 1 0 0 0 0 0 0 0 . 0 ; Integer lenghtInYears = 1; Date SMaturity = S e t t l e m e n t + l e n g h t I n Y e a r s Years ; VanillaSwap : : Type SwapType = VanillaSwap : : Payer ; // Fixed l e g Frequency f i x e d L e g F r e q u e n c y = Annual ; BusinessDayConvention f i x e d L e g C o n v e n t i o n = Unadjusted ; DayCounter fixedLegDayCounter = T h i r t y 3 6 0 ( T h i r t y 3 6 0 : : European ) ; Rate f i x e d R a t e = 0 . 0 2 ; S c h e d u l e f i x e d S c h e d u l e ( S e t t l e m e n t , SMaturity , P e r i o d ( f i x e d L e g F r e q u e n c y ) , c a l e n d a r , f i x e d L e g C o n v e n t i o n , f i x e d L e g C o n v e n t i o n , DateGeneration : : Forward , f a l s e ); // F l o a t i n g l e g 1 Frequency f l o a t i n g L e g F r e q u e n c y = Semiannual ; BusinessDayConvention f l o a t i n g L e g C o n v e n t i o n = M o d i f i e d F o l l o w i n g ; DayCounter f l o a t i n g L e g D a y C o u n t e r = Actual360 ( ) ; b o o s t : : s h a r e d p t r <I b o r I n d e x > e u r i b o r I n d e x ( new Euribor6M ( forecastingTermStructure ) ) ; Spread s p r e a d = 0 . 0 ; S c h e d u l e f l o a t S c h e d u l e ( S e t t l e m e n t , Maturity , P e r i o d ( f l o a t i n g L e g F r e q u e n c y ) , c a l e n d a r , f l o a t i n g L e g C o n v e n t i o n , f l o a t i n g L e g C o n v e n t i o n , DateGeneration : : Forward , false ) ;

15

17

19

21

23

25

27

29

31

33

35

37

39

// Swap C o n s t r u c t o r VanillaSwap spot1yearsSwap ( SwapType , Snominal , f i x e d S c h e d u l e , f i x e d R a t e , fixedLegDayCounter , f l o a t S c h e d u l e , e u r i b o r I n d e x , spread , floatingLegDayCounter ) ; // P r i n t Swap R e s u l t s s t d : : v e c t o r <s t d : : s t r i n g > h e a d e r s ( 4 ) ; h e a d e r s [ 0 ] = Term S t r u c t u r e ; h e a d e r s [ 1 ] = Net P r e s e n t Value ; headers [ 2 ] = f a i r spread ; headers [ 3 ] = f a i r f ixed rate ; std : : s t r i n g separator = | ; S i z e Swap width = h e a d e r s [ 0 ] . s i z e ( ) + s e p a r a t o r . s i z e ( ) + headers [ 1 ] . s i z e () + separator . s i z e () + headers [ 2 ] . s i z e () + separator . s i z e () + headers [ 3 ] . s i z e () + separator . s i z e () 1; s t d : : s t r i n g r u l e ( Swap width , ) ; s t d : : s t r i n g Swap dbrule ( Swap width , = ) , tab ( 8 , ) ; b o o s t : : s h a r e d p t r <P r i c i n g E n g i n e > SwapEngine ( new DiscountingSwapEngine ( discountingTermStructure ) ) ; spot1yearsSwap . s e t P r i c i n g E n g i n e ( SwapEngine ) ; // Curve forecastingTermStructure . linkTo ( pointer ytm ) ; discountingTermStructure . linkTo ( pointer ytm ) ; // Swam Member f u n c t i o n s Real NPV; Rate f a i r R a t e ; Spread f a i r S p r e a d ; NPV = spot1yearsSwap .NPV( ) ; f a i r R a t e = spot1yearsSwap . f a i r R a t e ( ) ; f a i r S p r e a d = spot1yearsSwap . f a i r S p r e a d ( ) ; cout cout cout cout cout << << << << << d b r u l << e n d l ; s t d : : setw ( h e a d e r s s t d : : setw ( h e a d e r s s t d : : setw ( h e a d e r s s t d : : setw ( h e a d e r s

10

12

14

16

18

20

22

24

26

28

30

32

34

36

38

40

[0]. [1]. [2]. [3].

size size size size

( ) )<< << s e p a r a t o r ; ( ) ) << NPV << s e p a r a t o r ; ( ) ) << Spread << s e p a r a t o r ; ( ) ) << Swap Rate << s e p a r a t o r << e n d l ;

42

44

c o u t << s t d : : setw ( h e a d e r s c o u t << s t d : : setw ( h e a d e r s NPV << s e p a r a t o r ; c o u t << s t d : : setw ( h e a d e r s c o u t << s t d : : setw ( h e a d e r s ;

[ 0 ] . s i z e ( ) )<< Swap << s e p a r a t o r ; [ 1 ] . s i z e ( ) ) << s t d : : f i x e d << s t d : : s e t p r e c i s i o n ( 2 ) << [ 2 ] . s i z e ( ) ) << i o : : r a t e ( f a i r S p r e a d ) << s e p a r a t o r ; [ 3 ] . s i z e ( ) ) << i o : : r a t e ( f a i r R a t e ) << s e p a r a t o r << e n d l

return 0; } c a t c h ( e x c e p t i o n& e ) { s t d : : c o u t << e . what ( ) << s t d : : e n d l ; return 1; } catch ( . . . ) { s t d : : c e r r << Unknow e r r o r << s t d : : e n d l ; return 1; } }

Potrebbero piacerti anche