StepMonteCarlo Example VBA





http://www.QuantTools.com
CapeTools (Compact) Process Simulation function list
StepMonteCarlo function

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

Key TAGs | Excel Index | API Index



Example Excel VBA Driver function. Preparing the parameters and the final function call (the result).

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

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 :




VBA Example - StepMonteCarlo





    '     ##################################################################################
    '     The first function here StepMonteCarlo(), contains a series of
    '     function calls leading upto the main function call, the second function
    '     within this file ( StepMonteCarloPart() ).
    '     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. 
    '     ##################################################################################
        


' Add a reference to the CTQL_VBA_API.xla file via the Tools->Reference menu (within the VBA editor).
' This holds VBA class objects for communicating with the CTQuantToolsXL20.dll Excel Addin
' This XLA does not use COM and has access to all the financial objects created via the spreadsheet functions.


' 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.
Global nCTProcessSimCGlobal As Long
    
    
Public Function VB_EX_StepMonteCarlo() As String

    nCTProcessSimCGlobal = nCTProcessSimCGlobal + 1
            
    
On Error GoTo err_Generic
        
            
    

    '    Creates a centralized valuation date object.

    Dim MyValuationDate As String
    MyValuationDate = _
        ValueDateObjPart()


    

    '    Creates a Generalized BlackScholes Process.
    Dim MyGBSProcess As String
    MyGBSProcess = _
        GBSProcessPart( _
        MyValuationDate)


    

    '    Creates a Generalized BlackScholes Process.
    Dim My2ndGBSProcess As String
    My2ndGBSProcess = _
        GBSProcess__2Part( _
        MyValuationDate)


    

    '    Creates a Generalized BlackScholes Process.
    Dim My3rdGBSProcess As String
    My3rdGBSProcess = _
        GBSProcess__3Part( _
        MyValuationDate)


    

    '    Creates a Generalized BlackScholes Process.
    Dim My4thGBSProcess As String
    My4thGBSProcess = _
        GBSProcess__4Part( _
        MyValuationDate)


    

    '    Creates a Generalized BlackScholes Process.
    Dim My5thGBSProcess As String
    My5thGBSProcess = _
        GBSProcess__5Part( _
        MyValuationDate)


    

    '    Creates an array of correlated one dimensional stochastic processes.
    Dim MyCorrArrayProcesses As String
    MyCorrArrayProcesses = _
        CorrArrayProcessesPart( _
        MyGBSProcess, _                
        My2ndGBSProcess, _                
        My3rdGBSProcess, _                
        My4thGBSProcess, _                
        My5thGBSProcess)


    

    '    Creates a Step Monte Carlo object given a process object and 
    '    a time line dates array.
    Dim MyStepMonteCarlo As String
    MyStepMonteCarlo = _
        StepMonteCarloPart( _
        MyCorrArrayProcesses, _
        MyValuationDate)

    ' This is the result we are looking for.
    VB_EX_StepMonteCarlo = MyStepMonteCarlo

            
    
    Exit Function
err_Generic:
    MsgBox "Error: " & Err.Number & vbCrLf & Err.Description    
    
            
End Function                
        


' ///////////////////////////////////////////////////////////////////

Private Function StepMonteCarloPart( _
    MyCorrArrayProcesses As String, _                
    MyValuationDate As String) As String


    
        '  Create example range for parameter StepMonteCarlo_MandatoryDates

        ' Column vector of 12 rows (indexed from 0, highest index of 11)
        Dim StepMonteCarlo_MandatoryDates(11, 0) As Variant
                
        StepMonteCarlo_MandatoryDates(0, 0) = DateValue("19/Jul/2005")
        StepMonteCarlo_MandatoryDates(1, 0) = DateValue("19/Jan/2006")
        StepMonteCarlo_MandatoryDates(2, 0) = DateValue("19/Jul/2006")
        StepMonteCarlo_MandatoryDates(3, 0) = DateValue("19/Jan/2007")
        StepMonteCarlo_MandatoryDates(4, 0) = DateValue("19/Jul/2007")
        StepMonteCarlo_MandatoryDates(5, 0) = DateValue("19/Jan/2008")
        StepMonteCarlo_MandatoryDates(6, 0) = DateValue("19/Jul/2008")
        StepMonteCarlo_MandatoryDates(7, 0) = DateValue("19/Jan/2009")
        StepMonteCarlo_MandatoryDates(8, 0) = DateValue("19/Jul/2009")
        StepMonteCarlo_MandatoryDates(9, 0) = DateValue("19/Jan/2010")
        StepMonteCarlo_MandatoryDates(10, 0) = DateValue("19/Jul/2010")
        StepMonteCarlo_MandatoryDates(11, 0) = DateValue("19/Jan/2011")
        
            



    '    Key value to use as a handle for the created object
        Dim MyStepMonteCarlo As String
        MyStepMonteCarlo = "MyStepMonteCarlo" & "_" & CStr(nCTProcessSimCGlobal)


    '    When creating this object for the first time, set this parameter 
    '    to a positive value.
        Dim Reload As Long
        Reload = 1


    '    Used to calculate time in years.
        Dim dayCounter As String
        dayCounter = "30360"


    '    The minimum number of steps that the discretization of the 'MandatoryDates' 
    '    parameter will take.
        Dim MinNoOfSteps As Long
        MinNoOfSteps = 50


    '    The random generator type to use.
        Dim MCMethod As String
        MCMethod = "Pseudo"


    '    Seed value.
        Dim Seed As Long
        Seed = 0

                    
    '  Excel function call is : "CT.PRO.StepMonteCarlo()"

    '    Creates a Step Monte Carlo object given a process object and 
    '    a time line dates array.
        Dim rStepMonteCarlo As String 
        
        ' We call the CreateCTProcessSimC() function via the CTQL module exposed from the CTQL_VBA_API.xla addin.
        Dim oCTProcessSimC As CTProcessSimC
        Set oCTProcessSimC = CTQL.CreateCTProcessSimC()
    
        rStepMonteCarlo = oCTProcessSimC.StepMonteCarlo( _
                MyStepMonteCarlo, _
                Reload, _
                MyCorrArrayProcesses, _
                MyValuationDate, _
                dayCounter, _
                StepMonteCarlo_MandatoryDates, _
                MinNoOfSteps, _
                MCMethod, _
                Seed)


    StepMonteCarloPart = rStepMonteCarlo
    
End Function                        








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