|
Hello,
I would like to monitor short-term-returns on simple strategies like donchian channel price outbreaks. That is, I would like to see the returns at 1:10 periods after the breakout-event occurs. For example: getSymbols("NOK", adjust=TRUE) # calculate donchian channel for NOK DC <- DonchianChannel(lag(cbind(Hi(NOK),Lo(NOK)))) # mark days when price closes below the low band of DC breakdown <- eval(Cl(NOK)<DC[,"low"]) colnames(breakdown) <- c("breakdown") # how often did that condition occur? colSums(breakdown, na.rm=TRUE) #breakdown # 135 Now I masked out all days when the price closed below the donchian channel. - Is there a easy way to display only the (135) rows where breakdown[,1] is TRUE? - How to calculate the 1:10 days- returns after the breakdown-events then? I would be grateful for some hints, Andreas _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. |
|
Hi Andreas,
Try something like below. Make sure it's right though, because I didn't really check my work. On Thu, Apr 12, 2012 at 11:36 AM, Andreas Voellenklee <[hidden email]> wrote: > - Is there a easy way to display only the (135) rows where > breakdown[,1] is TRUE? NOK[breakdown] > - How to calculate the 1:10 days- returns after the breakdown-events then? # 10 period return from the current period through 10 periods into the future lag(ROC(Cl(NOK), 10), -10)[breakdown] # 1:10 period return from the current period through 1:10 periods into the future do.call(cbind, lapply(1:10, function(i) { lag(ROC(Cl(NOK), i), -i)[breakdown] })) HTH, Garrett _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. |
|
Thank you Sean and Garrett. Garrett's example worked fine. If anybody
is interested, I incorporated this in a function and wrote some more to visualize the results. calculateNPeriodReturns <- function (TS, Filter, n) { returns <- do.call(cbind, lapply(1:n, function(i) { lag(ROC(Cl(TS), i), -i)[Filter] })) colnames(returns) <- 1:n return(returns) } returnsBoxplot <- function (returns) { par(mfrow = c(1, 1)) boxplot(as.matrix(returns), range = 0) } returnsHistogram <- function (returns) { columns <- dim(returns)[2]/2 par(mfrow = c(2, columns)) for (i in 1:dim(returns)[2]) { hist(returns[, i], main = paste("t+", i, sep = ""), xlab = "", breaks = 20, col = "blue") } } returnsLinePlot <- function (returns) { molten <- melt(t(as.data.frame(returns))) colnames(molten) <- c("period", "date", "return") ggplot(molten, aes(period, return)) + geom_line(aes(colour = date)) } library(quantmod) # try Nokia for this example getSymbols("NOK", adjust=TRUE) # calculate donchian channel for NOK DC <- DonchianChannel(lag(cbind(Hi(NOK),Lo(NOK)))) # mark days when price closes below the low band of DC breakdown <- eval(Cl(NOK)<DC[,"low"]) colnames(breakdown) <- c("breakdown") # calculate returns from 1:10 periods after breakdown events returns <- calculateNPeriodReturns(NOK, breakdown, 10) # show a boxplot of returns returnsBoxplot(returns) # show a histogram of returns returnsHistogram(returns) # show a line diagram library(ggplot2) library(reshape) returnsLinePlot(returns) _______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go. |
| Powered by Nabble | Edit this page |
