The Markowitz method, also known as the Modern Portfolio Theory, is a way to find the optimal weights for stocks in a portfolio. This method aims to balance the risk and returns of a portfolio to achieve the highest possible return for a given level of risk.
In this answer, we'll implement the Markowitz method in VBA (Visual Basic for Applications) to find the best combination of 3 stocks in a portfolio without using the Solver function.
Assumptions:
- The stock's historical daily returns are provided in a worksheet.
- The stocks are listed in columns B, C, and D, and their daily returns are listed in rows 3 to 1002.
Here is the VBA code:
Option Explicit
Function CalculatePortfolioRisk(Weight1 As Double, Weight2 As Double, Weight3 As Double, CovMatrix As Variant) As Double
CalculatePortfolioRisk = Weight1 * Weight1 * CovMatrix(1, 1) + Weight2 * Weight2 * CovMatrix(2, 2) + Weight3 * Weight3 * CovMatrix(3, 3) + 2 * Weight1 * Weight2 * CovMatrix(1, 2) + 2 * Weight1 * Weight3 * CovMatrix(1, 3) + 2 * Weight2 * Weight3 * CovMatrix(2, 3)
End Function
Sub CalculateBestWeightsMarkowitz()
Dim ReturnsRange As Range
Set ReturnsRange = Worksheets("Sheet1").Range("B3:D1002")
Dim StockReturns As Variant
StockReturns = ReturnsRange.Value
Dim CovMatrix(1 To 3, 1 To 3) As Double
Dim i As Long, j As Long, k As Long
Dim n As Long: n = UBound(StockReturns, 1)
' Calculate the covariance matrix
For i = 1 To 3
For j = 1 To 3
CovMatrix(i, j) = WorksheetFunction.Covar(Application.Index(StockReturns, , i), Application.Index(StockReturns, , j))
Next j
Next i
' Initialize portfolio weights and other variables
Dim Weight1 As Double, Weight2 As Double, Weight3 As Double
Weight1 = 1 / 3
Weight2 = 1 / 3
Weight3 = 1 / 3
Dim BestWeight1 As Double, BestWeight2 As Double, BestWeight3 As Double
Dim MinRisk As Double, CurrentRisk As Double
MinRisk = CalculatePortfolioRisk(Weight1, Weight2, Weight3, CovMatrix)
' Find the best portfolio weights (without short-selling allowed) using brute-force method
For i = 0 To 100
Weight1 = i / 100
For j = 0 To 100 - i
Weight2 = j / 100
Weight3 = 1 - Weight1 - Weight2
CurrentRisk = CalculatePortfolioRisk(Weight1, Weight2, Weight3, CovMatrix)
If CurrentRisk < MinRisk Then
MinRisk = CurrentRisk
BestWeight1 = Weight1
BestWeight2 = Weight2
BestWeight3 = Weight3
End If
Next j
Next i
' Print results to the worksheet in range E3:G3
Worksheets("Sheet1").Range("E3:G3").Value = Array(BestWeight1, BestWeight2, BestWeight3)
End Sub
You can then run the 'CalculateBestWeightsMarkowitz' subroutine to display the optimal weights of the stocks in your portfolio on your worksheet in range E3:G3. This implementation uses a brute-force method to calculate the best weights without using Excel's Solver function. Note that this example assumes no short-selling, and the weights are restricted to be non-negative and sum up to 1.
Remember to replace the sheet name and range references based on your specific worksheet layout.