Skip to content

Commit

Permalink
Fixes on validations
Browse files Browse the repository at this point in the history
  • Loading branch information
augustohassel committed Jul 3, 2019
1 parent 38cd6c8 commit 7960a9b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
5 changes: 3 additions & 2 deletions R/helper_functions.R
Original file line number Diff line number Diff line change
@@ -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})
}
28 changes: 15 additions & 13 deletions R/primary_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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.")
Expand All @@ -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)))
Expand Down
11 changes: 9 additions & 2 deletions man/trading_new_order.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7960a9b

Please sign in to comment.