+ Reply to Thread
Results 1 to 7 of 7

Covariance-Variance Stock Prices - VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Covariance-Variance Stock Prices - VBA code

    Hello,

    I try to find an optimized portfolio strategy. This work need a covariance matrix between different stock prices in a period of time.

    For example , see the file attached, I need a covariance matrix ( I have prepared it on the sheet2) between the 37 different stock prices (sheet1) during a period of time.
    For example since the 25/08/2011 to the 27/10/2011, but what is pretty diffcult is that I need a general code where I cStockMarketPrices.xlsxould enter the dates I want (For example by an InputBox) and the matrix appear.

    Why I need that ? Because I will have to change the period many times.

    I find approximately how to do the matrix, typing directly the equation on excel but I can't find the VBA code to do it , and I need a VBA code?


    If someone could help, It would be very kind of him,

    I just let you see the file attached


    Thank you

  2. #2
    Registered User
    Join Date
    02-06-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Covariance-Variance Stock Prices - VBA code

    Does someone understand my question ?

  3. #3
    Registered User
    Join Date
    02-06-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Covariance-Variance Stock Prices - VBA code

    Please I very need help

  4. #4
    Registered User
    Join Date
    02-06-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Covariance-Variance Stock Prices - VBA code

    up !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! sorry but I need it very rapidly . Is there any good mathematician ?

  5. #5
    Registered User
    Join Date
    02-06-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Covariance-Variance Stock Prices - VBA code

    may be i Could help from this :

    Sub Test()
    Dim dArrData(1 To 100, 1 To 10) As Double
    Dim dAutoCoVar() As Double
    Dim j As Long, k As Long

    ' fill the array with some bogus data
    Randomize
    For j = 1 To 100
    For k = 1 To 10
    dArrData(j, k) = Rnd
    Next k
    Next j

    ' write to the worksheet, for debug
    Range("A1:J100").Value = dArrData

    ' calculate the autocovariance matrix
    dAutoCoVar = Autocovar(dArrData)

    ' write to the worksheet, for debug
    Range("L1:U10").Value = dAutoCoVar
    End Sub

    Function Autocovar(dArrData() As Double) As Double()
    Dim dArrResult() As Double
    Dim j As Long, k As Long

    ' redim the result array as a square array.
    ReDim dArrResult(1 To UBound(dArrData, 2), 1 To UBound(dArrData, 2))

    ' calculate the autocovariance matrix
    For j = 1 To UBound(dArrData, 2)
    For k = 1 To UBound(dArrData, 2)
    With Application.WorksheetFunction
    dArrResult(j, k) = .Covar(.Index(dArrData, 0, j), .Index(dArrData, 0, k))
    End With
    Next k
    Next j
    Autocovar = dArrResult
    End Function

  6. #6
    Forum Contributor JP Romano's Avatar
    Join Date
    10-09-2008
    Location
    Princeton, NJ
    MS-Off Ver
    2010
    Posts
    500

    Re: Covariance-Variance Stock Prices - VBA code

    Are you asking for something like a pop up form into which the user would enter two dates, then the macro populates the differences for each stock's price into your list?

  7. #7
    Registered User
    Join Date
    02-06-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Covariance-Variance Stock Prices - VBA code

    I am looking for a pop up form into which the user enter the date of the end of the période because the beginning of the period is always the same : 25/08/2011. After that the code would calculate a covariance matrix between the 37 stocks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1