library(data.table)
library(QuantTools)
date_from <- '2018-11-01'
date_to <- '2018-11-30'
ticker <- 'SPFB.RTS'
# get days
dataDaily <- get_finam_data(ticker, date_from, date_to, 'day')
# get hours
dataHourly <- get_finam_data(ticker, date_from, date_to, 'hour')
# percent change of the day
dataDaily$pc <- ((dataDaily$close - dataDaily$open)/dataDaily$open)*100
# mark days with > 2 percent change
dataDaily$isBigCh <- dataDaily$pc[dataDaily$pc > 2]
因此,我上面有一个代码,可以每天/每小时下载一次期货的OHLC数据。
问题:
1)如何将标记从dataDaily$isBigCh
移动到dataHourly
?这似乎并不容易,因为这些数据帧具有不同的时间格式和不同的行长。
dataHourly$time # has a format like this 2018-11-09 23:00:00
dataDaily$date # has a format like this 2018-11-09
2)如何在dataHourly$time
中选择当天的第一个小节?
答案 0 :(得分:0)
稍作修改以提高可读性
# percent change of the day
dataDaily[, price_change := ( close / open - 1 ) * 100 ]
# mark days with > 2 percent change
dataDaily[, isBigCh := price_change > 2 ]
问题1
# add date column to hourly data
# note that 00:00 time corresponds to 23:00-00:00 candle
dataHourly[, date := as.Date( time - as.difftime( '01:00:00' ) ) ]
# copy dataDaily isBigCh to dataHourly isBigChDaily
dataHourly[ dataDaily, isBigChDaily := isBigCh, on = 'date' ]
问题2
# select first bar of the day
dataHourly[, .SD[1], by = date ]
可选
# remove date column from hourly data
dataHourly[, date := NULL ]
注意
library(data.table)
不必要,因为QuantTools
自动加载
请阅读data.table
手册,它将为您节省大量时间,以找出与您要求的类似的简单操作