-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_asset_management.jl
113 lines (90 loc) · 3.57 KB
/
dynamic_asset_management.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# TODO
# - run backtests on large data
# - allow universe subsets
# - inspect moment estimators directly
# - implement weight filters
# - save and load portfolios
# - check optimization status to catch errors
# - identify failed strategies and inspect them
## set up parallel computation
addprocs(3)
## load code
@everywhere begin
using DynAssMgmt
end
## setup
ENV["PLOTS_USE_ATOM_PLOTPANE"] = "false"
using Plots
using DynAssMgmt
using EconDatasets
using TimeSeries
using NamedArrays
using JLD
# load data
xxRets = dataset("IndustryPfs")
# store with information regarding the return type
retType = ReturnType(true, false, Dates.Day(1), false)
rets = Returns(xxRets, retType)
## define efficient frontier / diversfication frontier strategies
sigTargets = [linspace(.7, 1.4, 15)...]
diversTarget = 0.9
divFrontStrat = DivFront(diversTarget, sigTargets)
## estimate historic moments
ewmaEstimator = EWMA(0.99, 0.95)
# startInd = 500
startInd = findfirst(rets.data.timestamp .>= Date(1950,1,1))
@time univList = DynAssMgmt.applyOverTime(ewmaEstimator, rets, startInd)
# get subsets
univHistoryShort = UnivEvol(univList.universes[1:50], univList.dates[1:50],
univList.assetLabels)
btUniverses = univList
## backtest strategies
divFrontInvs = apply(divFrontStrat, btUniverses)
equWgtsInvs = apply(DynAssMgmt.EqualWgts(), btUniverses)
## evaluate performance
perfs = evalPerf(divFrontInvs, rets)
equWgtsPerfs = evalPerf(equWgtsInvs, rets)
## impute NaNs
oldPerfs = perfs
xx = DynAssMgmt.locf(perfs.data.values)
xxTA = TimeArray(perfs.data.timestamp, xx, perfs.data.colnames)
perfs = Performances(xxTA, perfs.retType)
# show performances
# Plots.gui()
# Plots.plot(perfs, leg=:topleft)
# Plots.plot!(equWgtsPerfs.data.timestamp, equWgtsPerfs.data.values*100, lab="Equal weights", line = (2, :red))
## get drawdowns
ddowns = evalDDowns(perfs)
Plots.plot(ddowns, leg=:bottomright)
Plots.gui()
## compute performance measures
perfStats = evalPerfStats(perfs)
perfSummary = DynAssMgmt.getPerfStatSummary(perfs)
## compute performance measures for underlying assets
xxInds = rets.data.timestamp .>= perfs.data.timestamp[1]
btAssRets = Returns(rets.data[xxInds], rets.retType)
assPerfs = convert(Performances, btAssRets)
assPerfStats = DynAssMgmt.getPerfStatSummary(assPerfs)
## compute performance measures for equal weights
equWgtsSummary = DynAssMgmt.getPerfStatSummary(equWgtsPerfs)
##
Plots.plot(perfSummary[:, "MaxDD"], perfSummary[:, "MuDailyToAnnualPerc"], seriestype = :scatter,
xlabel="Maximum drawdown", ylabel="Annual percentage return", labels="Strategies")
Plots.plot!(assPerfStats[:, "MaxDD"], assPerfStats[:, "MuDailyToAnnualPerc"], seriestype = :scatter,
labels="Assets")
Plots.plot!(equWgtsSummary[:, "MaxDD"], equWgtsSummary[:, "MuDailyToAnnualPerc"], seriestype = :scatter,
labels="Equal weights")
Plots.gui()
Plots.plot(perfSummary[:, "SigmaDailyToAnnualPerc"], perfSummary[:, "MuDailyToAnnualPerc"], seriestype = :scatter,
xlabel="Annual percentage vola", ylabel="Annual percentage return", labels="Strategies")
Plots.plot!(assPerfStats[:, "SigmaDailyToAnnualPerc"], assPerfStats[:, "MuDailyToAnnualPerc"], seriestype = :scatter,
labels="Assets")
Plots.plot!(equWgtsSummary[:, "SigmaDailyToAnnualPerc"], equWgtsSummary[:, "MuDailyToAnnualPerc"], seriestype = :scatter,
labels="Equal weights")
Plots.gui()
## write portfolios to file
save("./tmp/divFrontInvestments.jld", "divFrontInvs", divFrontInvs)
##
divFrontInvsLoaded = load("./tmp/divFrontInvestments.jld", "divFrontInvs")
divFrontInvsLoaded = load("./tmp/divFrontInvestments.jld")
divFrontInvs = divFrontInvsLoaded["divFrontInvs"]