// This file is a script that can be executed with the F# Interactive.  
// It can be used to explore and test the library project.
// Note that script files will not be part of the project build.

#r "bin/Debug/Cephei.Core.dll"
#r "bin/Debug/Cephei.QL101.dll"
#r "../Cephei.QL/Debug/Cephei.QL101.impl.dll"
#r "bin/Debug/Cephei.Fun.dll"

open System
open Cephei

//
// Create a Yield Curve object with domain market data
//
let YieldCurve months quotes referenceDate = 
    let calendar =  Fun.Times.Calendars.TARGET.Create ()
    let depositVector = 
        let depositRates =
            let quote v = Fun.Quotes.SimpleQuote.Create (Some v)
            let three_months = Fun.Times.Period.Create (3, QL.Times.TimeUnitEnum.Months)
            let day_counter = Fun.Times.Daycounters.Actual365Fixed.Create ()
            let modfollow = QL.Times.BusinessDayConventionEnum.ModifiedFollowing
            let periods =
                let period p =
                    Fun.Times.Period.Create (p, QL.Times.TimeUnitEnum.Months)
                Array.map (period) months
            let helper v m = 
                let fixingDays = 3u
                Fun.Termstructures.Yield.DepositRateHelper.Create 
                    ( quote(v)
                    , m
                    , fixingDays
                    , calendar
                    , modfollow
                    , true
                    , day_counter
                    ) :> QL.Termstructures.Yield.IRateHelper 
            Array.map2 (helper) quotes periods
        Fun.Termstructures.Yield.RateHelper.CreateVector (depositRates)
    let ActActISDA = 
        Fun.Times.Daycounters.ActualActual.Create (Some QL.Times.Daycounters.ActualActual.ConventionEnum.ISDA) 
    let tolerance = 1.0e-15  
    let refDate = calendar.Adjust (referenceDate, Some QL.Times.BusinessDayConventionEnum.ModifiedFollowing)
    Fun.Termstructures.Yield.PiecewiseYieldCurveDiscountLogLinear.Create 
        ( refDate
        , depositVector
        , ActActISDA
        , tolerance
        ) :> QL.Termstructures.IYieldTermStructure

//
// Define a Zero Coupon Bond
//
let ZeroCouponBond curve maturityDate issueDate faceAmount redeption =
    let pricer = 
        Fun.Pricingengines.Bond.DiscountingBondEngine.Create (Some (curve), None)
    let usCalendar = Fun.Times.Calendars.UnitedStates.Create (Some QL.Times.Calendars.UnitedStates.MarketEnum.GovernmentBond)
    let matDate = usCalendar.Adjust (maturityDate, Some QL.Times.BusinessDayConventionEnum.ModifiedFollowing )
    let isDate = usCalendar.Adjust (issueDate, Some QL.Times.BusinessDayConventionEnum.ModifiedFollowing )
    let paymentConvention = Some QL.Times.BusinessDayConventionEnum.Following
    let settlementDays = 3u
    Fun.Instruments.Bonds.ZeroCouponBond.Create 
        ( settlementDays
        , usCalendar
        , faceAmount
        , matDate
        , paymentConvention
        , Some redeption
        , Some isDate
        , pricer
        ) :> QL.Instruments.IBond 

//
// Define a Fixed Rate Bond
//
let FixedRateBond curve effectiveDate termDate faceAmount coupons = 
    let settlementDays = 3u
    let pricer = 
        Fun.Pricingengines.Bond.DiscountingBondEngine.Create (Some (curve), None)
    let fixedBondSchedule = 
        let usCalendar = Fun.Times.Calendars.UnitedStates.Create (Some QL.Times.Calendars.UnitedStates.MarketEnum.GovernmentBond)
        let fixedPeriod = Fun.Times.Period.Create (QL.Times.FrequencyEnum.Semiannual)
        let Unajusted = QL.Times.BusinessDayConventionEnum.Unadjusted 
        let backwards = QL.Times.DateGeneration.RuleEnum.Backward 
        Fun.Times.Schedule.Create 
            ( effectiveDate 
            , termDate 
            , fixedPeriod 
            , usCalendar 
            , Unajusted 
            , Unajusted 
            , backwards 
            , false 
            , None 
            , None
            )
    let couponsV = Fun.Doubles.CreateVector (coupons)
    let actualActual = Fun.Times.Daycounters.ActualActual.Create (Some QL.Times.Daycounters.ActualActual.ConventionEnum.Bond)
    let modFollow = QL.Times.BusinessDayConventionEnum.ModifiedFollowing 
    Fun.Instruments.Bonds.FixedRateBond.Create 
        ( settlementDays 
        , faceAmount 
        , fixedBondSchedule 
        , couponsV 
        , actualActual 
        , Some modFollow 
        , Some faceAmount 
        , Some effectiveDate  
        , pricer  
        ) :> QL.Instruments.IBond 

//
// Report Functions
//
let NPVs (bl : QL.Instruments.IBond list) = 
    List.map (fun (b : QL.Instruments.IBond) -> b.NPV) bl

let CleanPrices (bl : QL.Instruments.IBond list) = 
    List.map (fun (b : QL.Instruments.IBond) -> b.CleanPrice() ) bl

let DirtyPrices (bl : QL.Instruments.IBond list) = 
    List.map (fun (b : QL.Instruments.IBond) -> b.DirtyPrice() ) bl

//
// Sample Data
//

let OurCurve = 
    let months = [| 3; 6; 12; 24; 375 |]
    let quotes = [| 0.0096; 0.0145; 0.0194; 2.0; 2.5 |]
    let refDate = DateTime.Now.AddDays -2.0
    YieldCurve months quotes refDate 

let zbond1 = 
    let curve = OurCurve
    let maturity = DateTime(2013,1,1)
    let issue = DateTime(2011,1,1)
    let face = 100.0
    let mature = 116.0
    ZeroCouponBond curve maturity issue face mature

let fbond1 = 
    let curve = OurCurve
    let term = DateTime(2013,1,1)
    let effect = DateTime(2011,1,1)
    let face = 100.0
    let coupons = [|1.0; 0.045|]
    FixedRateBond curve effect term face coupons

//
// Report
//
let Report = 
    let bonds = [zbond1 ; fbond1]
    let layout = 
        [ sprintf "    %A\n" [ "Zero"    ; "Fixed" ]
        ; sprintf "NPV %A\n" (NPVs bonds)
        ; sprintf "CP  %A\n"  (CleanPrices bonds)
        ; sprintf "DP  %A\n"  (DirtyPrices bonds)
        ]
    List.map (printf "%s") layout

Last edited May 13, 2011 at 11:05 PM by channell, version 4

Comments

No comments yet.