[R-sig-finance] Using data from yahooImport (fBasics)

Ajay Narottam Shah ajayshah at mayin.org
Fri Apr 7 07:03:13 CEST 2006


> require(fMultivar)
> myFinCenter = "GMT"
> 
> # Download -> Data Slot -> As Time Series -> Close
> # GET Closing Prices: 2005-06-01 until now - see help(yahooImport)
> query = "s=^GDAXI&a=5&b=1&c=2005&d=0&q=31&f=2009&z=^GDAXI&x=.csv"
> DAX.CLOSE = as.timeSeries(yahooImport(query)@data)[,"Close"]
> query = "s=DCX.DE&a=5&b=1&c=2005&d=0&q=31&f=2009&z=DCX.DE&x=.csv"
> DCX.CLOSE = as.timeSeries(yahooImport(query)@data)[,"Close"]
> 
> # Align to daily dates: --- So both time series will have afterwards the 
> same time stamps
> # Consult help(timeSeries)
> DAX.ALIGNED = alignDailySeries(DAX.CLOSE, method = "interp")
> DCX.ALIGNED = alignDailySeries(DCX.CLOSE, method = "interp")
> 
> # Cut Common Piece from each:  --- help(timeDate)
> START = modify(c(start(DAX.ALIGNED), start(DCX.ALIGNED)), "sort")[2]
> END = modify(c(end(DAX.ALIGNED), end(DCX.ALIGNED)), "sort")[1]
> DAX.CUTTED = cutSeries(DAX.ALIGNED, from = START, to = END)
> DCX.CUTTED = cutSeries(DCX.ALIGNED, from = START, to = END)
> 
> # Merge the two Series:
> DCXDAX = mergeSeries(DCX.CUTTED, DAX.CUTTED at Data, units = c("DCX", "DAX"))
> 
> # Compute Return Series:
> DCXDAX.RET = as.data.frame(returnSeries(DCXDAX))
> 
> # Compute Beta: --- linear Modelling:
> c(Beta = lm(formula = DCX ~ DAX, data = DCXDAX.RET)$coef[2])
> 
> Beta.DAX
> 1.320997

This looks like hard work! It's easier using R + zoo:

       http://www.mayin.org/ajayshah/KB/R/html/o6.html
       http://www.mayin.org/ajayshah/KB/R/html/o5.html

-- 
Ajay Shah                                      http://www.mayin.org/ajayshah  
ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.



More information about the R-sig-finance mailing list