From 38cd6c8d9a373d8861c0d6f997d37c46272a5970 Mon Sep 17 00:00:00 2001 From: Juan Francisco Gomez Date: Wed, 3 Jul 2019 16:32:40 -0300 Subject: [PATCH 1/2] Agrego ordenes tipo GTD y Iceberg --- R/primary_api.R | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/R/primary_api.R b/R/primary_api.R index 4f3f8f2..c684b0b 100644 --- a/R/primary_api.R +++ b/R/primary_api.R @@ -260,12 +260,14 @@ trading_mdh <- function(market_id='ROFX', symbol, date, date_from, date_to) { #'\item Day. Day or session. #'\item IOC. Immediate or Cancel. #'\item FOK. Fill or Kill. -#'\item GTD. Good Till Date. (Not Available) +#'\item GTD. Good Till Date. #'} +#'@param iceberg Logical: if TRUE, then the order is 'iceberg'. FALSE as default. +#'@param expireDate String. Maturity date of the order, with format 'YYYYMMDD' +#'@param displayQty Numeric. Indicate the disclosed quantity for the 'iceberg' order. #'@param account String. Account Number / Account ID. -#' #'@return List with outputs like state of the order. -trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', time_in_force='Day', account) { +trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', time_in_force='Day', iceberg = FALSE, expireDate=NULL, displayQty=NULL, account) { if (!exists(".x_auth_token")) stop("You should first log in using trading_login()") market_id <- "ROFX" @@ -284,7 +286,12 @@ trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', if (!order_type %in% c("Limit", "MLL")) stop("Invalid 'order_type' parameter") if (!time_in_force %in% c("Day", "IOC", "FOK", "GTD")) stop("Invalid 'time_in_force' parameter") - if (time_in_force %in% c("GTD")) stop("Parameter 'time_in_force' not yet available.") + # if (time_in_force %in% c("GTD")) stop("Parameter 'time_in_force' not yet available.") + + if (time_in_force %in% c("GTD") & missing(expireDate)) stop("You should provide a maturity date") + + if (iceberg == "TRUE" & missing(displayQty)) stop("You should provide a disclosed quantity") + if (missing(account)) stop("You should pick a 'account' to move forward.") @@ -298,6 +305,9 @@ trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', price = price, ordType = if (order_type == "Limit") {"Limit"} else if (order_type == "MLL") {"Market_to_limit"}, timeInForce = time_in_force, + iceberg = iceberg, + expireDate = expireDate, + displayQty = displayQty, account = account ), add_headers(.headers = c("X-Auth-Token" = .x_auth_token))) From 7960a9b58479c6dda4bfef4734786ddc5fd1b42a Mon Sep 17 00:00:00 2001 From: Augusto Hassel Date: Wed, 3 Jul 2019 17:32:58 -0300 Subject: [PATCH 2/2] Fixes on validations --- R/helper_functions.R | 5 +++-- R/primary_api.R | 28 +++++++++++++++------------- man/trading_new_order.Rd | 11 +++++++++-- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/R/helper_functions.R b/R/helper_functions.R index 19071f9..46ee37b 100644 --- a/R/helper_functions.R +++ b/R/helper_functions.R @@ -1,4 +1,5 @@ # Helper Functions --------------------------- -.validate_fecha <- function(fecha) { - tryCatch({!is.na(format.Date(x = fecha, "%Y-%m-%d"))}, error = function(e) {FALSE}) +# If date invalid, returns FALSE +.validate_fecha <- function(date) { + tryCatch({!is.na(format.Date(x = date, "%Y-%m-%d"))}, error = function(e) {FALSE}) } diff --git a/R/primary_api.R b/R/primary_api.R index c684b0b..280581a 100644 --- a/R/primary_api.R +++ b/R/primary_api.R @@ -198,13 +198,11 @@ trading_mdh <- function(market_id='ROFX', symbol, date, date_from, date_to) { if (missing(symbol)) stop("You should pick a 'symbol' to move forward.") if (missing(date) & (missing(date_from) | missing(date_to))) stop("Invalid date parameters") - - if (!missing(date)) { - if (!.validate_fecha(fecha = date)) stop("The correct format for 'date' is %Y-%m-%d") + if (!.validate_fecha(date = date)) stop("The correct format for 'date' is %Y-%m-%d") } else { - if (!missing(date_from) & !.validate_fecha(fecha = date_from)) stop("The correct format for 'date_from' is %Y-%m-%d") - if (!missing(date_to) & !.validate_fecha(fecha = date_to)) stop("The correct format for 'date_to' is %Y-%m-%d") + if (!missing(date_from) & !.validate_fecha(date = date_from)) stop("The correct format for 'date_from' is %Y-%m-%d") + if (!missing(date_to) & !.validate_fecha(date = date_to)) stop("The correct format for 'date_to' is %Y-%m-%d") } # Base URL @@ -263,11 +261,11 @@ trading_mdh <- function(market_id='ROFX', symbol, date, date_from, date_to) { #'\item GTD. Good Till Date. #'} #'@param iceberg Logical: if TRUE, then the order is 'iceberg'. FALSE as default. -#'@param expireDate String. Maturity date of the order, with format 'YYYYMMDD' -#'@param displayQty Numeric. Indicate the disclosed quantity for the 'iceberg' order. +#'@param expire_date String. \strong{Only for GDT orders}. Maturity date of the order, With format '\%Y-\%m-\%d'. +#'@param display_quantity Numeric. \strong{Only for Iceberg orders}. Indicate the disclosed quantity for the 'iceberg' order. #'@param account String. Account Number / Account ID. #'@return List with outputs like state of the order. -trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', time_in_force='Day', iceberg = FALSE, expireDate=NULL, displayQty=NULL, account) { +trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', time_in_force='Day', iceberg=FALSE, expire_date=NULL, display_quantity=NULL, account) { if (!exists(".x_auth_token")) stop("You should first log in using trading_login()") market_id <- "ROFX" @@ -286,11 +284,15 @@ trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', if (!order_type %in% c("Limit", "MLL")) stop("Invalid 'order_type' parameter") if (!time_in_force %in% c("Day", "IOC", "FOK", "GTD")) stop("Invalid 'time_in_force' parameter") - # if (time_in_force %in% c("GTD")) stop("Parameter 'time_in_force' not yet available.") - if (time_in_force %in% c("GTD") & missing(expireDate)) stop("You should provide a maturity date") + if (time_in_force %in% c("GTD") & missing(expire_date)) stop("You should provide an 'expire_date' to move forward.") + if (!missing(expire_date) & !.validate_fecha(date = expire_date)) { + stop("The correct format for 'expire_date' is %Y-%m-%d") + } else if(!missing(expire_date) & .validate_fecha(date = expire_date)) { + expire_date <- gsub(pattern = "-", replacement = "", x = expire_date) + } - if (iceberg == "TRUE" & missing(displayQty)) stop("You should provide a disclosed quantity") + if (iceberg == "TRUE" & missing(display_quantity)) stop("You should provide a disclosed quantity") if (missing(account)) stop("You should pick a 'account' to move forward.") @@ -306,8 +308,8 @@ trading_new_order <- function(symbol, side, quantity, price, order_type='Limit', ordType = if (order_type == "Limit") {"Limit"} else if (order_type == "MLL") {"Market_to_limit"}, timeInForce = time_in_force, iceberg = iceberg, - expireDate = expireDate, - displayQty = displayQty, + expireDate = expire_date, + displayQty = if (iceberg == F) {NULL} else {display_quantity}, account = account ), add_headers(.headers = c("X-Auth-Token" = .x_auth_token))) diff --git a/man/trading_new_order.Rd b/man/trading_new_order.Rd index 783698a..7cf79f2 100644 --- a/man/trading_new_order.Rd +++ b/man/trading_new_order.Rd @@ -5,7 +5,8 @@ \title{Send Order to the Market} \usage{ trading_new_order(symbol, side, quantity, price, order_type = "Limit", - time_in_force = "Day", account) + time_in_force = "Day", iceberg = FALSE, expire_date = NULL, + display_quantity = NULL, account) } \arguments{ \item{symbol}{String. Use \code{\link{primary_instruments}} to see which symbols are available.} @@ -27,9 +28,15 @@ trading_new_order(symbol, side, quantity, price, order_type = "Limit", \item Day. Day or session. \item IOC. Immediate or Cancel. \item FOK. Fill or Kill. -\item GTD. Good Till Date. (Not Available) +\item GTD. Good Till Date. }} +\item{iceberg}{Logical: if TRUE, then the order is 'iceberg'. FALSE as default.} + +\item{expire_date}{String. \strong{Only for GDT orders}. Maturity date of the order, With format '\%Y-\%m-\%d'.} + +\item{display_quantity}{Numeric. \strong{Only for Iceberg orders}. Indicate the disclosed quantity for the 'iceberg' order.} + \item{account}{String. Account Number / Account ID.} } \value{