xts操作在烛台中返回空值::CSPDoji()

时间:2021-01-08 14:57:38

I'm trying to use the candlesticks package, but I get errors in some functions. For example, CSPDoji. Inside this function, Hi(TS) - BodyHi returns NULL. Both variables are xts, and the length and index are the same. Here is the detail:

我正在尝试使用烛台包,但在某些函数中会出现错误。例如,CSPDoji。在这个函数中,Hi(TS) - BodyHi返回NULL。两个变量都是xts,长度和索引是相同的。这是细节:

library(candlesticks)
getSymbols("YHOO",src="google",from="2013-01-01")
head(YHOO)
debug(CSPDoji)
CSPDoji(YHOO)

When I step through the function CSPdoji, I got the following:

当我通过函数CSPdoji的时候,我得到了以下信息:

Browse[2]> n
debug: if (!is.OHLC(TS)) {
    stop("Price series must contain Open, High, Low and Close.")
}
Browse[2]>
debug: NULL
Browse[2]>
debug: BL <- abs(Cl(TS) - Op(TS))
Browse[2]>
debug: CL <- Hi(TS) - Lo(TS)
Browse[2]>
debug: BodyHi <- as.xts(apply(cbind(Op(TS), Cl(TS)), 1, max))
Browse[2]>
debug: BodyLo <- as.xts(apply(cbind(Op(TS), Cl(TS)), 1, min))
Browse[2]>
debug: Doji <- reclass(BL < CL * maxbodyCL, TS)
Browse[2]>
debug: DFDoji <- reclass(Doji & (Hi(TS) - BodyHi <= CL * maxshadowCL), TS)  
Browse[2]>
debug: GSDoji <- reclass(Doji & (BodyLo - Lo(TS) <= CL * maxshadowCL), TS)  
Browse[2]> DFDoji
     [,1]
Browse[2]> class(Hi(TS))
[1] "xts" "zoo"
Browse[2]> class(BodyHi)
[1] "xts" "zoo"
Browse[2]> Hi(TS) - BodyHi
Data:
numeric(0)

Index:
numeric(0)
Browse[2]> head(Hi(TS))
           YHOO.High
2013-01-02     20.32
2013-01-03     20.10
2013-01-04     19.95
2013-01-07     19.58
2013-01-08     19.68
2013-01-09     19.75
Browse[2]> head(BodyHi)
            [,1]
2013-01-02 20.20
2013-01-03 20.05
2013-01-04 19.86
2013-01-07 19.56
2013-01-08 19.66
2013-01-09 19.73
Browse[2]>  length(Hi(TS))
[1] 177
Browse[2]> length(BodyHi)
[1] 177

The result of Hi(TS) - BodyHi should be another xts. But it is NULL here. Am I missing anything?

Hi(TS) - BodyHi的结果应该是另一个xts。但它是空的。我错过什么吗?

1 个解决方案

#1


4  

I am the maintainer of this package. The problem is caused by a timezone issue when the timezone of the R session is not set to UTC. It can be solved with the command

我是这个软件包的维护者。当R会话的时区没有设置为UTC时,就会出现时区问题。它可以通过命令来解决。

Sys.setenv(TZ="UTC")

Sys.setenv(TZ = UTC)

at the start of the session. This is documented in the package-description (?candlesticks)

在会议开始时。这在包描述(?烛台)中有记录

This is what causes the problem:

这就是问题的原因:

R --vanilla

Sys.setenv(TZ="CET")
require(candlesticks)
TS <- getSymbols("YHOO", src="google", auto.assign=FALSE)

#calculate the highest price of the candle's body
BodyHi <- as.xts(apply(cbind(Op(TS), Cl(TS)), 1, max))

# compare these two xts-objects
head(TS, n=2)
           YHOO.Open YHOO.High YHOO.Low YHOO.Close YHOO.Volume
2007-01-03     25.85     26.26    25.26      25.61    26654067
2007-01-04     25.64     26.92    25.52      26.85    32565729

head(BodyHi, n=2)
            [,1]
2007-01-03 25.85
2007-01-04 26.85

# looks good, but:
head(cbind(TS, BodyHi), n=4)
           YHOO.Open YHOO.High YHOO.Low YHOO.Close YHOO.Volume   ..2
2007-01-02        NA        NA       NA         NA          NA 25.85
2007-01-03     25.85     26.26    25.26      25.61    26654067    NA
2007-01-03        NA        NA       NA         NA          NA 26.85
2007-01-04     25.64     26.92    25.52      26.85    32565729    NA

# because:
head(index(TS), n=2)
[1] "2007-01-03" "2007-01-04"
head(index(BodyHi), n=2)
[1] "2007-01-03 CET" "2007-01-04 CET"

That's the reason the CSPDoji function fails. I haven't found a more elegant solution to that yet.

这就是CSPDoji函数失败的原因。我还没有找到更好的解决方案。

#1


4  

I am the maintainer of this package. The problem is caused by a timezone issue when the timezone of the R session is not set to UTC. It can be solved with the command

我是这个软件包的维护者。当R会话的时区没有设置为UTC时,就会出现时区问题。它可以通过命令来解决。

Sys.setenv(TZ="UTC")

Sys.setenv(TZ = UTC)

at the start of the session. This is documented in the package-description (?candlesticks)

在会议开始时。这在包描述(?烛台)中有记录

This is what causes the problem:

这就是问题的原因:

R --vanilla

Sys.setenv(TZ="CET")
require(candlesticks)
TS <- getSymbols("YHOO", src="google", auto.assign=FALSE)

#calculate the highest price of the candle's body
BodyHi <- as.xts(apply(cbind(Op(TS), Cl(TS)), 1, max))

# compare these two xts-objects
head(TS, n=2)
           YHOO.Open YHOO.High YHOO.Low YHOO.Close YHOO.Volume
2007-01-03     25.85     26.26    25.26      25.61    26654067
2007-01-04     25.64     26.92    25.52      26.85    32565729

head(BodyHi, n=2)
            [,1]
2007-01-03 25.85
2007-01-04 26.85

# looks good, but:
head(cbind(TS, BodyHi), n=4)
           YHOO.Open YHOO.High YHOO.Low YHOO.Close YHOO.Volume   ..2
2007-01-02        NA        NA       NA         NA          NA 25.85
2007-01-03     25.85     26.26    25.26      25.61    26654067    NA
2007-01-03        NA        NA       NA         NA          NA 26.85
2007-01-04     25.64     26.92    25.52      26.85    32565729    NA

# because:
head(index(TS), n=2)
[1] "2007-01-03" "2007-01-04"
head(index(BodyHi), n=2)
[1] "2007-01-03 CET" "2007-01-04 CET"

That's the reason the CSPDoji function fails. I haven't found a more elegant solution to that yet.

这就是CSPDoji函数失败的原因。我还没有找到更好的解决方案。