-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfolioBacktest组合回调检测.R
145 lines (93 loc) · 3.85 KB
/
portfolioBacktest组合回调检测.R
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
library(portfolioBacktest)
library(ceRtainty)
data("profitSWG")
#
# Computing the CE values, for a RAC range of 0.5-4.0, and Power utility function.
#
# Obtaining the CE table
certainty(data = profitSWG, ival = 0.5, fval = 4, utility = "Power")$CE_values
# Obtaining the RAC vector
certainty(data=profitSWG,ival=.5,fval=4,utility="Power")$RAC
# Performing the CE plot
certainty(data=profitSWG,ival=.5,fval=4,utility="Power")$CE_plot()
# Computing and storing the CE values using Power utility function
#
ces <- certainty(data = profitSWG, ival = 0.5, fval = 4, utility = "Power")
ces_values <- ces$CE_values # store CE table
ces_rac <- ces$RAC # store RAC vector
# Computing the RP values respect to SERENADE treatment
premium(tbase = "serenade",ce_data = ces_values, rac = ces_rac, utility = "Power")$PremiumRisk
# Computing the RP values in percentage respect to SERENADE treatment
premium(tbase = "serenade",ce_data = ces_values, rac = ces_rac, utility = "Power")$PremiumRiskPer100
# Plotting the RP absolute values
premium(tbase = "serenade",ce_data = ces_values, rac = ces_rac, utility = "Power")$RP_plot()
rac_generator(data = profitSWG$control, ini = 0.5, fin = 4.0)
library(portfolioBacktest)
data(dataset10) # load dataset
# define your own portfolio function
uniform_portfolio <- function(dataset) {
N <- ncol(dataset$adjusted)
return(rep(1/N, N))
}
# do backtest
bt <- portfolioBacktest(list("Uniform" = uniform_portfolio), dataset10)
# show the summary
bt_sum <- backtestSummary(bt)
names(bt_sum)
bt_sum$performance_summary
# define your own portfolio function
quintile_portfolio <- function(data) {
X <- diff(log(data$adjusted))[-1]
N <- ncol(X)
ranking <- sort(colMeans(X), decreasing = TRUE, index.return = TRUE)$ix
w <- rep(0, N)
w[ranking[1:round(N/5)]] <- 1/round(N/5)
return(w)
}
# do backtest
bt <- portfolioBacktest(list("Quintile" = quintile_portfolio), dataset10,
benchmark = c("uniform", "index"))
# see all performance measures available for the ranking
backtestSummary(bt)$performance
# show leaderboard
leaderboard <- backtestLeaderboard(bt, weights = list("Sharpe ratio" = 6,
"max drawdown" = 1,
"ROT bps" = 1,
"cpu time" = 1,
"failure rate" = 1))
leaderboard$leaderboard_scores
# do backtest
bt <- portfolioBacktest(list("Quintile" = quintile_portfolio), dataset10,
benchmark = c("uniform", "index"))
# now we can obtain the table
bt_summary_median <- backtestSummary(bt)
summaryBarPlot(bt_summary_median, measures = c("max drawdown", "annual volatility"))
summaryBarPlot(bt_summary_median, measures = c("max drawdown", "annual volatility"),
type = "simple")
# do backtest
bt <- portfolioBacktest(list("Uniform" = uniform_portfolio), dataset10)
# show the backtest results in table
bt_tab <- backtestTable(bt)
bt_tab[c("Sharpe ratio", "max drawdown")]
library(portfolioBacktest)
data(SP500_symbols)
head(SP500_symbols)
# download data from internet
SP500_data <- stockDataDownload(stock_symbols = SP500_symbols,
from = "2008-12-01", to = "2009-12-01")
data(dataset10)
# define your own portfolio function
quintile_portfolio <- function(data) {
X <- diff(log(data$adjusted))[-1]
N <- ncol(X)
ranking <- sort(colMeans(X), decreasing = TRUE, index.return = TRUE)$ix
w <- rep(0, N)
w[ranking[1:round(N/5)]] <- 1/round(N/5)
return(w)
}
# do backtest
bt <- portfolioBacktest(list("Quintile" = quintile_portfolio), dataset10,
benchmark = c("uniform", "index"))
# Now we can plot
backtestBoxPlot(bt, "Sharpe ratio")
backtestBoxPlot(bt, "Sharpe ratio", type = "simple")