+ Reply to Thread
Results 1 to 4 of 4

Correlation matrix automatically adjust the time range

  1. #1
    Registered User
    Join Date
    07-22-2014
    Location
    UK
    MS-Off Ver
    10 and 13
    Posts
    2

    Correlation matrix automatically adjust the time range

    Based on a historical stock prices table I need to calculate the correlation matrix for a define date range:
    Sheets("Raw data"):
    A B C D
    Date stock1 stock2 stock 3
    02/01/2012 0.934752772 0.991533951 0.698445112
    03/01/2012 0.519385963 0.878316925 0.924111967
    04/01/2012 0.285286565 0.35351676 0.79461415
    05/01/2012 0.632288941 0.88841507 0.329613832

    Sheets("Raw data").Select

    Dim rng_input As Range
    Set rng_input = Range([B1], [B1].End(xlDown).End(xlToRight))

    Sheets("CorrMatrix").Select
    Application.Run "ATPVBAEN.XLAM!Mcorrel", rng_input, _
    ActiveSheet.Range("$A$10"), "C", True

    the above code worked well but not sure how to properly adjust the range to be between 2 dates.
    For example how do you generate correlation matrix based on the red data between 03/01/12 - 04/01/12?
    i tried to find out the start and end date row numbers as well as the end column:

    r1 = Application.Match(Sheets("CorrMatrix").Range("A2"), Sheets("Raw data").Range("A:A"), 0)
    r2 = Application.Match(Sheets("CorrMatrix").Range("A3"), Sheets("Raw data").Range("A:A"), 0)

    Dim rLastCell As Range
    Set rLastCell = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
    LastCol = rLastCell.Column

    Sheets("Raw data").Select
    Set rng_input =ActiveSheet.Range(Cells(r1, 2), Cells(r2, LastCol))

    then Application.Run "ATPVBAEN.XLAM!Mcorrel", rng_input, _
    ActiveSheet.Range("$A$10"), "C", True
    but not working...
    Can anyone please kindly help out? thank you!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Correlation matrix automatically adjust the time range

    This is pretty simple to do with just formulas:

    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    1
    Date
    stock1
    stock2
    stock3
    2
    02/01/2012
    0.9348
    0.9915
    0.6984
    1.000
    0.862
    -0.294
    F2: =CORREL(B$2:B$5, INDEX($B$2:$D$5, 0, ROWS(F$2:F2)))
    3
    03/01/2012
    0.5194
    0.8783
    0.9241
    0.862
    1.000
    -0.267
    4
    04/01/2012
    0.2853
    0.3535
    0.7946
    -0.294
    -0.267
    1.000
    5
    05/01/2012
    0.6323
    0.8884
    0.3296
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-22-2014
    Location
    UK
    MS-Off Ver
    10 and 13
    Posts
    2

    Re: Correlation matrix automatically adjust the time range

    the goal is to use vba to generate the matrix and be able to auto-adjust the date range..

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Correlation matrix automatically adjust the time range

    It's easy enough to do with dynamic ranges to automatically select the entire (or some portion of the) date range, but if you're intent on using VBA and ATPVBAEN, I'll leave you to it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Correlation Matrix
    By jbergin200 in forum Excel General
    Replies: 1
    Last Post: 09-29-2011, 07:02 AM
  2. need help with correlation matrix .......
    By tijuemson in forum Excel General
    Replies: 1
    Last Post: 05-01-2011, 07:24 AM
  3. Correlation Matrix:XOM,
    By sajeel in forum Excel General
    Replies: 1
    Last Post: 06-10-2009, 02:02 PM
  4. Matrix correlation
    By excelboyz in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-03-2007, 06:21 AM
  5. correlation matrix
    By refreshplease3 in forum Excel General
    Replies: 2
    Last Post: 11-12-2006, 02:03 AM

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