Question

So i have a Trading Strategy that on average has a Max Drawdown that is greater than 20%. I would like to find a way to optimize maxDrawdown() . However looking into it, I found good articles showing how to this via DEoptim() but only showed examples for portfolio optimization. I do not know if it is feasible this way?

#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

I have created a function to optimize by DEoptim() called MAXDD this function calculates maxDrawdown (from PerformanceAnalytics) for each return.

# MAX DrawDown
MAXDD <- function(ret) {
ret <- na.omit(ret)
maxdd<- maxDrawdown(ret)
return (maxdd) 
}

# MAX DRAWDOWN 
system.time(
MaxDraw <- sapply(Returns, FUN = MAXDD)
)

Then I use DEoptim() for the function that I want to minimize: 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

This is where i am stuck as I do not know if I am doing this correctly? I do not know how to translate this to use in my objective.

Était-ce utile?

La solution

Since your code already computes the returns of all the strategies, you can compute the drawdown of all of them and take best one. That is an exhaustive search: you do not need DEoptim.

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

To use DEoptim, you need to provide a function whose arguments are the parameters of your strategy, and which returns the drawdown. The lower and upper arguments are lower and upper bounds on the parameters of your strategy.

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 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top