From c0ac33a7325d163712c1b5dabccead4c36a9aa43 Mon Sep 17 00:00:00 2001 From: nmatare Date: Thu, 21 Dec 2017 22:20:40 -0500 Subject: [PATCH 1/2] clone.orderbook && strip.history did not __actually__ remove history AND it kept first order in orderbook; if order was open this could produce undesirable results --- R/paramsets.R | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/R/paramsets.R b/R/paramsets.R index 1c2de2c..168dbc7 100644 --- a/R/paramsets.R +++ b/R/paramsets.R @@ -72,15 +72,14 @@ clone.orderbook <- function(portfolio.st, cloned.portfolio.st, strip.history=TRU #must.have.args(match.call(), c('portfolio.st', 'cloned.portfolio.st')) orderbook <- getOrderBook(portfolio.st) - - i <- 1 # TODO: find index number by name - names(orderbook)[i] <- cloned.portfolio.st + names(orderbook) <- cloned.portfolio.st if(strip.history == TRUE) { - for(symbol in names(orderbook[[portfolio.st]])) - orderbook[[portfolio.st]][[symbol]] <- orderbook[[portfolio.st]][[symbol]][1,] - } + symbols <- names(orderbook[[cloned.portfolio.st]]) + for(symbol in symbols) + orderbook[[cloned.portfolio.st]][symbol] <- list(NULL) + } put.orderbook(cloned.portfolio.st, orderbook) } From e9f3a149fe6f9d1a5b41847b46c778cce4e65331 Mon Sep 17 00:00:00 2001 From: nmatare Date: Fri, 22 Dec 2017 20:32:00 -0500 Subject: [PATCH 2/2] clone portfolio would alter original portfolio because .getPortfolio was method used; thus any changes to the clone resulted in changes to the original --- R/paramsets.R | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/R/paramsets.R b/R/paramsets.R index 168dbc7..b6d9727 100644 --- a/R/paramsets.R +++ b/R/paramsets.R @@ -46,7 +46,20 @@ clone.portfolio <- function(portfolio.st, cloned.portfolio.st, strip.history=TRU { #must.have.args(match.call(), c('portfolio.st', 'cloned.portfolio.st')) - portfolio <- .getPortfolio(portfolio.st) + # must copy underlying environments IOT to actually clone + portfolio.list <- getPortfolio(portfolio.st) + if(is.environment(portfolio.list)) + return() # should never happen + else + portfolio <- list2env(portfolio.list) + portfolio$symbols <- list2env(lapply(portfolio$symbols, list2env)) + attributes(portfolio) <- attributes(portfolio.list)[-1] # names are no longer attributes + + # sanity checks + # all.equal(.getPortfolio(portfolio.st), portfolio) # TRUE but now no longer pointer + # identical(data.table::address(portfolio), data.table::address(.getPortfolio(portfolio.st))) # FALSE, new address + # put.portfolio('test', .getPortfolio(portfolio.st)) # make test portfolio + # identical(data.table::address(.getPortfolio('test')), data.table::address(.getPortfolio(portfolio.st))) # TRUE, point to same address if(strip.history==TRUE) {