Closed mytarmail closed 8 months ago
Your example has almost 40 indicators. Can you please provide a minimal example with the indicators you think have an issue?
Your comment says that the SMA()
result is different based on the number of observations in the window. That's extremely unlikely. Also note that recursive indicators like EMA()
and indicators that use them (e.g. RSI()
) are unlikely to have the same values for different window sizes. See the Warning section in ?TTR::MovingAverages
.
Your example has almost 40 indicators. Can you please provide a minimal example with the indicators you think have an issue?
Your comment says that the
SMA()
result is different based on the number of observations in the window. That's extremely unlikely. Also note that recursive indicators likeEMA()
and indicators that use them (e.g.RSI()
) are unlikely to have the same values for different window sizes. See the Warning section in?TTR::MovingAverages
.
Hi! Here is a shorter example where all three indicators work differently on different windows
library(xts)
library(quantmod)
library(TTR)
n <- 100000
set.seed(1)
#######################################
######### make some prices ###########
#######################################
volume <- sample(1:10,n,replace = TRUE,prob = c(10:1))
prices <- round(cumsum(rnorm(n,sd = 0.01)),2)+1000
p <- cbind(price = prices, volume = volume) |>
xts(as.POSIXct("2000-01-01 00:00:00 EET")-n:1) |>
to.minutes(name = "my")
colnames(p) <- gsub("^my\\.", "", colnames(p))
############################################################
######### function for calculate all indicators ###########
############################################################
MINI_get_all_indicators <- function(p, n= 10){
CLOSE <- quantmod::Cl(p)
OHLC <- quantmod::OHLC(p)
HLC <- quantmod::HLC(p)
HL <- quantmod::HL(p)
VOLUME <- quantmod::Vo(p)
data.frame(
stoch = TTR::stoch(HLC = HLC, nFastK = n, nFastD = n, nSlowD = n*2),
TRIX = TTR::TRIX(price = CLOSE, n = n),
williamsAD = TTR::williamsAD(HLC = HLC)
)
}
############################################################
######### Making a comparison on different windows #########
############################################################
last_row_from_window500 <- tail(p,500) |> MINI_get_all_indicators(n = 10) |> tail(1)
last_row_from_window200 <- tail(p,200) |> MINI_get_all_indicators(n = 10) |> tail(1)
TwolastRows <- rbind.data.frame(last_row_from_window500,
last_row_from_window200)
#print(TwolastRows)
# Which indicators do not correspond to each other
colnames(last_row_from_window500)[ apply(TwolastRows,2, \(x) x[1]!=x[2]) ]
[1] "stoch.slowD" "TRIX.signal" "williamsAD"
Thanks! I just noticed that you're checking whether two numbers are exactly equal (x[1] != x[2]
). That's subject to floating point precision error (see FAQ 7.31). Use this instead:
num_diff <- function(x) { abs(diff(x)) > sqrt(.Machine$double.eps) }
(last_row_from_window500)[apply(TwolastRows, 2, num_diff)]
Once you do that, very few of the final indicator values are different. Here are the results using all 40-ish indicators:
print(diffs <- last_row_from_window500[apply(TwolastRows, 2, num_diff)])
## ADX.ADX chaikinAD obv SMI.SMI SMI.signal williamsAD
## 1999-12-31 23:59:59 35.06024 -4886.041 -2129 -55.81354 -49.64856 -1.43
TwolastRows[, colnames(diffs)]
## ADX.ADX chaikinAD obv SMI.SMI SMI.signal williamsAD
## 1999-12-31 23:59:59 35.06024 -4886.041 -2129 -55.81354 -49.64856 -1.43
## 1999-12-31 23:59:591 35.06024 -2925.638 -124 -55.81354 -49.64856 -0.55
So it looks like only chaikinAD()
, OBV()
, and williamsAD()
are different. All 3 of those functions use the cumulative sum of the series, so it makes sense that they would be different depending on the start of the series. That's not something I can fix.
ADX and SMI look close and are probably just a bit smaller than floating point precision error.
Thank you! You are amazing!!
Description
Hello! If I haven't made any mistake in my calculations. I found that the indicators show different values on windows of different sizes, this is not good because you cannot trust the results of previously trained models.
Expected behavior
It would be cool to sort this out since the results are misleading :)
Minimal, reproducible example
Session Info