1
$\begingroup$

I tried this on stack exchange, but think it is a better question here. I am beginning user and I need help with an error / bad output I am getting when calculating returns (using ROC) on stock closing prices. I created a trading strategy using 1,0,-1 as signals, but can't return an accurate percentage for my returns. I know the code is incorrect, but unsure how to correct it. Any help is much appreciated!

sum(ROC(Cl(x)) * x$signal, na.rm=TRUE) # Return of fully invested account

This returns: -0.004259766 Where, based on my strategy, it actually should be: 0.101174

Calculating this using: (88.16-80.06) / 80.06

 Date        Closing Price Signal 
 2015-03-06:     80.06        1 # enter trade
 2015-05-12:     88.16       -1 # exit trade

Hopefully that helps clarify my question. If not, please leave comments on how to improve it further.

Find sample code below:

getSymbols("CAT", src="google", from="2014-07-30", to="2015-07-29")

#######################################
######### BOLLINGER BANDS #############
#######################################

x <- na.omit(merge(CAT, BBands(Cl(CAT))))
x$sig <- NA

x$sig[c(FALSE, diff(sign(Cl(x) - x$mavg), na.pad=FALSE) != 0)] <- 0
x$sig[Cl(x) > x$up] <- -1 # short when Close is above up
x$sig[Cl(x) < x$dn] <- 1 # long when Close is below dn
x$sig[1] <- 0 # flat on the first day
    x$sig[nrow(x)] <- 0 # flat on the last day

# Fill in the signal for other times
x$sig <- na.locf(x$sig) 
x$sig <- Lag(x$sig)
x$sig[1] <- 0 

#######################################
############ STOCHASTICS ##############
#######################################

y <- na.omit(merge(CAT, stoch(Cl(CAT))))
y$diff <- y$fastK - y$slowD
    y$sig <- ifelse(y$diff >= -.1 & y$diff <= .1,1,0)
y$sig <- Lag(y$sig)
y$sig[1] <- 0 

#######################################
############### MACD ##################
#######################################

z <- na.omit(merge(CAT, MACD(Cl(CAT))))
z$diff <- z$macd - z$signal
    z$sig <- ifelse(z$diff >= -0.1 & z$diff <= 0.1,1,0)
z$sig <- Lag(z$sig)
z$sig[1] <- 0 

#######################################
################ MERGE ################
#######################################

all <- merge(x$sig,y$sig,z$sig)
    all[is.na(all)] <- 0
    all <- cbind(all,rowSums(all))
    all$..2 <- all$sig*all$sig.1*all$sig.2;

x$signal <- cbind(all$..2)
x$signal <- na.locf(x$signal)
x$signal <- Lag(x$signal)
x$signal[1] <- 1

#######################################
######## $$$ RETURNS $$$ ##############
#######################################

sum(abs(diff(x$signal, na.pad=FALSE))) # number of trades
    sum(diff(Cl(x)) * x$signal, na.rm=TRUE) # PnL of 1 share
cumsum(diff(Cl(x), na.pad=FALSE) * x$signal[-1]) # equity over time

sum(ROC(Cl(x)) * x$signal, na.rm=TRUE) # Return of fully invested account
    cumsum(ROC(Cl(x), na.pad=FALSE) * x$sig[-1]) # cumulative return

myReturn <- lag(x$signal) * dailyReturn(x)
charts.PerformanceSummary(cbind(dailyReturn(x),myReturn))
$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.