问题描述
我有一个股票交易事件的 xts 序列,我想对其进行处理以生成 1 分钟的 OHLC 时间序列.例如这组交易:
I have an xts sequence of stock trade events that I want to process to generate 1 minute OHLC time series. For instance this set of trades:
Timestamp Price Size
9:30:00.123 12.32 200
9:30.00.532 12.21 100
9:30.32.352 12.22 500
9:30.45.342 12.35 200
应该导致 9:30:00 记录:
Should result in the 9:30:00 record:
Timestamp Open High Low Close
9:30:00 12.32 12.35 12.21 12.35
我处理这个问题的方法是按分钟拆分原始交易系列:
The way I approached this is to split the original trade series by the minute:
myminseries = do.call(rbind, lapply(split(mytrades, "minutes"), myminprocessing))
这产生了我想要的记录,但有一个问题:如果股票在给定的一分钟内没有任何交易,我将完全错过那一分钟的记录.我想要的是为丢失的交易分钟记录全为 0.例如,如果在 9:31:00 没有任何交易,我应该:
This produce the records I want but there is a problem: if the stock doesn't have any trade in a given minute, I will miss that minute record entirely. What I want instead is to have an all 0s record for the missing trades minute. For example, if there isn't any trade at 9:31:00 I should have:
Timestamp Open High Low Close
9:30:00 12.32 12.35 12.21 12.35
9:31:00 0 0 0 0
9:32:00 12.40 12.42 12.38 12.42
如何回填 1 分钟系列?还是应该使用与 split() 完全不同的方法?
How can I backfill the 1 minute series? Or should I use a completely different approach than split()?
推荐答案
有 to.period()
函数,例如 xts 中的 to.minute()
There are to.period()
functions, eg to.minute()
in xts which do that.
德克
这篇关于R xts:从第二个事件生成 1 分钟的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!