R_quantmod

getSmybols()

读取数据,得到的数据类型为xts、zoo…

> args(getSymbols)
function (Symbols = NULL, env = parent.frame(), reload.Symbols = FALSE, verbose = FALSE, warnings = TRUE, src = "yahoo", symbol.lookup = TRUE, auto.assign = getOption("getSymbols.auto.assign", TRUE),    ...)

>> ## 下载数据
download = function(stock, from, to){  
df = getSymbols(stock, from = from, to = to, env = environment(),auto.assign = FALSE)                   names(df) = c("Open","High","Low","Close","Volume","Adjusted")                                     return(df)             #write.zoo(df,file=paste(stock,".csv",sep=""),sep=",",quote=FALSE) #保存到本地
}
env
where to create objects. Setting env=NULL is equal to auto.assign=FALSE
reload.Symbols
boolean to reload current symbols in specified environment. (FALSE)
verbose
boolean to turn on status of retrieval. (FALSE)
src
character string specifying sourcing method. (yahoo)
symbol.lookup
retrieve symbol’s sourcing method from external lookup (TRUE)
auto.assign:自动分配得到名为代码的数据,而且 Note that only one symbol at a time may be requested when auto assignment is disabled
should results be loaded to env If FALSE, return results instead. As of 0.4-0, this is the same as setting env=NULL. Defaults to TRUE。

例子:

### 自动分配下的重命名:
setSymbolLookup(QQQ= ' yahoo ' ,SPY= ' google ' )
# loads QQQQ from yahoo (set with setSymbolLookup)
# loads SPY from MySQL (set with setSymbolLookup)
getSymbols(c( ' QQQ ' , ' SPY ' ))


# loads symbol from MySQL database (set with setDefaults)
getSymbols( ' DIA ' , verbose=TRUE, src= ' MySQL ' )

# 改变数据类型:loads Ford as time series class ts
getSymbols( ' F ' ,src= ' yahoo ' ,return.class= ' ts ' )

### 环境和作用域相关
# load into a new environment
data.env <- new.env()
getSymbols("YHOO", env=data.env)
ls.str(data.env)
# constrain to local scope
try(local( {
getSymbols("AAPL") # or getSymbols("AAPL", env=environment())
str(AAPL)
}))
exists("AAPL") # FALSE

# assign into an attached environment
attach(NULL, name="DATA.ENV")
getSymbols("AAPL", env=as.environment("DATA.ENV"))
ls("DATA.ENV")
detach("DATA.ENV")
# directly return to caller
str( getSymbols("AAPL", env=NULL) )
str( getSymbols("AAPL", auto.assign=FALSE) ) # same
## End(Not run)

取时间序列子集

#from/to/start为时间点
bef = paste(from, '/', start, sep = '')      #前日期区间
          aft = paste(start, '/', to, sep = '')        #后日期区间
          bef_stock_rtn = stock_rtn[bef] #前段个股对数收益率
          bef_market_rtn = market_rtn[bef]#前段市场对数收益率

          aft_stock_rtn = stock_rtn[aft] #前段个股对数收益率
          aft_market_rtn = market_rtn[aft]#前段市场对数收益率

其他规则如下:

#sample.xts为一时间序列
sample.xts['2007']  # all of 2007
sample.xts['2007-03/']  # March 2007 to the end of the data set
sample.xts['2007-03/2007']  # March 2007 to the end of 2007
sample.xts['/'] # the whole data set
sample.xts['/2007'] # the beginning of the data through 2007
sample.xts['2007-01-03'] # just the 3rd of January 2007
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值