Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- new.env()
- library(XML)
- library(quantmod)
- Sys.setenv(TZ="Asia/Hong Kong")
- #******************************************************************************
- # hkex function to get all Hongkong StockExchange Mainboard listed companies
- # usage : mc=hkex()
- hkex <-function(){
- rawCODE <- readHTMLTable('http://www.hkex.com.hk/eng/market/sec_tradinfo/stockcode/eisdeqty_pf.htm')
- n.rows <- unlist(lapply(rawCODE, function(t) dim(t)[1]))
- mcr=rawCODE[[which.min(n.rows-1)]]
- # now remove colname line of table
- mcr <- mcr[-c(1), ]
- # change the colnames to what we actually want
- colnames(mcr)=c('SYMBOL','NAME','LOT','R1','R2','R3','R4')
- # this should be the actual stockcode count
- maxrows=n.rows[which.min(n.rows-1)]-1
- rownames(mcr)=1:maxrows
- # show head/tails of what we got
- mc=tail(mcr,maxrows) # mc is where everything resides now
- # for quantmod we need to massage the Symbols into form xxxx.HK
- # first add the.HK
- mc[,1]=paste(mc[,1],'.HK',sep="")
- #now remove the first 0
- mc[,1]=substring(mc[,1], 2, 9)
- # see the magic
- print(head(mc))
- print(tail(mc))
- # just to see if we actually fetched something
- if (mc[,1][1] == "0001.HK"){
- print(paste('STOCKS in LIST : ',maxrows))
- } else print("An Error occured stocks not fetched correctly")
- # All stocks listed in HKEX mainboard are in vector mc
- return(mc)
- }
- #*******************************************************************************
- #testing hkex
- mc=hkex()
- maxrows=NROW(mc)
- show(mc)
- writeLines(paste("Currently Listed Stocks on Hongkong Stock Exchange MainBoard :",maxrows))
- # Create 5 random portfolios with 5 stocks each and display the latest quotes
- runs=5
- for (k in 1:runs){
- s=sample(maxrows,runs,replace=FALSE,prob=runif(maxrows)) # this generates random stockcodes
- codes=mc[,1][s]
- pfnames=as.character(mc[,2][s])
- writeLines(paste("\n================ PORTFOLIO : ",k,"==================="))
- show(paste(codes,pfnames))
- writeLines(paste("\n================ Quotes ==========================="))
- show(getQuote(codes))
- }
- #*******************************END*********************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement