[R-SIG-Finance] rename a large number of stock codes

Marco Sun hs13322 at my.bristol.ac.uk
Fri Dec 5 04:50:04 CET 2014


Hey Nick,

Gorgeous! This perfectly answers my question. Thank you very much!

Best,

Marco

On 4 Dec 2014, at 06:36 pm, Nick White <n-e-w at qtradr.net> wrote:

> Hi Marco,
> 
> When calling stocks with numerical tickers (like most Asia ex-Japan markets) in R, you should typically reassign them using `get` when working interactively -- or `assign`.
> 
> In this case, you might want to have something like
> 
> TBEA <- get("600089.SS")
> 
> Now you have a simple text named variable in the environment to work with. ie:
> 
> > head(TBEA)
>            600089.SS.Open 600089.SS.High 600089.SS.Low 600089.SS.Close 600089.SS.Volume 600089.SS.Adjusted
> 2007-01-01          14.19          14.53         13.78           14.03                0               2.54
> 2007-01-02          14.19          14.53         13.78           14.03                0               2.54
> 2007-01-03          14.19          14.53         13.78           14.03                0               2.54
> 2007-01-04          14.22          15.11         14.03           14.58         75563100               2.64
> 2007-01-05          14.59          16.03         14.46           16.03         72978600               2.90
> 2007-01-08          16.05          16.60         15.85           16.16         41251600               2.93
> 
> You can extend this to a large group of tickers using a for loop (though it's not the only way). 
> 
> In the spirit of how you wrote the question, though, you could do something like this:
> 
> # Put your numerical tickers in a variable, `num.code`
> 
> > num.code <- c("600089.SS","600202.SS")
> 
> # Put their text equivalents in a different variable, `text.code`
> 
> > text.code <- c("TBEA","HAC")
> 
> #make a data.frame of the two
> 
> code.frame <- data.frame(num.code,text.code,stringsAsFactors=FALSE)
> 
> > code.frame
>    num.code text.code
> 1 600089.SS      TBEA
> 2 600202.SS       HAC
> 
> # now call your Symbols
> 
> > getSymbols(code.frame$num.code)
> 
> # and now run a simple for loop to `assign` the numerical codes to the text codes
> 
> > for (i in 1:length(code.frame)){
>   
>   assign(code.frame$text.code[i],get(code.frame$num.code[i]),envir=.GlobalEnv)
> }
> 
> 
> # You will now have two variables, TBEA and HAC which are `xts` objects that you can now manipulate how you wish
> 
> > str(TBEA)
> An ‘xts’ object on 2007-01-01/2014-12-03 containing:
>   Data: num [1:2041, 1:6] 14.2 14.2 14.2 14.2 14.6 ...
>  - attr(*, "dimnames")=List of 2
>   ..$ : NULL
>   ..$ : chr [1:6] "600089.SS.Open" "600089.SS.High" "600089.SS.Low" "600089.SS.Close" ...
>   Indexed by objects of class: [Date] TZ: UTC
>   xts Attributes:  
> List of 2
>  $ src    : chr "yahoo"
>  $ updated: POSIXct[1:1], format: "2014-12-04 21:24:39"
> 
> Hope that helps,
> 
> n.
> 
> 
> 
> 
> 
> 
> On Thu, Dec 4, 2014 at 7:58 PM, Marco Sun <hs13322 at my.bristol.ac.uk> wrote:
> Hi, sorry for the previous emails, I am now opening a new topic to demonstrate my problem clearly.
> 
> 
> 
> I wish to retrieve a bunch of Chinese stock through getSymbols.
> 
> 
> 
> symbols<-c("600089.SS","600202.SS")
> 
> getSymbols(symbols)
> 
> 
> 
> These stock codes are not easily recognised nor being processed in further analysis, i.e. R does not recognise 600089.SS, instead, it recognises `600089.SS`.
> 
> 
> 
> > 600089.SS
> 
> Error: unexpected symbol in "600089.SS"
> 
> 
> 
> > class(`600089.SS`)
> 
> [1] "xts" "zoo"
> 
> `
> 
> Therefore, I wish to replace symbols with some simple tickers called stockcodes. I can easily replace the name of 600089.SS via:
> 
> 
> 
> stockcodes<-c("TBDG","HKT")
> 
> TBDG<-`600089.SS`
> 
> 
> 
> However, it could take a lot while if I have a large portfolio and I do have a well-diversified portfolio under analysis.
> 
> 
> 
> Another approach came up to me was using setSymbolLookup like this:
> 
> 
> 
> > setSymbolLookup(TBDG=list(name="600089.SS"))
> 
> > getSymbols("TBDG")
> 
> [1] "TBDG"
> 
> 
> 
> But I got error when I was trying to use for loop as the following:
> 
> 
> 
> for(i in 1:2)
> 
> {
> 
>  setSymbolLookup(stockcodes[i]=list(name=symbols[i]))
> 
>  getSymbols(stockcodes[i])
> 
> }
> 
> 
> 
> Any comment would be appreciated.
> 
> 
> 
> Cheers,
> 
> 
> 
> Marco
> 
> 
> 
> 
> 
> 
>         [[alternative HTML version deleted]]
> 
> _______________________________________________
> R-SIG-Finance at r-project.org 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]]



More information about the R-SIG-Finance mailing list