jueves, 17 de octubre de 2013

HOW TO CREATE A PORTFOLIO OPTIMIZER - COMO CREAR UN OPTIMIZADOR DE CARTERAS


Existen diferentes formas de solucionar el problema de la optimización de carteras, algunas soluciones son elegantes e intuitivas; pero lentas de ejecución, otras son sencillamente brillantes y rápidas; como el Método Matricial (método del gradiente) que os presento a continuación.
There are different ways to solve the problem of portfolio optimization, some solutions are elegant and intuitive, but slower execution, others are simply brilliant and fast, like Matrix method (gradient method) that I present below.

Código fuente en Visual Basic para crear un optimizador de carteras en Excel.
Source code in Visual Basic for Excel to create a portfolio optimizer.

En color burdeos dentro de la subrutina GSM (Optimizador) aparecen la funciones internas que necesitamos desarrollar para poder hacer funcionar GSM(Optimizador).
The red inside the subroutine GSM (Optimizer) are internal functions that we need to develop to be able to run GSM (Optimizer).

Estas funciones las presentaré en la siguiente entrada del blog.
These functions present them in the following blog entry.

Los rangos de celdas dentro de la hoja Excel pueden ser personalizados por cada usuario, adecuando posteriormente estos rangos en la subrutina GSM (Optimizador).
The ranges of cells within the Excel sheet can be customized for each user, then adapting these ranges in the subroutine GSM (Optimizer).



METODO MATRICIAL CON LAMBDA AUTOMATICO Y PERSONALIZADO
MATRIX METHOD WITH AUTOMATIC AND CUSTOM LAMBDA.

Sub GSM()
Sheets("Optimizador").Select
Rows("28:50").Select
Selection.ClearContents
Range("a12").Select
Sheets("Optimizador").Cells(28, 2) = "Carteras"
Sheets("Optimizador").Cells(29, 2) = "Nivel Lambda"
Sheets("Optimizador").Cells(30, 2) = "Dt"
Sheets("Optimizador").Cells(31, 2) = "R(e)"


'DEFINICION DE VARIABLES INICIALES CAPTURA DATOS
'VARIABLE DEFINITION OF INITIAL DATA CAPTURE
n = Sheets("INPUTS").Cells(2, 1)
ncarteras = Sheets("Optimizador").Cells(8, 3)
precision = Sheets("Optimizador").Cells(7, 3)
ReDim activo(n)
ReDim DESVIA(n, 1)
ReDim rentab(n, 1)
ReDim cor(n, n)
For X = 1 To n
activo(X) = Sheets("INPUTS").Cells(X + 1, 2)
Sheets("Optimizador").Cells(X + 27 + 4, 2) = activo(X)
DESVIA(X, 1) = Sheets("INPUTS").Cells(X + 1, 3) / 100
rentab(X, 1) = Sheets("INPUTS").Cells(X + 1, 4) / 100
Next X
For X = 1 To n
For i = 1 To n
cor(i, X) = Sheets("INPUTS").Cells(X + 1, i + 5)
Next i
Next X