从tickdata到时间框架(OHLC) - R

时间:2017-11-20 19:07:34

标签: r csv xts bitcoin

我希望从比特币交易的滴答滴答时间戳开始获得5分钟,h1和h4时间帧OHLC时间序列。使用脚本我从CSV格式的api.bitcoincharts.com/v1/CSV下载tickdata

library(xts)
library(highfrequency)
library(quantmod)
bitcoin_file <- "bitbayUSD.csv.gz"
# 1coinUSD.csv.gz
# bitstampUSD.csv.gz
URL <- "http://api.bitcoincharts.com/v1/csv"
source_file <- file.path(URL,bitcoin_file)
dataDir <-"C:/r_data/BTC/CSV"
dest_file <- file.path(dataDir,bitcoin_file)
download.file(source_file,destfile = dest_file)
raw <- read.csv(gzfile(dest_file),header=FALSE)
names(raw) <- c("unixtime","price","amount")
raw$datetime <- as.POSIXct(raw$unixtime, origin="1970-01-01", tz="GMT")

以下是 head(raw,3)

的内容
    unixtime  price    amount            datetime
1 1400230012 446.67 0.0823241 2014-05-16 08:46:52
2 1400230023 446.67 0.0657015 2014-05-16 08:47:03
3 1400230048 446.59 0.0679500 2014-05-16 08:47:28

我对'金额'栏不感兴趣,所以我可以放弃它。考虑到在刻度的那一刻,Open,High,Low和Close具有相同的值,我创建了另一个data.frame:

rawx= raw[-c(3)] #remove amount column
rawx$Open = rawx$price
rawx$High = rawx$price
rawx$Low = rawx$price
rawx$Close = raw$price
rawx$datetime = raw$datetime
rawx= rawx[-c(2)] #remove price column, which is redundant

以下是 head(rawx,3)

的内容
  unixtime            datetime   Open   High    Low  Close
1 1400230012 2014-05-16 08:46:52 446.67 446.67 446.67 446.67
2 1400230023 2014-05-16 08:47:03 446.67 446.67 446.67 446.67
3 1400230048 2014-05-16 08:47:28 446.59 446.59 446.59 446.59

我现在陷入困境,我试图将其转换为XTS时间序列,**head(rawx,3)**

     unixtime     datetime              Open          High          Low           Close        
2014-05-16 08:46:52 "1400230012" "2014-05-16 08:46:52" "   446.6700" "   446.6700" "   446.6700" "   446.6700"
2014-05-16 08:47:03 "1400230023" "2014-05-16 08:47:03" "   446.6700" "   446.6700" "   446.6700" "   446.6700"
2014-05-16 08:47:28 "1400230048" "2014-05-16 08:47:28" "   446.5900" "   446.5900" "   446.5900" "   446.5900"
Warning message:
timezone of object (GMT) is different than current timezone (). 

并使用to.period函数但我收到此错误:

> rawx=xts(rawx, order.by = rawx$datetime)
> m5=to.period(rawx, period = "minutes", k=5)
Error in to.period(rawx, period = "minutes", k = 5) : unsupported type
> 

有解决方案吗?我的猜测是问题是关于POSIX日期时间转换,不知何故,“xts”函数“to.period”无法读取该信息。

提前谢谢。

0 个答案:

没有答案