R 讀取 通達信V5.92 日數據文件 *.day 代碼

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

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

函數返回的是時間序列 xts,zoo類型。

通達信V5.92 是當前比較主流的版本。

注意,該數據文件包含的不是復權數據,是原始的真實數據。

如需讀取 5分鐘數據代碼,歡迎留言交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章