我正在使用ttr包来生成股票指标。但是,指示灯函数将NA(如果适用 - 例如,CMO,SMA,CMF等)添加到系列的开头而不是结束。是否有一种方法可以将输出对准左侧,以便将NA值添加到系列的末尾,而不是开始?

例如:

library(TTR)    
x = 1:10
# TTR's simple moving average
SMA(x,n=2)
[1]  NA 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5
.

Zoo Package在末尾用NAS填充串联件:

library(zoo)
rollmean(x,2,na.pad=TRUE,align='left')
[1] 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5  NA
.

是否有一种方法可以在TTR中指定类似的东西,因为我需要生成超出移动平均值的指示符?我想我可以在这些函数周围创建包装器,并手动移动结果值,但不确定是否有更好的方法来做。

此外,由于TTR大量用于增加指标票价,我想知道为什么填充在一开始就是反对结束,特别是因为大多数历史价格按降序排序(截至日期)在上面的例子中,如果X [1]是今天股票的价格和X [10]价格10天前,如果今天不应该移动平均值(跨度= 2)今天的平均+昨天?尽管我想在最后添加NAS,但我还要确保我不误解这些指标的用途。

谢谢, -e

有帮助吗?

解决方案

I couldn't figure out an option in the function call to shift the series in a different direction. However, now I understand why TTR shifts the series downwards. Historical stock prices obtained via quantmod's getSymbols() returns them sorted by date in ascending order. When I manually download the quotes from Yahoo! or use ystockquote.py, the order is descending. I just re-sorted my data by date and used the TTR library as-is.

There were certain vectors that I wanted to be shifted up (padded with NAs) and I just used this code:

miss_len = length(x[is.na(x])
x = x[!is.na(x)]
length(x) = length(x) + miss_len
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top