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

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Fri Apr 7 16:41:35 CEST 2006


Achim Zeileis wrote:

>On Fri, 07 Apr 2006 11:08:07 +0200 Diethelm Wuertz wrote:
>
>  
>
>>Come On!!!!
>>    
>>
>
>Could we please *not* have discussions like this? This is not a
>competition (at least IMHO).
>  
>
Just one remark to this: When I was a student I hated it when a teacher 
said,
"it is (very) easy to show that", or "it is (much) easier to do" and it 
was never
shown or done ...
When this is the case, why one  don't show it or do it? The student will 
be happy.
Then I think *Come on!!!*  is neither a rude nor an unfair reaction nor the
signal for a competetion, it is just a reminder that it would be fine to 
show the
easy way to the student. He will be thankful.

Anyway, if somebody felt be attacked by the selection of my words, I
apologize - but not for the content behind my words.

Nevertheless, my heart was really happy about the many positive reactions
and code solutions submitted which wouldn't be happened otherwise.

DW
please also read my other remarks further down ...

>Except for the alignment part which I've omitted for simplicity, you can
>do:
>
>## get data from yahoo
>library("tseries")
>dax <- get.hist.quote("^GDAXI", start = "2005-06-01")[, "Close"]
>dcx <- get.hist.quote("DCX.DE", start = "2005-06-01")[, "Close"]
>
>## merge -> returns -> lm
>dcxdax <- merge(dcx, dax, all = FALSE)
>ddret <- diff(log(dcxdax))
>lm(dcx ~ dax, data = as.data.frame(ddret))
>
>Best,
>Z
>
>  
>
Thanks for this clear part of code.
DW

Two other comments:

To have a  great flexibility yahooImport()  requests the download query. 
This may be not
very user friendly, but it gives a great flexibility.

Compare:
          dax1 = 
as.timeSeries(yahooImport(query="s=^GDAXI&a=0&b=1&c=2003&g=w&x=.csv")@data 
) [,"Close"]
          dax2 = get.hist.quote (instrument = "^GDAXI", 
start="2003-01-01", quote = "Close", compression = "w")

Like get.hist.quote() one can make it more user friendly very easily: [I 
show it]


getYahoo =
function(symbol = "^GDAXI", start = "2003-01-01", quote = "Close",  
aggregation = "w", class = c("timeSeries", "zoo"))
{
    # Extend it ...
    year = substring(start, 1, 4)
    month = as.character(as.integer(substring(start, 6,7))-1)
    day = substring(start, 9, 10)
    query = paste("s=", symbol, "&a=", month, "&b=", day, "&c=",
        year, "&g=", aggregation, "&x=.csv", sep = "")    
    X = yahooImport(query="s=^GDAXI&a=0&b=1&c=2003&g=w&x=.csv")@data
    ans = as.timeSeries(X)[, quote]
    if (class[1] == "zoo")  ans = as.zoo(ans)
    ans
}

also use:
as.zoo.timeSeries = function(x, ...)  zoo(seriesData(x), 
as.character(seriesPositions(x))) }


Then you can use any code proposal ...

The download results either in a timeSeries object or optionally in a 
zoo Object         
 
Convert zoo to timeSeries ...

as.timeSeries.zoo = function(x, ...) {
    # make as.timeSeries() generic in Rmetrics ... (Still to Improve it)
    timeSeries(as.matrix(x), attr(x, "index"), myFincenter = "GMT")
}

Why Rmetrics uses mergeSeries, diffSeries, logSeries instead of the
generic functions merge, diff, log, etc. is for compatibility reasons 
with SPlus/Finmetrics.
With the next version of Rmetrics I will add the generic functions for 
the Rmetrics
timeDate and timeSeries objects.


regards Diethelm

>>Thanks Diethelm
>>
>>    
>>
>>>      http://www.mayin.org/ajayshah/KB/R/html/o6.html
>>>      http://www.mayin.org/ajayshah/KB/R/html/o5.html
>>>
>>> 
>>>
>>>      
>>>
>>_______________________________________________
>>R-sig-finance at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>
>>    
>>
>
>  
>



More information about the R-sig-finance mailing list