#' @name instrument
#' @aliases instrument
#' @author Suberlin Sinaga @2021
#' @description {Asset manager.}
#' @title Instrument(s)/Asset(s) object generator
#' @return Object of type \code{\link{instrument}}
#' @inherit portfolio
#' @inherit account
#' @export
instrument <- R6Class(
"instrument",
inherit = account,
public = list(
#' @description Object initializer. This function will be automatically called when we initialize object.
#' @param owner: The name of the owner of the instrument
#' @return NA
initialize = function(owner) {
private$owner = owner
private$instruments = tibble(id = NA_integer_, account_id = NA_integer_,
portfolio = NA_character_, username = NA_character_,
platform = NA_character_, open_price = NA_real_,
value = NA_real_, order_id = NA_integer_,
close_price = NA_real_, profit_loss = NA_real_, pct_profit_loss = NA_real_,
status = NA_character_, type = NA_character_)
},
#' @description Adding instrument(s) as our asset once we buy them.
#' @aliases add_instrument
#' @param account_id: The account_id of the owner
#' @param portfolio_symbol: The symbol of portfolio (pair) that we are collected as asset(s)/instrument(s)
#' @param open_price: The price when we buy the asset(s)/instrument(s)
#' @param value: How many we buy the asset(s)/instrument(s)
#' @param order_id: The id registered when we buy the instrument(s)
#' @return Object of type \code{\link{instrument}}
add_instrument = function(account_id, portfolio_symbol,
open_price, value, order_id, user_name, platform) {
if (!paste0("account", account_id) %in% names(private)) {
stop(paste0("You do not have account: account", account_id, ". Create it first!"))
}
if (all(is.na(private$instruments[1,] %>% unlist()))) {
private$instruments[1, c("id", "account_id", "portfolio", "open_price", "value", "order_id", "username", "platform")] <-
list(1, account_id, portfolio_symbol, open_price, value, order_id, user_name, platform)
} else{
# TODO: do not run in parallel
id = max(private$instruments$id, na.rm = TRUE) + 1
private$instruments[1, c("id", "account_id", "portfolio", "open_price", "value", "order_id", "username", "platform")] <-
list(id, account_id, portfolio_symbol, open_price, value, order_id, user_name, platform)
}
return(self)
},
#' @description Method to print out our instrument class specifically
#' @return String to print information about the owner of the instruments
print = function() {
cat(paste0("<Instrument(s) Object Manager>\n\t owner: ",
private$owner, "\n"))
}
),
lock_objects = FALSE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.