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

Update stochastics.R #63

Open davft opened 6 years ago

davft commented 6 years ago

added try.xts(HLC) in the SMI function

joshuaulrich commented 6 years ago

Why? What was this supposed to address?

davft commented 6 years ago

hi Joshua, it does not solve a particular issue. anyway, in the stoch function there is try.xts, so if HLC is a DF with dates, the stoch's output will be an xts, whereas the SMI's output will be a DF.

Davide

On 4 April 2018 at 12:27, Joshua Ulrich notifications@github.com wrote:

Why? What problem did this solve?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/joshuaulrich/TTR/pull/63#issuecomment-378553993, or mute the thread https://github.com/notifications/unsubscribe-auth/AZT7MrGvLmhNsDKA7u5kM6Tyk8RHIzxdks5tlKALgaJpZM4TGe-p .

joshuaulrich commented 6 years ago

Thanks for explaining! I can't confirm your claim with a simple example:

library(quantmod)  # for HLC
data(ttrc)

x <- ttrc
rownames(x) <- x$Date
x$Date <- NULL
hlc <- HLC(x)

# both of these should return a data.frame
str(stoch(hlc))  # error
str(SMI(hlc))    # matrix

The output of both functions should be the same class as the input. But that is not the case for both... so it looks like you've uncovered a few issues. I would appreciate if you would take a look, but I can investigate if you don't. I would also greatly appreciate if you could add tests that catch these two cases (and any others you may uncover). Thanks!

davft commented 6 years ago

the error in str(stoch(hlc)) is due to the function runMax and runMin: they do not return an xts if the input is an xts deriving from a data.frame, even if there is the reclass function at the end. try this:

DF <- ttrc rownames(DF) <- DF$Date DF <- DF['Close'] XTS <- try.xts(DF, error = as.matrix) str(XTS) str(runMax(DF)) # data.frame, even if it should be converted to an xts str(runMax(XTS)) # data.frame, even if it is an xts

DT <- as.data.table(ttrc) DT <- DT[, .(Date, Close)] XTS2 <- try.xts(DT, error = as.matrix) str(XTS2) str(runMax(DT)) # xts str(runMax(XTS2)) # xts

this is probably due to the fact that XTS has this attribute:

Original class: 'data.frame'

as you can see from the code below, stoch applied to a DF returns an error, while it returns an xts when it is applied to a DT. on the other hand, the function SMI doesn't know how to deal with a DF/DT with Date, and it will always return a matrix (or an error if the column Date of the DT is supplied).

library(data.table) data(ttrc)

DF <- ttrc rownames(DF) <- DF$Date DF$Date <- NULL DF$Open <- NULL DF$Volume <- NULL

str(DF)

DF is a data.frame containing cols High|Low|Close

str(stoch(DF)) # error str(SMI(DF)) # matrix

DT <- as.data.table(ttrc) DT[, c('Open', 'Volume') := NULL]

str(DT)

DT is a data.table containing cols Date|High|Low|Close

str(stoch(DT)) # xts str(stoch(DT[, c('High', 'Low', 'Close')])) # matrix str(SMI(DT)) # error str(SMI(DT[, c('High', 'Low', 'Close')])) # matrix

I hope I was able to clarify my point.

davide

On 4 April 2018 at 13:08, Joshua Ulrich notifications@github.com wrote:

Thanks for explaining! I can't confirm your claim with a simple example:

library(quantmod) # for HLC data(ttrc) x <- ttrc rownames(x) <- x$Datex$Date <- NULLhlc <- HLC(x)

both of these should return a data.frame

str(stoch(hlc)) # error str(SMI(hlc)) # matrix

The output of both functions should be the same class as the input. But that is not the case for both... so it looks like you've uncovered a few issues. I would appreciate if you would take a look, but I can investigate if you don't. I would also greatly appreciate if you could add tests that catch these two cases (and any others you may uncover). Thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/joshuaulrich/TTR/pull/63#issuecomment-378563481, or mute the thread https://github.com/notifications/unsubscribe-auth/AZT7MoZpMl5CDIXTyk5Q0tXBB31A7cSOks5tlKmQgaJpZM4TGe-p .