From c812b9589dd386c3c1bfdbb1790ebf9db24dadf7 Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Wed, 13 Dec 2023 13:02:58 +0000 Subject: [PATCH] enhance oanda_quote() --- NEWS.md | 1 + R/oanda.R | 26 +++++++++++++------------- R/plot.R | 5 ++--- R/switch.R | 6 +++--- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index 167dd8fc..6eb451a3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # ichimoku 1.4.11.9000 (development) * Fixes `oanda_switch()`, regression since v1.4.11. +* Internal performance enhancements. # ichimoku 1.4.11 diff --git a/R/oanda.R b/R/oanda.R index b965b7b0..7fea35e3 100644 --- a/R/oanda.R +++ b/R/oanda.R @@ -892,10 +892,10 @@ oanda_view <- function(market = c("allfx", "bonds", "commodities", "fx", "metals market <- readline("Enter market [a]llfx [b]onds [c]ommodities [f]x [m]etals [s]tocks: ") market <- match.arg(market) price <- match.arg(price) - server <- if (missing(server)) do_$getServer() else match.arg(server, c("practice", "live")) - if (missing(apikey)) apikey <- do_$getKey(server = server) + server <- if (missing(server)) do_[["getServer"]]() else match.arg(server, c("practice", "live")) + if (missing(apikey)) apikey <- do_[["getKey"]](server = server) - ins <- do_$getInstruments(server = server, apikey = apikey) + ins <- do_[["getInstruments"]](server = server, apikey = apikey) sel <- switch(market, fx = { vec <- .subset2(ins, "name")[.subset2(ins, "type") == "CURRENCY"] @@ -966,14 +966,14 @@ oanda_quote <- function(instrument, price = c("M", "B", "A"), server, apikey) { if (missing(instrument) && interactive()) instrument <- readline("Enter instrument:") instrument <- sub("-", "_", toupper(force(instrument)), fixed = TRUE) price <- match.arg(price) - server <- if (missing(server)) do_$getServer() else match.arg(server, c("practice", "live")) - if (missing(apikey)) apikey <- do_$getKey(server = server) + server <- if (missing(server)) do_[["getServer"]]() else match.arg(server, c("practice", "live")) + if (missing(apikey)) apikey <- do_[["getKey"]](server = server) data <- getPrices(instrument = instrument, granularity = "D", count = 1, price = price, server = server, apikey = apikey, .validate = FALSE) - pctchg <- round(100 * (data[["c"]] / data[["o"]] - 1), digits = 4L) - cat(instrument, format.POSIXct(.Call(ichimoku_psxct, data[["t"]])), - "open:", data[["o"]], " high:", data[["h"]], " low:", data[["l"]], - " last:\u001b[7m", data[["c"]], "\u001b[27m %chg:", pctchg, price, file = stdout()) + pctchg <- 100 * (data[["c"]] / data[["o"]] - 1) + cat(sprintf("%s %s open: %.6g high: %.6g low: %.6g last:\u001b[7m %.6g \u001b[27m %%chg: %.4f %s", + instrument, format.POSIXct(.Call(ichimoku_psxct, data[["t"]])), + data[["o"]], data[["h"]], data[["l"]], data[["c"]], pctchg, price), file = stdout()) } @@ -1009,8 +1009,8 @@ oanda_positions <- function(instrument, time, server, apikey) { if (missing(instrument) && interactive()) instrument <- readline("Enter instrument:") instrument <- sub("-", "_", toupper(force(instrument)), fixed = TRUE) - server <- if (missing(server)) do_$getServer() else match.arg(server, c("practice", "live")) - if (missing(apikey)) apikey <- do_$getKey(server = server) + server <- if (missing(server)) do_[["getServer"]]() else match.arg(server, c("practice", "live")) + if (missing(apikey)) apikey <- do_[["getKey"]](server = server) url <- paste0("https://api-fx", switch(server, practice = "practice", live = "trade"), ".oanda.com/v3/instruments/", instrument, "/positionBook", @@ -1101,8 +1101,8 @@ oanda_orders <- function(instrument, time, server, apikey) { if (missing(instrument) && interactive()) instrument <- readline("Enter instrument:") instrument <- sub("-", "_", toupper(force(instrument)), fixed = TRUE) - server <- if (missing(server)) do_$getServer() else match.arg(server, c("practice", "live")) - if (missing(apikey)) apikey <- do_$getKey(server = server) + server <- if (missing(server)) do_[["getServer"]]() else match.arg(server, c("practice", "live")) + if (missing(apikey)) apikey <- do_[["getKey"]](server = server) url <- paste0("https://api-fx", switch(server, practice = "practice", live = "trade"), ".oanda.com/v3/instruments/", instrument, "/orderBook", diff --git a/R/plot.R b/R/plot.R index 8818bab6..f9a2f778 100644 --- a/R/plot.R +++ b/R/plot.R @@ -145,9 +145,8 @@ create_data <- function(object, window, type) { cd <- core[, "cd"] close <- core[, "close"] open <- core[, "open"] - object$osc_typ_slw <- 1 - 1 / - (1 + .Call(ichimoku_wmean, (cd == 1) * (close - open), p2) / - .Call(ichimoku_wmean, (cd == -1) * (open - close), p2)) + object$osc_typ_slw <- 1 - 1 / (1 + .Call(ichimoku_wmean, (cd == 1) * (close - open), p2) / + .Call(ichimoku_wmean, (cd == -1) * (open - close), p2)) } else if (type == "s") { core <- coredata.ichimoku(object) diff --git a/R/switch.R b/R/switch.R index ddc62313..2043d4fe 100644 --- a/R/switch.R +++ b/R/switch.R @@ -39,7 +39,7 @@ #' #' @export #' -oanda_switch <- function() do_$switchServer() +oanda_switch <- function() do_[["switchServer"]]() #' Deserialize JSON #' @@ -116,7 +116,7 @@ do_ <- function() { getAccount = function(server, apikey) { if (is.null(account)) { server <- if (missing(server)) server_type else match.arg(server, c("practice", "live")) - if (missing(apikey)) apikey <- do_$getKey(server = server) + if (missing(apikey)) apikey <- do_[["getKey"]](server = server) url <- switch(server, practice = "https://api-fxpractice.oanda.com/v3/accounts", live = "https://api-fxtrade.oanda.com/v3/accounts") @@ -134,7 +134,7 @@ do_ <- function() { getInstruments = function(server, apikey) { if (is.null(instruments)) { server <- if (missing(server)) server_type else match.arg(server, c("practice", "live")) - if (missing(apikey)) apikey <- do_$getKey(server = server) + if (missing(apikey)) apikey <- do_[["getKey"]](server = server) url <- paste0("https://api-fx", switch(server, practice = "practice", live = "trade"), ".oanda.com/v3/accounts/", do_$getAccount(server = server, apikey = apikey), "/instruments")