Code Monkey home page Code Monkey logo

Comments (4)

joshuaulrich avatar joshuaulrich commented on August 23, 2024 1

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.

from ttr.

mytarmail avatar mytarmail commented on August 23, 2024 1

Thank you! You are amazing!!

from ttr.

joshuaulrich avatar joshuaulrich commented on August 23, 2024

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.

from ttr.

mytarmail avatar mytarmail commented on August 23, 2024

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.

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" 

from ttr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.