#########################################计算简单收益率#install.packages('xts')library(xts)da=read.csv('luowen.csv')#读取文件class(da)#查看文件类型head(da)#查看数据da=da[,c(2,9)]#截取时间和收盘价c=da[,2]#获取收盘价head(c)df=xts(c,order.by=as.Date(da[,1]))#时间序列df=df[-1,]df=df[!is.na(df)]df1=lag(df,1)#滞后一天df1=df1[-1,]df1=df1[!is.na(df1)]data=merge(df,df1)head(data)simplerate=(df-df1)/dfhead(simplerate)############使用R包计算简单收益率install.packages('quantmod')library(quantmod)rate=periodReturn(df,period="daily",type="arithmetic")head(rate)head(simplerate)使用的数据是螺纹钢的日线数据。
新闻热点
疑难解答