joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
326 stars 102 forks source link

stoch() NA/NaN/Inf error when no equity value change over time #52

Closed cjuncosa closed 3 years ago

cjuncosa commented 6 years ago

Running stoch() throws an error if the equity doesn't change in price coming from the inclusion of Inf in the calculation.

equity<-getSymbols("QTWN")
stochastic<-stoch(QTWN[,2:4],nFastK = 10,nFastD = 3)
Error in runSum(x, n) : NA/NaN/Inf in foreign function call (arg 1)
traceback()
5: runSum(x, n)
4: runMean(x, n)
3: SMA(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.0123579821717995, 
   -0.474019764105834, -0.474019764105834, -0.474019764105834, -0.474019764105834, 
   -0.474019764105834, -0.474019764105834, -0.474019764105834, -0.474019764105834, 
   -0.474019764105834, -Inf, -Inf, -Inf, -Inf, -1.956, -1.956, -1.956, 
   -1.956, -1.956, -1.956, -1.956, -1.956, -1.956, -Inf, -Inf, -0.982666666666669, 
   -0.982666666666669, -0.982666666666669, -0.982666666666669, -0.982666666666669, 
   -0.982666666666669, -0.982666666666669, -0.982666666666669, -0.982666666666669, 
   -0.375290313225058, -0.375290313225058, -0.375290313225058, -0.375290313225058, 
   -0.387309488177193, -0.199608454382243, -0.158781208233093, -0.158781208233093, 
   -0.158781208233093, -0.202523184452779, -0.28688746679546, -0.28688746679546, 
   -0.28688746679546, -0.28688746679546, -0.761539102564105, -Inf, 
   -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, 
   -Inf, -Inf, -0.0582093831450938, -0.0582093831450938, -0.0582093831450938, 
   -0.0582093831450938, 0.534271952575028, 0.613656449165402, 0.613656449165402, 
   0.613656449165402, 0.629747752971875, 0.444299390774587, 0.444299390774587, 
   0.444299390774587, 0.444299390774587, -0.702666666666668, 0.254946862634259, 
   0.254946862634259, 0.254946862634259, 0.540458867161159, 0.523601483479433, 
   0.250505731625085, 0.328051247471341, 0.328051247471341, 0.554164965426406, 
   0.219145905420991, -0.500703234880453, 0.549902768834514, 0.23564916918429, 
   0.23564916918429, 0.23564916918429, 0.211976846307386, 0.205314814814815, 
   0.205314814814815, 0.205314814814815, 0.205314814814815, 0.178185678601168, 
   -13.3958125000005, -13.3958125000005, -13.3958125000005, -0.724590163934427, 
   -0.724590163934427, -0.724590163934427, -0.724590163934427, -0.691802732240438, 
   -0.413576793452097, -0.413576793452097, -0.413576793452097, -0.168031295137218, 
   -0.034605932203391, -0.00635663841807757, -0.00635663841807757, 
   -0.783191384180796, -0.825564971751417, -0.472457627118648, -0.516363636363637, 
   -0.516363636363637, -0.516363636363637, -0.516363636363637, -0.540334855403349, 
   -0.540334855403349, -1.11459968602826, -1.11459968602826, -Inf, 
   -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, 
   -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, 
   -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, 
   -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, 
   -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, -Inf, 
   0.755725190839694, 0.755725190839694, 0.755725190839694, 0.774961554710605, 
   0.718631178707225, 0.718631178707225, 0.718631178707225, 0.718631178707225, 
   0.718631178707225, -0.81967213114754, -1.11652542372882, -1.11652542372882, 
   -2.60649819494588, -20.0555555555568, -Inf, -0.564468020304564, 
   -0.564468020304564, -1.27512791878172, -0.871065375302667, -0.871065375302667, 
   -0.871065375302667, -0.871065375302667, -0.871065375302667, -0.871065375302667, 
   -0.871065375302667, -0.871065375302667, -1.52436440677967, -7.37948717948744, 
   -Inf, -Inf, -Inf, -0.632762312633836, -0.632762312633836, -0.632762312633836, 
   -1.52337398373983, -1.52337398373983, -1.52337398373983, -1.52337398373983, 
   -1.52337398373983, -1.52337398373983, 0.516198044009779, 0.516198044009779, 
   0.256418398533008, 0.256418398533008, 0.256418398533008, 0.256418398533008, 
   0.256418398533008, 0.256418398533008, 0.256418398533008, -1.79013646788991, 
   -1.79013646788991, -Inf, -Inf, -Inf, -Inf, -Inf, -2.02763289473682, 
   -2.02763289473682, -2.02763289473682, -0.00317121116043121, -0.00317121116043121, 
   -0.00317121116043121, -0.00317121116043121, -0.00317121116043121, 
   -0.00317121116043121, -0.387444514901709, -0.387444514901709, 
   -0.387444514901709, -2.46202531645567, -2.46202531645567, -0.816613268608415, 
   -0.816613268608415, -0.327724919093851, -0.32793905070119, 4.63392028487137e-07, 
   5.43478259497414e-07, 5.43478259497414e-07, 0.119103335318642
   ), n = 3)
2: do.call(maType, c(list(fastK), list(n = nFastD, ...)))
1: stoch(QTWN[, 2:4], nFastK = 10, nFastD = 3)

TTR version 0.23-2

DeanLeeFumu commented 6 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 ?

joshuaulrich commented 3 years ago

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().