# COMP5531.R - Last Updated 7 April 2013#UNIT 1#find the...

This preview shows pages 1–4. Sign up to view the full content.

Last Updated: 7 April 2013 #UNIT 1 ############################################################################## #find the correlation between n and n-1 #if there is no correlation, support weak-form EMH #Example 1.1 install.packages("quantmod") library(quantmod) getSymbols("^SSEC") a=SSEC[,4] b=filter(a, c(0,1), sides=1) c=filter(b, c(0,1), sides=1) x=cbind(a, b) x=cbind(x, c) p=(x[,1]-x[,2])/x[,2] q=(x[,2]-x[,3])/x[,3] plot(p,q) x=p[-(1:2)] # why takes out the first two rows? y=q[-(1:2)] cor(x,y) #UNIT 2 (SUPP-R) ##################################################################### #TTR Demonstration #Example 2.1 #get data library(TTR) data(ttrc) # Directional Movement Index dmi.adx <- ADX(ttrc[,c("High","Low","Close")]) tail(dmi.adx) # Moving Averages ema <- EMA(ttrc[,"Close"], n=20) sma <- SMA(ttrc[,"Close"], n=20) # MACD macd <- MACD( ttrc[,"Close"] ) # RSI rsi <- RSI(ttrc[,"Close"]) # Stoichastics stoch.osc <- stoch(ttrc[,c("High","Low","Close")]) # OBV obv <- OBV(ttrc[,"Close"], ttrc[,"Volume"]) # Bollinger Bands bbands <- BBands( ttrc[,c("High","Low","Close")] ) #quantmod Demonstration #Example 2.2 # plot candlestick chart

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
library(quantmod) getSymbols("^SSEC") length(SSEC[,1]) x=SSEC[c(1385:1485)] chartSeries(x, theme="white", TA="addSMA(5)") myAddSMA() # add your own SMA line # add your customized line myAverage=function(x) ((x[,4]+x[,2]+x[,3]+x[,1])/4) addTA(myAverage(x),col='blue',on=1) # for more information see http://www.quantmod.com/examples/charting/ myAddSMA = function (n = 10, on = 1, with.col = Cl, overlay = TRUE, col = "brown") { stopifnot("package:TTR" %in% search() || require("TTR", quietly = TRUE)) lchob <- quantmod:::get.current.chob() chobTA <- new("chobTA") [email protected] <- !overlay if (on == 1) { x <- as.matrix([email protected]) if (!is.OHLC(x) | missing(with.col)) with.col <- 1 if (is.function(with.col)) { x.tmp <- do.call(with.col, list(x)) } else x.tmp <- x[, with.col] } ma.tmp <- NULL for (i in 1:length(n)) { ma <- SMA(x.tmp, n = n[i]) ma.tmp <- cbind(ma.tmp, ma) } [email protected] <- matrix(ma.tmp[[email protected], ], ncol = NCOL(ma.tmp)) [email protected] <- "chartSMA" [email protected] <- match.call() [email protected] <- on [email protected] <- list(xrange = [email protected], colors = [email protected], color.vol = [email protected], multi.col = [email protected], spacing = [email protected], width = [email protected], bp = [email protected], x.labels = [email protected], time.scale = [email protected], col = col, n = n) if (is.null(sys.call(-1))) { TA <- [email protected]\$TA [email protected]\$TA <- c(TA, chobTA) [email protected] <- [email protected] + ifelse([email protected], 1, 0) chartSeries.chob <- quantmod:::chartSeries.chob do.call("chartSeries.chob", list(lchob)) invisible(chobTA) } else { return(chobTA) } } #ZigZag #Example 2.3 #http://www.inside-r.org/packages/cran/TTR/docs/ZigZag library(quantmod)
library(TTR) s <- getSymbols('^HSI', auto.assign=FALSE) #s=s[time(s)>"2012-07-01"] #char_Series(s) chart_Series(s, subset="2012-07::") add_TA(ZigZag(s[,2:3],5),on=1, col="blue") #percent=T #Timing the market/stock #Example 2.4 T = 5. #time 0 -> T dt = 0.01 #s n = T/dt F = 1/dt # freq domain -F/2 -> F/2 df = 1/T t = seq(0,T,by=dt) freq = 5 #Hz y <- 10*sin(2*pi*freq*t) #FREQ ARRAY f <- 1:length(t)/T #FOURIER WORK Y <- fft(y) mag <- sqrt(Re(Y)^2+Im(Y)^2)*2/n #Amplitude phase <- Arg(Y)*180/pi Yr <- Re(Y) Yi <- Im(Y) plot( f[1:length(f)/2],mag[1:length(f)/2],type="l", xlab="Frequency, Hz",ylab="Amplitude") #plot( f[1:length(f)/2],mag[1:length(f)/2],type="l", xlab="Time Period, Day",ylab="Amplitude") grid(NULL,NULL, col = "lightgray", lty = "dotted",lwd = 1) #UNIT 3 ############################################################################## #Bootstrapping #Example 3.1 A=c(1, 2, 4, 4, 10) # set up the data sample(A, replace=T) # resample once resampleA= lapply(1:20, function(i) sample(A, replace = T)) # resample 20 times rMean <- sapply(resampleA, mean)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern