Timing the marketstock example 24 t 5 time 0 t dt 001

Info iconThis preview shows pages 3–6. Sign up to view the full content.

View Full Document Right Arrow Icon
#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) sort(rMean) hist(rMean) #Bollinger Bands #Example 3.2 #Use quantmod built-in function library(quantmod) getSymbols("601318.SS") length(`601318.SS`[,1]) x=`601318.SS`[c(1385:1485)]
Background image of page 3

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

View Full Document Right Arrow Icon
chartSeries(x, theme="white", TA="addVo();addBBands();addCCI();addRSI();addMFI()") Donchian Channel #Example 3.3 #Use TTR and quantmod library(quantmod) library(TTR) s <- getSymbols('^HSI', auto.assign=FALSE) chart_Series(s, subset="2013-01::") band=DonchianChannel(s[,2:3], n = 10 ) add_TA( band ,on=1, col="blue") #Example 3.4 #Use TTR and quantmod library(quantmod) library(TTR) s <- getSymbols('^HSI', auto.assign=FALSE) chart_Series(s, subset="2013-01::") ####band=DonchianChannel(s[,2:3], n = 10 ) band=BBands(s[,c(2,3,4)] ) band$pctB=NULL add_TA( band ,on=1, col="green") #UNIT 4 ############################################################################## #Forecast #Example 4.1 install.packages("quadprog") install.packages("forecast") library(quadprog) library(forecast) library(quantmod) getSymbols("^HSI") a=HSI[c(1455:1545)] x=a[,4] b=meanf(x, h=20) #b=naive(x, h=20) or b=rwf(x, h=20) #b=rwf(x, drift=T, h=20) plot(b, type="l") #Generate White Noise #Example 4.2 w= rnorm(500,0,1) # 500 N(0,1) variates v = filter(w, sides=2, rep(1/3,3)) # moving average par(mfrow=c(2,1)) plot.ts(w, main="white noise") plot.ts(v, main="moving average") #How to calculate Autocovariance #Example 4.3 x <- c(1,2,3,3,2,3,4,5,6) acf(x,plot=F, type="covariance") #then it returns # 0 1 2 3 4 5 6 7 8 # 2.173 1.081 0.211 -0.103 -0.108 -0.163 -0.502 -0.816 -0.686
Background image of page 4
#Now we want to get the same results by ourselves #lag=0 x <- c(1,2,3,3,2,3,4,5,6) mx=mean(x) n=length(x) diffx=x-mx covar_lag0=sum(diffx*diffx)/n print(covar_lag0) #it shows 2.17284 #lag=1 diffy=diffx[-1] length(diffy)=n covar_lag1=sum(diffx*diffy, na.rm=T)/n print(covar_lag1) #it shows 1.080933 #lag=2 diffy=diffy[-1] length(diffy)=n covar_lag2=sum(diffx*diffy, na.rm=T)/n print(covar_lag2) #it shows 0.2112483 #How to calculate Autocorrelation #Example 4.4 x <- c(1,2,3,3,2,3,4,5,6) acf(x,plot=F) #Autocorrelations of series x , by lag # 0 1 2 3 4 5 6 7 8 # 1.000 0.497 0.097 -0.047 -0.050 -0.075 -0.231 -0.376 -0.316
Background image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page3 / 15

Timing the marketstock Example 24 T 5 time 0 T dt 001 s n...

This preview shows document pages 3 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online