Skip to content

Commit

Permalink
enhance oanda_quote()
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Dec 13, 2023
1 parent 532d2cf commit c812b95
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 13 additions & 13 deletions R/oanda.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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())

}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions R/switch.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#'
#' @export
#'
oanda_switch <- function() do_$switchServer()
oanda_switch <- function() do_[["switchServer"]]()

#' Deserialize JSON
#'
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit c812b95

Please sign in to comment.