R Under development (unstable) (2024-08-23 r87049 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ################################################################################ > ## > ## $Id: portfolio.expose.test.R 406 2007-04-19 16:30:22Z enos $ > ## > ## Tests the expose method of class portfolio. > ## > ################################################################################ > > library(portfolio) Loading required package: grid Loading required package: lattice > > load("portfolio.expose.test.RData") > > ## save(test.portfolio, test.trades, empty.trades, bad.1, bad.2, bad.3, bad.4, truth, file = "portfolio.expose.test.RData", compress = TRUE) > > result.1 <- expose(test.portfolio, test.trades) > result.1 <- calcWeights(result.1) > > result.2 <- expose(test.portfolio, empty.trades) > result.2 <- calcWeights(result.2) > > stopifnot( + all.equal(result.1, truth), + all.equal(result.2, test.portfolio) + ) Warning messages: 1: In validityMethod(as(object, superClass)) : The securities with the following id's have weights but no data in the data slot: l 2: In validityMethod(as(object, superClass)) : The securities with the following id's have weights but no data in the data slot: kThe securities with the following id's have weights but no data in the data slot: l > > > ## Try to sell when you are shorting > > trial.1 <- try( + expose(test.portfolio, bad.1), silent = TRUE + ) > > stopifnot( + inherits(trial.1, "try-error"), + as.logical(grep("Illegal trades found", trial.1[1])) + ) > > ## Try to short when you are already longing > > trial.2 <- try( + expose(test.portfolio, bad.2), silent = TRUE + ) > > stopifnot( + inherits(trial.2, "try-error"), + as.logical(grep("Illegal trades found", trial.2[1])) + ) > > ## Try to cover more than you are shorting > > trial.3 <- try( + expose(test.portfolio, bad.3), silent = TRUE + ) > > stopifnot( + inherits(trial.3, "try-error"), + as.logical(grep("Illegal trades found", trial.3[1])) + ) > > ## Try a side change, from long to short, without selling all your holdings > > trial.4 <- try( + expose(test.portfolio, bad.4), silent = TRUE + ) > > stopifnot( + inherits(trial.4, "try-error"), + as.logical(grep("Illegal trades found", trial.4[1])) + ) > > proc.time() user system elapsed 0.73 0.07 0.79