优化R中的maxDrawdown()

时间:2014-04-19 01:16:15

标签: r optimization maximize trading

所以我的交易策略平均有超过20%的最大亏损。我想找到一种优化maxDrawdown()的方法。无论如何,我通过DEoptim()找到了很好的文章,但是只展示了投资组合优化的例子。我不知道这样可行吗?

#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

这是我被困的地方,因为我不知道我是否正确地这样做了?我不知道如何将其翻译成我的目标。

1 个答案:

答案 0 :(得分:2)

由于您的代码已经计算了所有策略的回报, 你可以计算所有这些的缩编并采取最好的一个。 这是一次详尽的搜索:您不需要DEoptim

z[ which.min( apply( Returns, 2, maxDrawdown ) ), ]

要使用DEoptim,您需要提供一个功能 其参数是您策略的参数, 并返回缩编。 lowerupper参数是下限和上限 关于你的战略参数。

f <- function(u) 
  maxDrawdown( getStratRet( 
    u[1], u[2], u[3], stock=stock, stockret=stockret 
  ) )
r <- DEoptim( f, c(3,3,3), c(45,45,45) )
r$optim$bestmem
#      par1      par2      par3 
#  3.486908 40.528064 13.813589