braverock / blotter

blotter provides transaction infrastructure for defining transactions, portfolios and accounts for trading systems and simulation. Provides portfolio support for multi-asset class and multi-currency portfolios. Actively maintained and developed.
112 stars 49 forks source link

txnsim - When setting 'maxlongpos' and 'maxshortpos' we need to make sure it exists #64

Closed jaymon0703 closed 6 years ago

jaymon0703 commented 6 years ago

Inside txnstruct() we currently use !is.null(maxlongpos) and !is.null(maxshortpos) to check whether maxlongpos or maxshortpos exists. When one of these variables does not exist, the function will error out as the argument is missing.

Instead using try() would work.

longcheck <- try(missing(maxlongpos), silent = TRUE)
shortcheck <- try(missing(maxshortpos), silent = TRUE)
if (class(longcheck) != "try-error") attr(txnsimdf,"maxlongpos") <- maxlongpos
if (class(shortcheck) != "try-error") attr(txnsimdf,"maxshortpos") <- maxshortpos

Note joran's reply to this SO question. Note also @gsee reply.