Closed cjuncosa closed 3 years ago
Hi, I've got the same issue here.
Turned out the problem is in this line:
numMA <- do.call(maType, c(list(num), list(n = smooth)))
The output contains strange float errors ex: expected 0 but -0.000000000000003552714 expected 0.2 but 0.199999999999995736744
And the code:
fastK <- numMA/denMA
That's why there are Infinities in fastK
Since the Inf is in the vector, the code below is not able to correct it:
fastK[is.nan(fastK)] <- 0.5
Can it be fixed ?
The only way I can reproduce this is if the Close is > High, and High and Low are equal. But that would be a data error. Regardless, this can be fixed in stoch()
by changing is.nan()
to !is.finite()
.
Here's a reproducible example:
x <- structure(c(53.99, 54.69, 55.55, 55.55, 55.55, 52.5, 52.5, 52.5, 52.5,
52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 51.77, 51.77, 53.99,
54.69, 55.55, 55.55, 55.55, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5, 52.5,
52.5, 52.5, 52.5, 52.5, 52.5, 51.77, 51.77, 54.09, 54.79, 55.65, 55.65,
55.65, 52.6, 52.6, 52.6, 52.6, 52.6, 52.6, 52.6, 52.6, 52.6, 52.6, 52.6,
52.6, 52.6, 51.87, 51.87), class = c("xts", "zoo"), src = "yahoo",
updated = structure(1597593022.65377, class = c("POSIXct", "POSIXt")),
index = structure(c(1446422400, 1446508800, 1446595200, 1446681600,
1446768000, 1447027200, 1447113600, 1447200000, 1447286400, 1447372800,
1447632000, 1447718400, 1447804800, 1447891200, 1447977600, 1448236800,
1448323200, 1448409600, 1448582400, 1448841600), tzone = "UTC", tclass = "Date"),
.Dim = c(20L, 3L), .Dimnames = list(NULL, c("QTWN.High", "QTWN.Low", "QTWN.Close")))
s <- TTR::stoch(x, 10, 3)
The error is different because the original error was from a .Fortran()
call and the current code use .Call()
.
Running stoch() throws an error if the equity doesn't change in price coming from the inclusion of Inf in the calculation.
TTR version 0.23-2