QBSBondOption Example CS





http://www.QuantTools.com
CapeTools Bond Options function list
QBSBondOption function

Welcome | Documentation format | QuantTools Groups | QuantTools Categories | Licence

Key TAGs | Excel Index | API Index



Example C# Driver function. Preparing the parameters, sub-function calls and the final function call (the result).

High level view of the code structure (resulting in the final function call to QBSBondOption() )

These are the financial QuantTools function calls that are used within the examples :





The objects generated by these functions are inter-connected in the following way :




C# Example - QBSBondOption





    //     ##################################################################################
    //     The first function here QBSBondOption(), contains a series of
    //     function calls leading upto the main function call, the second function
    //     within this file ( QBSBondOptionPart() ).
    //     which contains the answer that we are looking for.

    //     The first function here is simply an example of how to construct the parameters 
    //     in order acquire either a string Key (that is to be passed to other functions) 
    //     or a computed result.

    //     If you are viewing this source code from the chm or web help file you can use the
    //     outlining features to collapse certain sections of the code for better readability. 
    //     ##################################################################################
    

using System;

// Optional using instruction. We will use a mix of utilising fully qualified names (in the case of the financial objects)
// and using the reduced version (in the case of declaring enumerations).
// This is just to demostrate both types of coding.

using CTQL; // You need to add a reference to the QuantToolsNET.v2.dll also

// Some global parameter in order to append to user defined keys.
// We use it here to ensure that we have unique Keys (in the case several of our examples
// use the same key-name)
// In normal use, a user defined string will be used and so this variable will be pointless.
static int nCTBondOptionsGlobal;
    
// Used by function parameters that take an optional range value. 
// In Excel we simply omit the value, within the API functions, 
// we pass an empty range object
static CTQL.CTRangeData oEmptyRange = new CTQL.CTRangeData();
    
public string CS_EX_QBSBondOption()
{
    nCTBondOptionsGlobal += 1;
            
    string szErrorMsg = "";

    try
    {


    //    Creates an European Exercise object.
    

    string MyEuropeanExercise;
    MyEuropeanExercise = 
        EuropeanExercisePart();
    
    


    //    Creates a generic calendar object.
    

    string MyNewCalendar;
    MyNewCalendar = 
        GenericCalendarPart();
    
    


    //    Creates a fixed coupon bond object.
    
    string MyFixedCouponBond_y;
    MyFixedCouponBond_y = 
        FixedCouponBond_yPart(
        MyNewCalendar);
    
    


    //    Creates a BondOption object that will price the option via a 
    //    Black Scholes methodology.
    
    string MyQBSBondOption;
    MyQBSBondOption = 
        QBSBondOptionPart(
        MyFixedCouponBond_y,
        MyEuropeanExercise);
    
    // This is the result we are looking for.
    return MyQBSBondOption;
    

    }
    catch(Exception e)
    {
        szErrorMsg = e.Message;
        throw e;
    }
                        
}                
        


// ///////////////////////////////////////////////////////////////////

private string QBSBondOptionPart(
    string MyFixedCouponBond_y,
    string MyEuropeanExercise)
{



    //    Key Handle to be used for the new Bond object.
        string MyQBSBondOption = "MyQBSBondOption" + "_" + System.Convert.ToString(nCTBondOptionsGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Type of option (C)all or (P)ut.
        string OptionType = "Call";

    //    The exercise price of the BondOption.
        double exercisePrice = 101;

    //    The volatility of the Bond.
        double Vol = 0.2;

    //    The type of volatility passed in : Either 'Yield' or 'Price' 
    //    vol.
        string VolType = "Yield";

    //    A repurchase rate (repo rate) value.
        double reporate = 0.055;

    //    The compounding of the repurchase rate (repo rate) passed in.
        CTIEnums.COMPEnum compounding = CTIEnums.COMPEnum.COMP_Compounded;

    //    Frequency of the repo rate.
        CTIEnums.FreqEnum Freq = CTIEnums.FreqEnum.Freq_annual;

    //    The DayCounter of the repo rate.
        CTIEnums.DayCountEnum DayCount = CTIEnums.DayCountEnum.DayCount_actual360;

                    
    //  Excel function call would be this - "CT.BNDOPT.QBSBondOption()"

    //    Creates a BondOption object that will price the option via a 
    //    Black Scholes methodology.
        string rQBSBondOption;
                                        
        rQBSBondOption = CTQL.CTBondOptionsSA.QBSBondOption(
                MyQBSBondOption,
                Reload,
                MyFixedCouponBond_y,
                OptionType,
                MyEuropeanExercise,
                exercisePrice,
                Vol,
                VolType,
                reporate,
                compounding,
                Freq,
                DayCount);


    return rQBSBondOption;
}        



// ///////////////////////////////////////////////////////////////////

private string FixedCouponBond_yPart(
    string MyNewCalendar)
{

        //  Create example range for parameter FixedCouponBond_y_coupons
        CTQL.CTRangeData FixedCouponBond_y_coupons;    
        
        FixedCouponBond_y_coupons = new  CTQL.CTRangeData(0.05);


    //    Key Handle to be used for the new Bond object.
        string MyFixedCouponBond_y = "MyFixedCouponBond_y" + "_" + System.Convert.ToString(nCTBondOptionsGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    The currency code that you want to associate with the Bond.
        CTIEnums.CCYEnum Ccy = CTIEnums.CCYEnum.CCY_EUR;

    //    Settlement date of the Bond.
        CTQL.Date SettleDate =  new CTQL.Date("19/7/2005", "dd/mm/yyyy");

    //    Dated date of the bond.
        CTQL.Date datedDate =  new CTQL.Date("21/4/2005", "dd/mm/yyyy");

    //    First Coupon Date: Date that the first coupon is paid (if bond 
    //    does not have an odd first period, leave blank or enter 0).
        int firstCouponDate = 0;

    //    Penultimate Coupon Date: Date that the penultimate coupon is 
    //    paid (if bond does not have an odd last period, leave blank 
    //    or enter 0).
        int penultCouponDate = 0;

    //    Maturity date of the bond.
        CTQL.Date matDate =  new CTQL.Date("21/4/2015", "dd/mm/yyyy");

    //    The ex-dividend tenor.
        string exDivTenor = "3BD";

    //    Coupon Frequency of the bond.
        CTIEnums.FreqEnum couponFreq = CTIEnums.FreqEnum.Freq_semiannual;

    //    Whether (for non short or long period periods) coupon payments 
    //    are equal (false) or exact (true).
        bool exactCoupon = false;

    //    Coupon DayCounter to use for the bond coupon payments.
        CTIEnums.DayCountEnum cpnDayCount = CTIEnums.DayCountEnum.DayCount_30360;

    //    Accrued interest DayCounter to use for the bond.
        CTIEnums.DayCountEnum accDayCount = CTIEnums.DayCountEnum.DayCount_30360;

    //    Business Day Convention to use.
        CTIEnums.BDCEnum BusDayConv = CTIEnums.BDCEnum.BDC_following;

    //    Redemption value of the bond (ie 100.0) .
        double redemption = 100;

    //    The Bond's yield value.
        double BondYield = 0.055;

    //    Yield calculation convention for all coupon periods except when 
    //    there is only one coupon period left to bond maturity (Bond's 
    //    settlement date is greater or equal to the 'penultCouponDate' 
    //    date).
        string YieldMethod = "ISMA:30360";

    //    Final Coupon Yield calculation convention for the last coupon 
    //    paying period when there is only one coupon period left to bond 
    //    maturity (Bond's settlement date is greater or equal to the 
    //    'penultCouponDate' date).
        string FCYieldMethod = "Simple:Actual360";

                    
    //  Excel function call would be this - "CT.BOND.FixedCouponBond_y()"

    //    Creates a fixed coupon bond object.
        string rFixedCouponBond_y;
                                        
        rFixedCouponBond_y = CTQL.CTBonds_ySA.FixedCouponBond_y(
                MyFixedCouponBond_y,
                Reload,
                Ccy,
                SettleDate.serialNumber(),
                datedDate.serialNumber(),
                firstCouponDate,
                penultCouponDate,
                matDate.serialNumber(),
                exDivTenor,
                FixedCouponBond_y_coupons,
                couponFreq,
                exactCoupon,
                cpnDayCount,
                accDayCount,
                MyNewCalendar,
                BusDayConv,
                redemption,
                BondYield,
                YieldMethod,
                FCYieldMethod);


    return rFixedCouponBond_y;
}        



// ///////////////////////////////////////////////////////////////////

private string EuropeanExercisePart()
{



    //    Key value to use as a handle for the created object
        string MyEuropeanExercise = "MyEuropeanExercise" + "_" + System.Convert.ToString(nCTBondOptionsGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

    //    Exercise date.
        CTQL.Date dtDate =  new CTQL.Date("21/10/2005", "dd/mm/yyyy");

                    
    //  Excel function call would be this - "CT.EX.EuropeanExercise()"

    //    Creates an European Exercise object.
        string rEuropeanExercise;
                                        
        rEuropeanExercise = CTQL.CTExerciseSA.EuropeanExercise(
                MyEuropeanExercise,
                Reload,
                dtDate.serialNumber());


    return rEuropeanExercise;
}        



// ///////////////////////////////////////////////////////////////////

private string GenericCalendarPart()
{

        //  Create example range for parameter GenericCalendar_AddHols
        CTQL.CTRangeData GenericCalendar_AddHols;    
        

        int[] arrBGenericCalendar_AddHols = { 
            CTQL.Date.serialNumber("1/2/2006", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("2/2/2006", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("3/2/2006", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("4/2/2006", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("5/2/2006", "dd/mm/yyyy"), 
            CTQL.Date.serialNumber("6/2/2006", "dd/mm/yyyy")  //  Array Data
        
        };
        
        CTQL.IntVector arrGenericCalendar_AddHols = 
            new  CTQL.IntVector(arrBGenericCalendar_AddHols);
    
        // Second parameter determines whether the array is a column array (false) or a row array (true)
        GenericCalendar_AddHols = new  CTQL.CTRangeData(arrGenericCalendar_AddHols, false);
            


    //    Key value to use as a handle for the created object
        string MyNewCalendar = "MyNewCalendar" + "_" + System.Convert.ToString(nCTBondOptionsGlobal);

    //    When creating this object for the first time, set this parameter 
    //    to a positive value.
        int Reload = 1;

                    
    //  Excel function call would be this - "CT.CAL.GenericCalendar()"

    //    Creates a generic calendar object.
        string rGenericCalendar;
                                        
        rGenericCalendar = CTQL.CTCalendarsSA.GenericCalendar(
                MyNewCalendar,
                Reload,
                GenericCalendar_AddHols);


    return rGenericCalendar;
}        








Copyright (c) 2003-2007 CapeTools - All Rights Reserved.