'VBA Stock info function with different calculations based on one input
I am trying to code a VBA function Stock_Info that will calculate the sharpe ratio if you input sharpe, the CAPM Beta if you input beta and the CAPM Alpha if you input alpha.
The risk free rate (RF) will only be given in a single cell, not as a range, unlike the Returns of the stock.
The STOCK_RET is given in monthly returns, same as the MKT_RET (Which will only be used for the CAPM calculations).
I need to find the excess return by substracting the risk-free rate (RF) from the STOCK_RET. How does one best do this when STOCK_RET is a range and RF is a value of 0.25 given in one single cell?
Furthermore how does one make it so the input under CHOICE will determine what calculation the function will do? My best guess is the IF CHOICE = sharpe Then but there may be a better way?
Here's the code I got so far.
Function Stock_Info(STOCK_RET As Range, RF As Single, CHOICE As Parameter, Optional MKT_RET As Range)
Dim AverageReturn As Double
Dim StandardDev As Double
Dim ExcessReturn() As Double
Dim Sharpe_ratio As Double
If CHOICE = sharpe Then
ExcessReturn(i) = STOCK_RET(i) - RF
AverageReturn = Application.WorksheetFunction.Average(ExcessReturn)
StandardDev = Application.WorksheetFunction.StDev_S(ExcessReturn)
Sharpe_ratio = AverageReturn / StandardDev
ElseIf CHOICE = beta Then
ElseIf CHOICE = alpha Then
End If
End Function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
