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

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Fri Apr 7 11:58:21 CEST 2006


Why you should definitely use Rmetrics to solve Owe Jessens problem:

# Be sure you have the latest Version of Rmetrics ...65 from the CRAN Server

# Download -> Data Slot -> As Time Series -> Close
# GET Closing Prices: 2005-06-01 until now
require(fCalendar)
myFinCenter = "GMT"
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"]

# Assume we have the Data, then
DAX = alignDailySeries(DAX.CLOSE)
DCX = alignDailySeries(DCX.CLOSE)
DAX = cutSeries(DAX, start(DCX), end(DCX))
DCX = cutSeries(DCX, start(DCX), end(DCX))
DCA = returnSeries(mergeSeries(DCX, DAX at Data, c("DCX", "DAX")))
c(Beta = lm(DCX ~ DAX, as.data.frame(DCA))$coef[2])

Beta.DAX
1.348041

When you have downloaded the data,
you can do it with default settings in 6 lines !!!!

Note you have several alternatives to align the data, and to
compute the returns. Check the arguments of the functions
alignDailySeries() and returnSeries().

Diethelm




Ajay Narottam Shah wrote:

>>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
>
>  
>



More information about the R-sig-finance mailing list