viernes, 8 de noviembre de 2013

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


En esta actualización de la entrada anterior; se incorporan las funciones auxiliares necesarias para completar el optimizador de carteras y para los que no queráis perder mucho tiempo con la fase de diseño, os incluyo mas abajo un link en el que podéis descargaros el optimizador para Excel ya completo y funcional.
In this update of the previous post, are incorporated auxiliary functions necessary to complete the portfolio optimizer and for those who do not wish to lose a lot of time with the design phase, I include below a link where you can download the Excel optimizer and fully functional.




FUNCIONES AUXILIARES QUE UTILIZA EL OPTIMIZADOR:
'PRODUCTO DE DOS MATRICES
'PRODUCT OF TWO MATRIX
Public Function MATRIZP(MATRA(), MATRB()) As Variant
ReDim MATRIZ(UBound(MATRA, 1), UBound(MATRB, 2))
For Z = 1 To UBound(MATRB, 2)
For X = 1 To UBound(MATRA, 1)
SUMA = 0
For Y = 1 To UBound(MATRA, 2)
SUMA1 = MATRA(X, Y) * MATRB(Y, Z)
SUMA = SUMA1 + SUMA
Next Y
MATRIZ(X, Z) = SUMA
Next X
Next Z
MATRIZP = MATRIZ
End Function

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