基金产品评价Excel-VBA(自动化)

VBA 自用源代码

Sub 收益率()

Dim i As Integer
Dim n As Integer

n = Application.WorksheetFunction.Count(Range("B:B")) + 3

For i = 4 To n
    Cells(i, "D") = (Cells(i, "B") - Cells(4, "B")) / Cells(4, "B")

Next

End Sub

Sub 年化收益率()

Dim n As Integer
n = Application.WorksheetFunction.Count(Range("B:B")) + 3

Cells(3, "P") = (Cells(n, "D") * (n / 250))
Cells(3, "Q") = (Cells(n, "H") * (n / 250))
Cells(3, "R") = (Cells(n, "L") * (n / 250))


End Sub

Sub 年化波动率()

Dim n As Integer
n = Application.WorksheetFunction.Count(Range("B:B")) + 3

Cells(4, "P") = Application.WorksheetFunction.StDev_S(Range(Cells(4, "B"), Cells(n, "B"))) * ((360 / n) ^ 0.5)
Cells(4, "Q") = Application.WorksheetFunction.StDev_S(Range(Cells(4, "G"), Cells(n, "G"))) * ((360 / n) ^ 0.5)
Cells(4, "R") = Application.WorksheetFunction.StDev_S(Range(Cells(4, "K"), Cells(n, "K"))) * ((360 / n) ^ 0.5)


End Sub


Sub 周最大撤回()

Dim i As Integer
Dim n As Integer
n = Application.WorksheetFunction.Count(Range("B:B")) + 3
    
For i = 4 To n
Cells(i, "E") = (Cells(i, "B") - Application.WorksheetFunction.Max(Range(Cells(4, "B"), Cells(i, "B")))) / Application.WorksheetFunction.Max(Range(Cells(4, "B"), Cells(i, "B")))
Cells(i, "I") = (Cells(i, "F") - Application.WorksheetFunction.Max(Range(Cells(4, "F"), Cells(i, "F")))) / Application.WorksheetFunction.Max(Range(Cells(4, "F"), Cells(i, "F")))
Cells(i, "M") = (Cells(i, "J") - Application.WorksheetFunction.Max(Range(Cells(4, "J"), Cells(i, "J")))) / Application.WorksheetFunction.Max(Range(Cells(4, "J"), Cells(i, "J")))

Next

End Sub

Sub 下行标准差()

'下行数据数量大于等于2 不然会报错'
Cells(5, "P") = "=STDEV.S(IF(C4:C303 <0, C4:C303),0)"
Cells(5, "Q") = "=STDEV.S(IF(H4:H303 <0, H4:H303),0)"
Cells(5, "R") = "=STDEV.S(IF(L4:L303 <0, L4:L303),0)"

End Sub



Sub 中证500收益率()

Dim i As Integer
Dim n As Integer

n = Application.WorksheetFunction.Count(Range("B:B")) + 3

For i = 4 To n
    Cells(i, "H") = (Cells(i, "F") - Cells(4, "F")) / Cells(4, "F")
    Cells(i, "L") = (Cells(i, "J") - Cells(4, "J")) / Cells(4, "J")

Next

End Sub

Sub 市场相关性()

Cells(6, "P") = "=CORREL(D:D,H:H)"
Cells(7, "P") = "=CORREL(D:D,L:L)"

End Sub

Sub 夏普比率()

exp_r = Application.WorksheetFunction.Average(Range("d:d"))
std_r = Application.WorksheetFunction.StDev_S(Range("d:d"))

exp_r_hs = Application.WorksheetFunction.Average(Range("h:h"))
std_r_hs = Application.WorksheetFunction.StDev_S(Range("h:h"))

exp_r_zz = Application.WorksheetFunction.Average(Range("l:l"))
std_r_zz = Application.WorksheetFunction.StDev_S(Range("l:l"))

Cells(8, "P") = (exp_r - Cells(13, "P")) / std_r
Cells(8, "Q") = (exp_r_hs - Cells(13, "P")) / std_r_hs
Cells(8, "R") = (exp_r_zz - Cells(13, "P")) / std_r_zz

End Sub
 

Sub 最大撤回()

Cells(9, "p") = Application.WorksheetFunction.Min(Range("E:E"))
Cells(9, "q") = Application.WorksheetFunction.Min(Range("i:i"))
Cells(9, "r") = Application.WorksheetFunction.Min(Range("i:i"))

End Sub




Sub 标准化()


Dim i As Integer
Dim n As Integer

n = Application.WorksheetFunction.Count(Range("B:B")) + 3

For i = 4 To n
    Cells(i, "C") = (Cells(i, "B") - Application.WorksheetFunction.Average(Range(Cells(4, "B"), Cells(n, "B")))) / Application.WorksheetFunction.StDev_S(Range(Cells(4, "B"), Cells(n, "B")))
    Cells(i, "G") = (Cells(i, "F") - Application.WorksheetFunction.Average(Range(Cells(4, "F"), Cells(n, "F")))) / Application.WorksheetFunction.StDev_S(Range(Cells(4, "F"), Cells(n, "F")))
    Cells(i, "K") = (Cells(i, "J") - Application.WorksheetFunction.Average(Range(Cells(4, "J"), Cells(n, "J")))) / Application.WorksheetFunction.StDev_S(Range(Cells(4, "J"), Cells(n, "J")))

Next

End Sub



Sub 平均盈亏比()

Cells(10, "P") = "=SUM(IF(D4:D303>0,D4:D303))/SUM(IF(D4:D303<0,D4:D303))"
Cells(10, "Q") = "=SUM(IF(h4:h303>0,h4:h303))/SUM(IF(h4:h303<0,h4:h303))"
Cells(10, "R") = "=SUM(IF(l4:l303>0,l4:l303))/SUM(IF(l4:l303<0,l4:l303))"


End Sub





Sub 收益率copy()

Dim i As Integer
Dim n As Integer

n = Application.WorksheetFunction.Count(Range("B:B")) + 3

For i = 4 To n
    Cells(i, "U") = Cells(i, "D")
    Cells(i, "V") = Cells(i, "H")
    Cells(i, "W") = Cells(i, "L")
    
Next

End Sub

Sub 运行()
    Call 标准化
    Call 收益率
    Call 中证500收益率
    Call 周最大撤回
    Call 年化收益率
    Call 年化波动率
    Call 下行标准差
    Call 市场相关性
    Call 夏普比率
    Call 最大撤回
    Call 平均盈亏比
    Call 收益率copy

End Sub
发布了7 篇原创文章 · 获赞 0 · 访问量 223

猜你喜欢

转载自blog.csdn.net/weixin_45750663/article/details/105066558