+ Reply to Thread
Results 1 to 8 of 8

Creating a Sharpe Ratio function using VBA

  1. #1
    Registered User
    Join Date
    09-22-2008
    Location
    London
    Posts
    2

    Creating a Sharpe Ratio function using VBA

    Hi,

    I'm fairly new to VBA and am trying to create some statistical functions in excel using VBA, but am having difficulty writing the code for one: the Sharpe ratio.

    In Excel, I use the formula following: average(C:C)/stdev(C:C) * sqrt(12)
    where column C is the excess return of a fund over a risk-free rate (i.e. column A minus column B or C1=A1-B1, C2=A2-B2, etc).

    I can create a function that works off column C where I calculate the excess in Excel, but would prefer one that works off columns A and B alone with the excess calculated within the function. Unfortunately I've hit a bit of a block here.

    Any ideas on how to write this would be greatly appreciated?

    Many thanks!

  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
    Why use VBA? How about the array formula

    =AVERAGE(A1:A30 - B1:B30) / STDEV(A1:A30 - B1:B30) * SQRT(12)

    Array formulas MUST be confirmed with Ctrl+Shift+Enter, not Enter. You'll know you did it correctly if curly braces appear around the formula in the Formula Bar; you cannot type in the braces directly.

    I don't know where you get the SQRT(12) term from ...


    Please read the Forum Rules about thread titles before starting your next thread.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-22-2008
    Location
    London
    Posts
    2

    Follow-up on Sharpe ratio code

    Hi,

    Thanks for the response and apologies on the vague thread title.

    The reason that I want to create a function is so that users who do not know the Sharpe Ratio formula can simply type something along the lines of:

    =SharpeRatio(A:A,B:B)

    For info, SQRT(12) is to annualise the Sharpe Ratio, as the calculations will be based on monthly returns.

    Thanks.

  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
    This is only a couple of lines of code. I'll post something tomorrow if no one else does in the meanwhile.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Try this:
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    839

    Re: Creating a Sharpe Ratio function using VBA

    Hi,

    Can someone please tell me where in the code in Shg's post is the instruction to look at columns A and B when calculating the ratio?

    Thanks

  7. #7
    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: Creating a Sharpe Ratio function using VBA

    =SharpeRatio(A:A, B:B)

  8. #8
    Registered User
    Join Date
    07-30-2010
    Location
    South Pole
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: Creating a Sharpe Ratio function using VBA

    There's an Excel spreadsheet for the Sharpe Ratio here. It uses VBA.

+ 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. How does this code output the coefficients of a trendline?
    By gshock in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-13-2010, 05:54 PM
  2. Adding help comments when Creating a function
    By Neil07979 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-05-2008, 04:34 AM
  3. Help with creating function...
    By ibleedoil in forum Excel General
    Replies: 5
    Last Post: 06-05-2008, 11:27 AM
  4. Creating an IF function with VB
    By Tracer123 in forum Excel General
    Replies: 4
    Last Post: 05-23-2008, 09:59 PM
  5. Creating an advanced search/find function
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-03-2008, 03:46 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