关于如何获取 xts 中下一个/上一个元素的基本问题

发布于 2024-10-19 06:22:02 字数 365 浏览 4 评论 0原文

获取 xts 对象中当前日期特定交易品种的收盘价

closePrice<-as.double(Cl(get(symbol))[currentDate]) 

我有一个非常基本的问题...假设我可以使用How can I get close Price on the transaction day one day,两天,三天,... ,或者之前/之后n天?

closePriceNDaysBefore<-as.double(Cl(get(symbol))[currentDate - n]) 

不行...

提前致谢。

亲切的问候, 萨摩。

I have a very basic question... Suppose I can get a closing price for specific symbol for current date in an xts object using

closePrice<-as.double(Cl(get(symbol))[currentDate]) 

How can I get close price on the trading day one day, two days, three days, ... or n days before/after?

closePriceNDaysBefore<-as.double(Cl(get(symbol))[currentDate - n]) 

is not ok...

Thanks in advance.

Kind regards,
Samo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱已欠费 2024-10-26 06:22:03

您可以很容易地做到这一点,具体取决于您所说的“当前日期”的含义。

如果当前日期是最后一个条目(刚刚提取到日期数据),那么最后一个和第一个会有所帮助:

> x <- xts(1:10, Sys.Date()-0:9)
> x <- xts(1:10, Sys.Date()-0:9)
> x
           [,1]
2011-02-16   10
2011-02-17    9
2011-02-18    8
2011-02-19    7
2011-02-20    6
2011-02-21    5
2011-02-22    4
2011-02-23    3
2011-02-24    2
2011-02-25    1

# gets the last 3 periods (days here)
> last(x,3)  # or last(x, "3 days")
           [,1]
2011-02-23    3
2011-02-24    2
2011-02-25    1

# this would get you the 3rd day back from the end
> first(last(x,3),1)
           [,1]
2011-02-23    3

如果您需要当前日期来表示您在此特定循环/上下文中关心的日期,则 子集的 which.i=TRUE 参数将有所帮助 - 因为它采用完全相同的快速 ISO 查找,但返回匹配的位置。也就是说,它不做子集。

> x[x["2011-02-25", which.i=TRUE] - 0]  # today
           [,1]
2011-02-25    1

> x[x["2011-02-25", which.i=TRUE] - 1]  # yesterday
           [,1]
2011-02-24    2
> x[x["2011-02-25", which.i=TRUE] - 2]  # 2 days ago...
           [,1]
2011-02-23    3

> x[x["2011-02-25", which.i=TRUE] - 3]  # you get the idea ;-)
           [,1]
2011-02-22    4

> x["2011-02-25", which.i=TRUE] 
[1] 10

You can do this rather easily, depending on what you mean by 'current date'.

If current date is the last entry (just pulled up to date data), then last and first will help:

> x <- xts(1:10, Sys.Date()-0:9)
> x <- xts(1:10, Sys.Date()-0:9)
> x
           [,1]
2011-02-16   10
2011-02-17    9
2011-02-18    8
2011-02-19    7
2011-02-20    6
2011-02-21    5
2011-02-22    4
2011-02-23    3
2011-02-24    2
2011-02-25    1

# gets the last 3 periods (days here)
> last(x,3)  # or last(x, "3 days")
           [,1]
2011-02-23    3
2011-02-24    2
2011-02-25    1

# this would get you the 3rd day back from the end
> first(last(x,3),1)
           [,1]
2011-02-23    3

If instead you need the current date to mean something like the date you care about in this particular loop/context, the which.i=TRUE argument to the subset will help - as it employes the very same fast ISO lookup, but returns the position(s) that match. That is, it doesn't do the subset.

> x[x["2011-02-25", which.i=TRUE] - 0]  # today
           [,1]
2011-02-25    1

> x[x["2011-02-25", which.i=TRUE] - 1]  # yesterday
           [,1]
2011-02-24    2
> x[x["2011-02-25", which.i=TRUE] - 2]  # 2 days ago...
           [,1]
2011-02-23    3

> x[x["2011-02-25", which.i=TRUE] - 3]  # you get the idea ;-)
           [,1]
2011-02-22    4

> x["2011-02-25", which.i=TRUE] 
[1] 10
花辞树 2024-10-26 06:22:03

假设您的数据是每日数据并且仅包括交易日,请在子集化之前滞后时间序列。

closePriceNDaysBefore <- as.double(lag(Cl(get(symbol)),n)[currentDate])

如果这不起作用,请更具体地说明您的数据结构。

Assuming your data are daily and only include trading days, lag the time series before subsetting.

closePriceNDaysBefore <- as.double(lag(Cl(get(symbol)),n)[currentDate])

If that doesn't work, please be more specific about your data structure.

淡笑忘祈一世凡恋 2024-10-26 06:22:03

记住,您可以通过获取向量的尾部或头部来向任一方向移动向量,这一点很有用。在您的情况下,请在开头附加 NA,因为第一天没有“昨天”。

M$Prev.Close <- c(NA, head(M$SPY.Close, -1))

It is useful to remember that you can shift a vector either direction by taking its tail or head. In your case, append an NA at the start because the first day does not have a "yesterday".

M$Prev.Close <- c(NA, head(M$SPY.Close, -1))
伪心 2024-10-26 06:22:03

使用 which() 并将日期字符串与行名匹配。然后结果是数字,可以作为数字索引处理:

M <- as.xts(read.table(textConnection(" SPY.Close    mavg      dn.1      up.1 
2010-11-18    119.96 120.713 118.17955 119.99845   
2010-11-19    120.29 120.470 118.33112 120.09688   
2010-11-22    120.19 120.240 118.47911 120.18489   
2010-11-23    118.45 119.924 118.55112 120.20888   
2010-11-24    120.20 119.734 118.63565 120.27635   
") ) )
> M[which(rownames(M)=="2010-11-22"), "SPY.Close"]
[1] 120.19
> M[which(rownames(M)=="2010-11-22")-1, "SPY.Close"]
[1] 120.29

感谢 J. Winchester 指出,通过顺序应用 quantmod 中的 getSymbols 和 Cl 函数生成的 xts 对象具有空或 NULL 行名,但 time() 函数可以访问该信息并用作:
which(as.character(time(M))=="2010-11-22")

Use which() and match your date string to rownames. Then the result is numeric and can be handled as a numeric index:

M <- as.xts(read.table(textConnection(" SPY.Close    mavg      dn.1      up.1 
2010-11-18    119.96 120.713 118.17955 119.99845   
2010-11-19    120.29 120.470 118.33112 120.09688   
2010-11-22    120.19 120.240 118.47911 120.18489   
2010-11-23    118.45 119.924 118.55112 120.20888   
2010-11-24    120.20 119.734 118.63565 120.27635   
") ) )
> M[which(rownames(M)=="2010-11-22"), "SPY.Close"]
[1] 120.19
> M[which(rownames(M)=="2010-11-22")-1, "SPY.Close"]
[1] 120.29

Thanks to J. Winchester for pointing out that xts objects produced by the sequential application of getSymbols and Cl functions from quantmod have empty or NULL rownames but that the time() function can access that information and be used as:
which(as.character(time(M))=="2010-11-22")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文