Using getSymbol in a R function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using getSymbol in a R function

Yitao Zhang
Hey guys,

I'm trying to write a function that will tell me if a pair of stocks are stationary. However, I'm having some trouble with both using the getSymbol function and with using the output to generate a ratio. This is my code

correlation_check=function(ticker1,ticker2) {
        stock1=getSymbols(toString(ticker1), src="yahoo", from= '2005-6-01', to ='2012-6-21')
        stock2=getSymbols(toString(ticker2), src="yahoo", from= '2005-6-01', to ='2012-6-21')
        ratio=(toString(stock1)$toString(stock1).Open)/(toString(stock2)$toString(stock2).Open)
        return=adf.test(ratio)
}

The function is supposed to return whether the 2 ratio of the 2 stock prices are stationary.

Problem 1: getSymbol requires the symbol to be entered as e.g "COKE". How can I run the ticker1/ticker2 into the function?

Problem 2: If getSymbol works, stock2's class becomes a character. How do I get a column within the ticker for each stock for a ratio?

Thanks so much!

--
Yitao Zhang
Stanford University '14
B.A. in Economics
(650)-391-6966 | [hidden email]




        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: Using getSymbol in a R function

Ivan Popivanov-2
Assuming that ticker1 and ticker2 are the strings containing the symbol,
aka "AAPL", then:

stock1 = getSymbols(ticker1, src="yahoo", from= '2005-06-01', to
='2012-06-21', auto.assign=FALSE)

Before computing the ratio, I'd merge them first:

mm = merge( Op(stock1), Op(stock2), all=FALSE )
ratio = mm[,1]/mm[,2]

You may want to check the docs for getSymbols, merge, Op. ;)

Hope this helps,
Ivan


On Fri, Apr 19, 2013 at 6:32 PM, Yitao Zhang <[hidden email]> wrote:

> Hey guys,
>
> I'm trying to write a function that will tell me if a pair of stocks are
> stationary. However, I'm having some trouble with both using the getSymbol
> function and with using the output to generate a ratio. This is my code
>
> correlation_check=function(ticker1,ticker2) {
>         stock1=getSymbols(toString(ticker1), src="yahoo", from=
> '2005-6-01', to ='2012-6-21')
>         stock2=getSymbols(toString(ticker2), src="yahoo", from=
> '2005-6-01', to ='2012-6-21')
>
> ratio=(toString(stock1)$toString(stock1).Open)/(toString(stock2)$toString(stock2).Open)
>         return=adf.test(ratio)
> }
>
> The function is supposed to return whether the 2 ratio of the 2 stock
> prices are stationary.
>
> Problem 1: getSymbol requires the symbol to be entered as e.g "COKE". How
> can I run the ticker1/ticker2 into the function?
>
> Problem 2: If getSymbol works, stock2's class becomes a character. How do
> I get a column within the ticker for each stock for a ratio?
>
> Thanks so much!
>
> --
> Yitao Zhang
> Stanford University '14
> B.A. in Economics
> (650)-391-6966 | [hidden email]
>
>
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.