0
所以我有一個交易策略,平均有一個大於20%的最大回撤。我想找到一種方法來優化maxDrawdown()
。不過考慮一下,我發現了很好的文章,通過DEoptim()
來展示如何通過這個文章,但只展示了投資組合優化的例子。我不知道這是否可行?優化R中的maxDrawdown()
#Choose the Adjusted Close of a Symbol
stock <- Ad(TNA)
# I want to create a table with all possible combinations from the ranges below
i = c(3:45)
k = c(3:45)
j = c(3:45)
# stores possible combinations into z
z <- expand.grid(i,k,j)
z <- z[z[,1]<z[,2], ]
colnames(z)<- c("one","two","three")
row.names(z)<- c(paste(z[,1],z[,2],z[,3],sep=","))
# Function that will be used
getStratRet <- function(nFast, nSlow, nSig, stock, stockret) {
x <- MACD((stock), nFast=nFast, nSlow=nSlow, nSig=nSig, maType="EMA")
x <- na.omit(x)
sig <- Lag(ifelse((x$macd <= x$signal),-1, 0)) + Lag(ifelse((x$macd >= x$signal),1, 0))
return(na.omit(stockret * sig))
}
# time elapsed for 3:45 combinations: 479.886 seconds,
system.time(
Returns <- do.call(merge, mapply(FUN = getStratRet, nFast = z[,1], nSlow = z[,2], nSig
= z[,3], MoreArgs = list(stock = stock, stockret = stockret), SIMPLIFY = TRUE))
)
#My strategy returns a matrix of returns
View(Returns[1:10,1:5])
row.names 3,4,3 3,5,3 4,5,3 3,6,3 4,6,3
2011-01-11 -0.0035308990 NA NA NA NA
2011-01-12 0.0090226176 0.0090226176 0.0090226176 NA NA
2011-01-13 -0.0016647249 -0.0016647249 -0.0016647249 -0.0016647249 -0.0016647249
2011-01-14 0.0072214466 0.0072214466 0.0072214466 0.0072214466 0.0072214466
2011-01-18 0.0017353225 0.0017353225 0.0017353225 0.0017353225 0.0017353225
2011-01-19 -0.0098735504 -0.0098735504 -0.0098735504 -0.0098735504 -0.0098735504
2011-01-20 0.0013350023 0.0013350023 0.0013350023 0.0013350023 0.0013350023
2011-01-21 -0.0022517836 -0.0022517836 -0.0022517836 -0.0022517836 -0.0022517836
2011-01-24 -0.0056487939 -0.0056487939 -0.0056487939 -0.0056487939 -0.0056487939
2011-01-25 0.0005796862 0.0005796862 0.0005796862 0.0005796862 0.0005796862
我已經創建了一個功能通過DEoptim()
調用,以優化此MAXDD
函數計算maxDrawdown
(從PerformanceAnalytics
)爲每個返回。
# MAX DrawDown
MAXDD <- function(ret) {
ret <- na.omit(ret)
maxdd<- maxDrawdown(ret)
return (maxdd)
}
# MAX DRAWDOWN
system.time(
MaxDraw <- sapply(Returns, FUN = MAXDD)
)
然後我用DEoptim()
的,我希望儘量減少功能:MAXDD
library(DEoptim)
lower <- c(-0.10, 0) # I think this sets the max drawdown that will be acceptable
upper<- -lower # I think this will set what the maximum return acceptable
res <- DEoptim(MAXDD,lower,upper, control=list(NP=2000, itermax=10))
# I set the iteration to 5, to print them here:
Iteration: 1 bestvalit: -0.000000 bestmemit: 0.051143 0.000000
Iteration: 2 bestvalit: -0.000000 bestmemit: 0.034017 0.000000
Iteration: 3 bestvalit: -0.000000 bestmemit: 0.020190 0.000000
Iteration: 4 bestvalit: -0.000000 bestmemit: 0.028910 0.000000
Iteration: 5 bestvalit: -0.000000 bestmemit: 0.038250 0.000000
這是我堅持,因爲我不知道我是否做正確嗎?我不知道如何翻譯這個以用於我的目標。
非常感謝,現在我對如何使用'DEoptim()'有了更好的理解。 – Jason