I am trying alternative methods for calculating a minimum threshold value (0.05) using a Function, a Sort procedure, and arrays (CoursAction)
However, I get a message that an array is expected for the Var Function.
Does anyone have a guess what could the problem be?
See the attachment below.
Option Explicit
Option Base 1
Sub Performance()
Dim N As Long
Dim EnsembleActions As Range
Dim Nb_Actions As Integer
Dim Action As Range
Dim CoursAction As Range
Dim i As Integer
Dim SharpeRatio As Double
Dim TauxRf As Double
Dim RendsEcart As Double
Dim NomAction As String
Dim ValeursAction() As Variant
Dim NB As Integer
Dim alpha As Double
Dim VarA As Double
With Worksheets("Actions")
Nb_Actions = .Cells(1, Columns.Count).End(xlToLeft).Column
End With
With Worksheets("Actions")
Set EnsembleActions = .Range(.Cells(2, 1), .Cells(.Rows.Count, Nb_Actions).End(xlUp))
End With
For Each Action In EnsembleActions.Columns
i = i + 1
Set CoursAction = Action
TauxRf = Worksheets("Performance").Cells(2, 2).Value
RendsEcart = WorksheetFunction.StDev(CoursAction)
NB = WorksheetFunction.Count(CoursAction)
Dim alpha As Double
Dim Var As Double
alpha = Worksheets("Performance").Cells(3, 2).Value
VarA = VarB(CoursAction, alpha)
NomAction = Worksheets("Actions").Cells(1, i).Value
With Worksheets("Performance")
.Cells(4 + i, 1) = NomAction
.Cells(4 + i, 2) = VarA
End With
Next Action
End Sub
Function VarB(Rendements As Range, A As Double)
Dim N As Double
Dim i As Integer
Dim CoursAction As Double
CoursAction = 0
N = WorksheetFunction.Count(Rendements)
For i = 1 To N
CoursAction = CoursAction + Rendements(i)
Next i
'We sort the array
Call Tri1(CoursAction)
VarB = CoursAction(Int(N * A)) 'A ici correspond au Alpha
End Function
Sub Tri1(plaga As Variant)
Dim ligne_Deb As Long
Dim ligne_Fin As Long
ligne_Deb = LBound(plaga)
ligne_Fin = UBound(plaga)
Dim i As Long, J As Long
Dim tmp As Long
For i = ligne_Deb To ligne_Fin - 1
For J = ligne_Fin To i + 1 Step -1
If plaga(J, 1) < plaga(J - 1, 1) Then
tmp = plaga(J, 1)
plaga(J, 1) = plaga(J - 1, 1)
plaga(J - 1, 1) = tmp
End If
Next J
Next i
End Sub
Bookmarks