Sortrmean histrmean bollinger bands example 32 use

Info icon This preview shows pages 3–7. Sign up to view the full content.

View Full Document Right Arrow Icon
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)]
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
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 #Now we want to get the same results by ourselves #we know from example 4.3 #lag=0 correl_lag0= covar_lag0/covar_lag0 print(correl_lag0) #lag=1 correl_lag1= covar_lag1/covar_lag0 print(correl_lag1) #lag=2 correl_lag2= covar_lag2/covar_lag0 print(correl_lag2) #Model identification (i.e. find p, q) #Example 4.5 data=c(60,43,67,50,56,42,50,65,68,43,65,34,47,34,49,41, 13,35,53,56,16,43,69,59,48,59,86,55,68,51,33,49, 67,77,81,67,71,81,68,70,77,56) datats = ts(data) plot(datats ) datatsdiff1 <- diff(datats, differences=1) acf(datatsdiff1, lag.max=20) #find p by the plot pacf(datatsdiff1, lag.max=20) #find q by the plot install.packages("forecast")
Image of page 5

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

View Full Document Right Arrow Icon
library("forecast") auto.arima(data) #get p and q by auto.ariam() datatsarima <- arima(datats, order=c(0,1,1)) plot( datatsforecasts <- forecast.Arima( datatsarima, h=5) ) #Predicting values using arima #Example 4.6 data<-c(100.8, 81.6, 66.5, 34.8, 30.6, 7, 19.8 , 92.5 , 154.4, 125.9, 84.8, 68.1, 38.5, 22.8, 10.2, 24.1, 82.9 , 132 , 130.9, 118.1, 89.9, 66.6, 60 , 46.9, 41 , 21.3, 16 , 6.4 , 4.1 , 6.8 , 14.5, 34 , 45 , 43.1, 47.5, 42.2, 28.1 , 10.1 , 8.1 , 2.5 , 0, 1.4 , 5 , 12.2, 13.9, 35.4, 45.8 , 41.1 , 30.4 , 23.9 , 15.7, 6.6 , 4 , 1.8 , 8.5 , 16.6, 36.3 , 49.7 , 62.5 , 67 , 71, 47.8, 27.5, 8.5 , 13.2, 56.9, 121.5, 138.3, 103.2, 85.8 , 63.2, 36.8, 24.2, 10.7, 15 , 40.1, 61.5 , 98.5 , 124.3, 95.9 , 66.5, 64.5, 54.2, 39 , 20.6, 6.7 , 4.3 ,? 22.8, 54.8 , 93.8 , 95.7, 77.2, 59.1, 44 , 47 ,?30.5, 16.3 , 7.3 , 37.3 , 73.9 ) data.ts=ts(data) aa <- arima(data.ts, order=c(2,1,1)) aaa <- forecast.Arima(aa, h=5) data.arma <- arima(data.ts, order=c(2,1,1)) predict <- forecast.Arima(data.arma, h=5) #Is it real stock price time series?
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern