Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix_has_ohlcv_305 #426

Merged
merged 9 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions R/OHLC.transformations.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ function(x)
`Op` <-
function(x)
{
if(has.Op(x))
return(x[,grep('Open',colnames(x),ignore.case=TRUE)])
stop('subscript out of bounds: no column name containing "Open"')
loc <- has.Op(x,which = TRUE)
if ((length(loc) == 1) && (is.numeric(loc)))
return(x[,loc])
stop('subscript out of bounds: no or multiple column name containing "Open"')
}

`has.Op` <-
Expand All @@ -219,7 +220,8 @@ function(x,which=FALSE)
if(!is.null(colAttr))
return(if(which) colAttr else TRUE)

loc <- grep('Open',colnames(x),ignore.case=TRUE)
loc <- grep('\\bOpen\\b',colnames(x),ignore.case=TRUE)
if (length(loc) > 1) loc <- grep('\\.Open$',colnames(x),ignore.case=TRUE)
if(!identical(loc,integer(0))) {
return(if(which) loc else TRUE)
} else FALSE
Expand All @@ -228,9 +230,10 @@ function(x,which=FALSE)
`Hi` <-
function(x)
{
if(has.Hi(x))
return(x[,grep('High',colnames(x),ignore.case=TRUE)])
stop('subscript out of bounds: no column name containing "High"')
loc <- has.Hi(x,which = TRUE)
if ((length(loc) == 1) && (is.numeric(loc)))
return(x[,loc])
stop('subscript out of bounds: no or multiple column name containing "High"')
}

`has.Hi` <-
Expand All @@ -240,7 +243,8 @@ function(x,which=FALSE)
if(!is.null(colAttr))
return(if(which) colAttr else TRUE)

loc <- grep('High',colnames(x),ignore.case=TRUE)
loc <- grep('\\bHigh\\b',colnames(x),ignore.case=TRUE)
if (length(loc) > 1) loc <- grep('\\.High$',colnames(x),ignore.case=TRUE)
if(!identical(loc,integer(0))) {
return(if(which) loc else TRUE)
} else FALSE
Expand All @@ -249,9 +253,10 @@ function(x,which=FALSE)
`Lo` <-
function(x)
{
if(has.Lo(x))
return(x[,grep('Low',colnames(x),ignore.case=TRUE)])
stop('subscript out of bounds: no column name containing "Low"')
loc <- has.Lo(x,which = TRUE)
if ((length(loc) == 1) && (is.numeric(loc)))
return(x[,loc])
stop('subscript out of bounds: no or multiple column name containing "Low"')
}

`has.Lo` <-
Expand All @@ -261,7 +266,8 @@ function(x,which=FALSE)
if(!is.null(colAttr))
return(if(which) colAttr else TRUE)

loc <- grep('Low',colnames(x),ignore.case=TRUE)
loc <- grep('\\bLow\\b',colnames(x),ignore.case=TRUE)
if (length(loc) > 1) loc <- grep('\\.Low$',colnames(x),ignore.case=TRUE)
if(!identical(loc,integer(0))) {
return(if(which) loc else TRUE)
} else FALSE
Expand All @@ -270,9 +276,10 @@ function(x,which=FALSE)
`Cl` <-
function(x)
{
if(has.Cl(x))
return(x[,grep('Close',colnames(x),ignore.case=TRUE)])
stop('subscript out of bounds: no column name containing "Close"')
loc <- has.Cl(x,which = TRUE)
if ((length(loc) == 1) && (is.numeric(loc)))
return(x[,loc])
stop('subscript out of bounds: no or multiple column name containing "Close"')
}
`has.Cl` <-
function(x,which=FALSE)
Expand All @@ -281,7 +288,8 @@ function(x,which=FALSE)
if(!is.null(colAttr))
return(if(which) colAttr else TRUE)

loc <- grep('Close',colnames(x),ignore.case=TRUE)
loc <- grep('\\bClose\\b',colnames(x),ignore.case=TRUE)
if (length(loc) > 1) loc <- grep('\\.Close$',colnames(x),ignore.case=TRUE)
if(!identical(loc,integer(0))) {
return(if(which) loc else TRUE)
} else FALSE
Expand All @@ -290,11 +298,10 @@ function(x,which=FALSE)
`Vo` <-
function(x)
{
#vo <- grep('Volume',colnames(x))
#if(!identical(vo,integer(0)))
if(has.Vo(x))
return(x[,grep('Volume',colnames(x),ignore.case=TRUE)])
stop('subscript out of bounds: no column name containing "Volume"')
loc <- has.Vo(x,which = TRUE)
if ((length(loc) == 1) && (is.numeric(loc)))
return(x[,loc])
stop('subscript out of bounds: no or multiple column name containing "Volume"')
}
`has.Vo` <-
function(x,which=FALSE)
Expand All @@ -303,7 +310,8 @@ function(x,which=FALSE)
if(!is.null(colAttr))
return(if(which) colAttr else TRUE)

loc <- grep('Volume',colnames(x),ignore.case=TRUE)
loc <- grep('\\bVolume\\b',colnames(x),ignore.case=TRUE)
if (length(loc) > 1) loc <- grep('\\.Volume\\b',colnames(x),ignore.case=TRUE)
if(!identical(loc,integer(0))) {
return(if(which) loc else TRUE)
} else FALSE
Expand All @@ -312,9 +320,10 @@ function(x,which=FALSE)
`Ad` <-
function(x)
{
if(has.Ad(x))
return(x[,grep('Adjusted',colnames(x),ignore.case=TRUE)])
stop('subscript out of bounds: no column name containing "Adjusted"')
loc <- has.Ad(x,which = TRUE)
if ((length(loc) == 1) && (is.numeric(loc)))
return(x[,loc])
stop('subscript out of bounds: no or multiple column name containing "Adjusted"')
}
`has.Ad` <-
function(x,which=FALSE)
Expand All @@ -323,7 +332,8 @@ function(x,which=FALSE)
if(!is.null(colAttr))
return(if(which) colAttr else TRUE)

loc <- grep('Adjusted',colnames(x),ignore.case=TRUE)
loc <- grep('\\bAdjusted\\b',colnames(x),ignore.case=TRUE)
if (length(loc) > 1) loc <- grep('\\.Adjusted\\b',colnames(x),ignore.case=TRUE)
if(!identical(loc,integer(0))) {
return(if(which) loc else TRUE)
} else FALSE
Expand Down
94 changes: 94 additions & 0 deletions tests/test_has.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
library(quantmod)
library(tinytest)

data(sample_matrix, package = "xts")
stock <- as.xts(sample_matrix)
stock$Volume <- stock$Close
stock$Adjusted <- stock$Close
simple.colnames <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")

# basic functionality
colnames(stock) <- paste("MSFT", simple.colnames, sep = ".")
expect_true(has.Op(stock))
expect_true(has.Hi(stock))
expect_true(has.Lo(stock))
expect_true(has.Cl(stock))
expect_true(has.Vo(stock))
expect_true(has.Ad(stock))
expect_true(is.HLC(stock))
expect_true(all(has.HLC(stock)))
expect_true(is.OHLC(stock))
expect_true(all(has.OHLC(stock)))
expect_true(is.OHLCV(stock))
expect_true(all(has.OHLCV(stock)))

# Test which for has/OHLC functions.
expect_equal(has.Op(stock, which = TRUE), 1)
expect_equal(has.Hi(stock, which = TRUE), 2)
expect_equal(has.Lo(stock, which = TRUE), 3)
expect_equal(has.Cl(stock, which = TRUE), 4)
expect_equal(has.Vo(stock, which = TRUE), 5)
expect_equal(has.Ad(stock, which = TRUE), 6)
expect_equal(has.HLC(stock, which = TRUE), c(2,3,4))
expect_equal(has.OHLC(stock, which = TRUE), c(1,2,3,4))
expect_equal(has.OHLCV(stock, which = TRUE), c(1,2,3,4,5))

#has.OHLC will test underlying has.Op, has.Cl, etc. It will NOT test has.Ad
colnames(stock) <- simple.colnames
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("OPEN", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("HIGH", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("LOW", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("CLOSE", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("VOLUME", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("ADJUSTED", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("ILOW", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("LOW.W", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("LOW_A", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("^LOW", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("My.LOW", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

colnames(stock) <- paste("VLOWY", simple.colnames, sep = ".")
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))

# low in colname returned by function TTR::stoch()
colnames(stock) <- paste("LOW", simple.colnames, sep = ".")
stock$slowD <- stock[,4]
expect_true(all(has.OHLCV(stock)))
expect_equal(has.OHLCV(stock, which = T), c(1,2,3,4,5))
stock$slowD <- NULL