Advertisement
GarlandA

VBA Portofolio

May 25th, 2024 (edited)
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Function Portofolio(Data As Variant, StartDate As Date, EndDate As Date, priceType As StockPriceType) As Variant
  2.  
  3.   ' Initialisation
  4.    Dim Variances As Collection   ' Vecteur des Variances pondéré au carré
  5.    Dim Rendements As Collection  ' Vecteur des rendements pondéré
  6.    Dim Covariances As Collection ' Matrice Covariances Pondéré par leur poids
  7.  
  8.     Dim Portofolio As Collection  ' Structure de donnée en sortie, au lieu d'un collection, peut être un objet = dict
  9.    
  10.     Dim i As Integer
  11.     Dim j As Integer
  12.    
  13.   ' Calcul des rendements et variances de chaque action du portefeuille
  14.    Dim Stock As Collection
  15.    
  16.     For i = 1 To Data("Stocks").Item.Count
  17.         Set Stock = GetStockData(Data("Stocks").Item(i), StartDate, EndDate)
  18.         Rendements.Add CalcMean(Stock, priceType) * Data("Weight").Item(i)            ' Le rendement pondéré par son poids
  19.        Variances.Add CalcVolatilite(Stock, priceType) * (Data("Weight").Item(i) ^ 2) ' La variance pondéré par son poids
  20.    Next
  21.    
  22.   ' Calcul des differentes paires de Covariances
  23.    Dim Stock_i As Collection
  24.     Dim Stock_j As Collection
  25.     Dim Covariance As Collection
  26.    
  27.     For i = 1 To Data("Stocks").Item.Count - 1                                         ' On itère de 1 à N-1
  28.        Set Stock_i = GetStockData(Data("Stocks").Item(i), StartDate, EndDate)
  29.         For j = 2 To Data("Stocks").Item.Count                                         ' On itère de 2 à N
  30.            Set Stock_j = GetStockData(Data("Stocks").Item(i), StartDate, EndDate)
  31.             Covariance.Add CalcCovariance(Stock_i, Stock_j, priceType) * Data("Weight").Item(i) * Data("Weight").Item(j)
  32.         Next j
  33.         Covariances.Add Covariance
  34.         Set Covariance = New Collection                                                ' Reinitialisation Covariance
  35.    Next i
  36.    
  37.   ' Rendement et Variance du portefeuille
  38.    Dim Rendement As Collection
  39.     Dim Variance As Collection
  40.    
  41.     Dim R As Double
  42.     Dim V As Double
  43.     Dim CV As Double
  44.     Dim CVs As Double
  45.    
  46.     ' Somme des Rendements pondéres des actions du portefeuille
  47.    For i = 1 To Rendements.Item.Count
  48.      R = R + Rendements.Item(i)
  49.      V = V + Variances.Item(i)
  50.     Next i
  51.    
  52.     ' Calcul de la Variance du portefeuille
  53.    For i = 1 To Data("Stocks").Item.Count - 1
  54.         For j = 1 To Covariances(i).Item.Count
  55.             CVs = CVs + Covariances(i).Item(j)
  56.         Next j
  57.         CV = CV + CVs
  58.         CVs = 0
  59.     Next i
  60.    
  61.     Rendement.Add R
  62.     Variance.Add V + 2 * CV
  63.    
  64.     Portofolio.Add Rendement
  65.     Portofolio.Add Variance
  66.    
  67.  
  68. End Function
  69.  
  70. Sub ShowPortofolio()
  71.  Dim Data As Dictionary 'J'aimerais que data soit un dictionnaire
  72. Dim Portofolio As Dictionary 'Le dictionaire qui contient le rendement et variance de notre portefeuille
  73. Dim StartDate As Date
  74.  Dim EndDate As Date
  75.  Dim P As Variant
  76.  
  77.  Set Data("Stocks") = GetDictonnary 'GetDictonnary serait une fonction qui renvoie une liste de valeurs, ici on voudrait les symboles de nos actions du portefeuille
  78. Set Data("Weight") = GetDictonnary 'Ici on recupere les ponderations pour les actions de notre portefeuille
  79. Set StartDate = DateSerial(2007, 1, 1)
  80.  Set EndDate = DateSerial(2007, 12, 31)
  81.  
  82.  P = Portofolio(Data, StartDate, EndDate, StockPriceType.closePrice)
  83.  Set Portofolio("Rendement") = P.Item(1)
  84.  Set Portofolio("Variance") = P.Item(2)
  85.  
  86. ' Print the average return
  87.    Debug.Print "Portofolio's return from " & StartDate & " To " & EndDate & ": " & Portofolio("Rendement").Item
  88.     Debug.Print "Portofolio's variance from " & StartDate & " To " & EndDate & ": " & Portofolio("Variance").Item
  89.    
  90.     Debug.Print "Average Return for " & StockSymbol & ": " & meanReturn
  91. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement