tests/testthat/test-weight-ave-3roc.R

suppressPackageStartupMessages(require(quantmod))

context("Weighted averages 3ROC")

load_data <- function() {
  h <- new.env()
  load("testdata/history.RData",envir=h)
  return(get("history",envir=h))
}

test_that("default parameters work", {
  h <- load_data()
  XLE <- get("XLE",envir=h)

  rv <- xts::last(weight_ave_3ROC(XLE))
  expect_equal(as.numeric(Op(rv)), 6)
  expect_equal(as.numeric(Ad(rv)), 2.5)

  rv <- xts::last(weight_ave_3ROC_strict(XLE))
  expect_true( is.na( Op(rv)) )
  expect_equal(as.numeric(Hi(rv)), 3)
  expect_equal(as.numeric(Ad(rv)), 1.5)

  rv <- xts::last(weight_ave_3ROC_filter(XLE))
  expect_true( is.na( Op(rv)) )
  expect_equal(as.numeric(Hi(rv)), 4)
  expect_equal(as.numeric(Ad(rv)), 2.5)

})

test_that("modified parameters work", {
  h <- load_data()
  XLE <- get("XLE",envir=h)

  rv <- xts::last(weight_ave_3ROC_strict(XLE, n = c(2, 4, 6)))
  expect_true( is.na( Op(rv)) )
  expect_true( is.na( Hi(rv)) )
  expect_equal(as.numeric(Vo(rv)), 1)

  rv <- xts::last(weight_ave_3ROC_strict(XLE, weights = c(0.1,0.1,0.8)))
  expect_true( is.na(Op(rv) ))
  expect_equal(as.numeric(Hi(rv)), 3)
  expect_equal(as.numeric(Cl(rv)), 1.5)

  rv <- xts::last(weight_ave_3ROC_filter(XLE, n = c(2, 5, 6)))
  expect_true( is.na( Op(rv)) )
  expect_true( is.na( Hi(rv)) )
  expect_equal(as.numeric(Vo(rv)), 1)

  rv <- xts::last(weight_ave_3ROC_filter(XLE, weights = c(1 / 2, 1 / 4, 1 / 4)))
  expect_true( is.na( Op(rv)) )
  expect_equal(as.numeric(Hi(rv)), 4)
  expect_equal(as.numeric(Lo(rv)), 5)

})

test_that("invalid weights stop", {
  h <- load_data()
  XLE <- get("XLE",envir=h)

  expect_error(xts::last(weight_ave_3ROC_strict(XLE, weights = c(6, 4, 2))))
  expect_error(xts::last(weight_ave_3ROC_filter(XLE, weights = c(6, 4, 2))))
})

test_that("flipped parameters work", {
  h <- load_data()
  XLE <- get("XLE",envir=h)

  rv <- xts::last(weight_ave_3ROC(XLE, n = c(6, 4, 2)))
  expect_equal(as.numeric(Op(rv)), 6)
  expect_equal(as.numeric(Hi(rv)), 2)
  expect_equal(as.numeric(Lo(rv)), 5)
  expect_equal(as.numeric(Ad(rv)), 3.5)

  rv <- xts::last(weight_ave_3ROC_strict(XLE, n = c(6, 4, 2)))
  expect_true( is.na( Op(rv)) )
  expect_true( is.na( Hi(rv)) )
  expect_equal(as.numeric(Vo(rv)), 1)

  rv <- xts::last(weight_ave_3ROC_filter(XLE, n = c(6, 4, 2)))
  expect_true( is.na( Op(rv)) )
  expect_equal(as.numeric(Hi(rv)), 2)
  expect_equal(as.numeric(Cl(rv)), 3.5)

})

test_that("combined symbols work", {
  h <- load_data()
  XLE <- get("XLE",envir=h)
  XLK <- get("XLK",envir=h)

  cs <- merge(Ad(XLE), Ad(XLK))

  rv <- xts::last(weight_ave_3ROC(cs))
  colnames(rv) <- gsub(".Adjusted", "", colnames(rv))
  expect_equal(as.numeric(rv[1,"XLE"]),2)
  expect_equal(as.numeric(rv[1,"XLK"]),1)

  rv <- xts::last(weight_ave_3ROC_strict(cs))
  colnames(rv) <- gsub(".Adjusted", "", colnames(rv))
  expect_equal(as.numeric(rv[1,"XLE"]),2)
  expect_equal(as.numeric(rv[1,"XLK"]),1)

  rv <- xts::last(weight_ave_3ROC_filter(cs))
  colnames(rv) <- gsub(".Adjusted", "", colnames(rv))
  expect_equal(as.numeric(rv[1, "XLE"]), 2)
  expect_equal(as.numeric(rv[1, "XLK"]), 1)
})
greatgray/scorecard documentation built on Feb. 14, 2018, 4:28 a.m.