joshuaulrich / TTR

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

runMedian cumulative=T (wrong?) values #93

Closed stellathecat closed 3 years ago

stellathecat commented 4 years ago

Description

runMedian(..., cumulative = T) produces strange results which I am unable to trace.

Expected behavior

[Describe the behavior/output you expected]

test <- sample(1:1000/1000, 1000)
cbind('a'=TTR::runMedian(test, n = 10, cumulative = T),
      'b'=TTR::runMedian(test, n = 20, cumulative = T),
      'c'=PerformanceAnalytics::apply.fromstart(test, median, na.rm=T))

The three columns should be identical (after obs 20) - which they are in this example.

Minimal, reproducible example

test <- readRDS(url('https://www.dropbox.com/s/hi1qay3gr2gjxxl/runMedian.rds?dl=1'))
weird <- cbind('a'=TTR::runMedian(test, n = 20, cumulative = T),
               'b'=PerformanceAnalytics::apply.fromstart(test, median, na.rm=T))
tail(weird)

[1142,] 0.01474216 0.01577147
[1143,] 0.01577218 0.01577218
[1144,] 0.01478934 0.01581398
[1145,] **0.14649457** 0.01585579
[1146,] 0.01478934 0.01581398
[1147,] **0.13993371** 0.01577218

Same results when I use as.numeric(...) as input.

tail(TTR::runMedian(as.numeric(test), n = 20, cumulative = T))
[1] 0.01474216 0.01577218 0.01478934 **0.14649457** 0.01478934 **0.13993371**

tail(as.numeric(test))
[1] 0.02203701 0.02080171 0.01948616 **0.01778697** 0.01469783 **0.01363202**

Input series is declining, but runMedian is suddenly jumping up?

Session Info

R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=German_Switzerland.1252  LC_CTYPE=German_Switzerland.1252    LC_MONETARY=German_Switzerland.1252
[4] LC_NUMERIC=C                        LC_TIME=German_Switzerland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] xts_0.12-0 zoo_1.8-8 

loaded via a namespace (and not attached):
[1] compiler_3.6.2             tools_3.6.2                curl_4.3                   grid_3.6.2                
[6] TTR_0.23-6                 PerformanceAnalytics_1.5.3 lattice_0.20-38            quadprog_1.5-8  
joshuaulrich commented 4 years ago

Thanks for the report. This looks like a bug, and my hypothesis is that it's an artifact of the missing values at the beginning of your series.

For example, here's the plot of the two running median calculations, with the NA in the series.

weird <-
  cbind(test,
        ttr = TTR::runMedian(test, n = 20, cumulative = TRUE),
        perf = PerformanceAnalytics::apply.fromstart(test, median, na.rm = TRUE))
x <- xts(weird, index(test))
plot(x)

image

And here's the same plot with the leading NA removed.

test2 <- na.omit(test)
weird2 <-
  cbind(test2,
        ttr = TTR::runMedian(test2, n = 20, cumulative = TRUE),
        perf = PerformanceAnalytics::apply.fromstart(test2, median, na.rm = TRUE))
x2 <- xts(weird2, index(test2))
plot(x2)

image

I need to do some more digging to make sure this is the root cause, then I'll work on a fix. Thanks again for the report!

joshuaulrich commented 3 years ago

This is fixed in master now. I also fixed the same issue in runMAD(). Thanks again for the report!