R 读取 通达信V5.92 日数据文件 *.day 代码

theFile是 文件的全路径例如 c:\zxzq\vipdoc\sh\lday\sh600100.day

函数返回的是时间序列 xts,zoo类型。

通达信V5.92 是当前比较主流的版本。

注意,该数据文件包含的不是复权数据,是原始的真实数据。

如需读取 5分钟数据代码,欢迎留言交流。

getHS1DayDataFromRaw <- function(theFile,sPeriod="/"){
    dat <- readBin(theFile, 'raw', n=100000000)
    mat <- matrix(dat, byrow=T, ncol=32)
    dt <- as.Date(as.character(apply(mat[, 1:4, drop=FALSE], 1, function(x) readBin(x,"integer",size=4) )), '%Y%m%d')
    op <- apply(mat[, 5:8, drop=FALSE], 1, function(x) readBin(x,"integer",size=4)/100 )
    hi <- apply(mat[, 9:12, drop=FALSE], 1, function(x) readBin(x,"integer",size=4)/100 )
    lo <- apply(mat[, 13:16, drop=FALSE], 1, function(x) readBin(x,"integer",size=4)/100 )
    cl <- apply(mat[, 17:20, drop=FALSE], 1, function(x) readBin(x,"integer",size=4)/100 )
    am <- apply(mat[, 21:24, drop=FALSE], 1, function(x) readBin(x,"double",size=4) )
    vo <- apply(mat[, 25:28, drop=FALSE], 1, function(x) readBin(x,"integer",size=4))
    theData <- data.frame(dt, op, hi, lo, cl, vo, am,stringsAsFactors=FALSE)
    theData <- try.xts(theData[,c(2,3,4,5,6,7)],as.POSIXct(theData[,1],
                            tryFormats = c("%Y-%m-%d","%Y/%m/%d","%Y/%m/%d %H:%M:%OS")))
    names(theData) <- c("Open", "High","Low", "Close",    "Volume","Amount" );
    return(theData[sPeriod])

 

 注册OKEx并首次购买金额在200-1000元之间数字货币,您可获得价值20元的比特币奖励  点击链接 OKEX交易所(国内三大之一)。

最后,提供几个福利,不用科学上网也能注册或访问几大交易所:

1,注册中币(ZB),请访问 http://www.zbex3.com/

2,注册OKEX,请访问 http://www.okex.me/

End