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])