GetBars <- function(GetBarMethod, symbol, barsType = "Bid", periodicity = "M1", startTime, endTime, count) {
tempStartTime <- as.double(startTime)*1000
history <- data.table("Volume"= numeric(),
"Close" = numeric(), "Low"= numeric(),"High"= numeric(), "Open"= numeric(),
"Timestamp"= numeric())
maxCount <- 1000
if(count == 0) {
repeat{
bars <- Throttling(GetBarMethod, symbol, barsType, periodicity, tempStartTime, maxCount)
lastHistoryNoteTimestamp <- history[.N, Timestamp]
excludeIndex <-ifelse(length(lastHistoryNoteTimestamp) <= 0, numeric(0), bars[Timestamp==lastHistoryNoteTimestamp,which=T])
if(!is.na(excludeIndex))
bars <-bars[-excludeIndex]
if(nrow(bars) == 0) {
break;
}else{
endTimeInMs <- as.double(endTime) * 1000
if(bars[.N, Timestamp] >= endTimeInMs){
history = rbind(history, bars[Timestamp <= endTimeInMs])
break;
}
}
history <- rbind(history, bars)
tempStartTime <- history[.N, Timestamp]
}
}else{
if(abs(count) < maxCount){
history <- Throttling(GetBarMethod, symbol, barsType, periodicity, tempStartTime, count)
}else{
history <- Throttling(GetBarMethod, symbol, barsType, periodicity, tempStartTime, maxCount * sign(count))
}
}
history[, Timestamp := as.POSIXct(Timestamp / 1000, origin = "1970-01-01", tz = "GMT")]
setkey(history, Timestamp)
setcolorder(history, c("Timestamp", "Open", "Low", "High", "Close", "Volume"))
return(history)
}
GetTicks <- function(GetTickMethod, symbol, startTime, endTime, count) {
tempStartTime <- as.double(startTime)*1000
history <- data.table("Timestamp" = numeric(),
"BidPrice" = numeric() , "BidVolume" = numeric(),
"BidType" = character(), "AskPrice" = numeric(),
"AskVolume" = numeric(), "AskType" = character())
maxCount <- 1000
if(count == 0) {
repeat{
ticks <- Throttling(GetTickMethod, symbol, tempStartTime, maxCount)
lastHistoryNoteTimestamp <- history[.N, Timestamp]
excludeIndex <-ifelse(length(lastHistoryNoteTimestamp) <= 0, numeric(0), ticks[Timestamp==lastHistoryNoteTimestamp, which=TRUE])
if(!is.na(excludeIndex))
ticks <-ticks[-excludeIndex]
if(nrow(ticks) == 0) {
break;
}else{
endTimeInMs <- as.double(endTime) * 1000
if(ticks[.N, Timestamp] >= endTimeInMs){
history = rbind(history, ticks[Timestamp <= endTimeInMs])
break;
}
}
history <- rbind(history, ticks)
tempStartTime <- history[.N, Timestamp]
}
}else{
if(abs(count) < maxCount){
history <- Throttling(GetTickMethod, symbol, tempStartTime, count)
}else{
history <- Throttling(GetTickMethod, symbol, tempStartTime, maxCount * sign(count))
}
}
history[, Timestamp := as.POSIXct(Timestamp / 1000, origin = "1970-01-01", tz = "GMT")]
setkey(history, Timestamp)
return(history)
}
Throttling <- function(func, ..., N = 10, sleepTimeSec = 1, errorPattern = "429"){
res <- NULL
func <- match.fun(func)
for(i in 1:N){
res <- tryCatch({
return(func(...))
# return(stop(errorPattern))
}, error = function(e){
print(paste( i, "-", e$message))
if(!grepl(errorPattern, e$message, fixed = TRUE))
stop(e$message)
print(paste("Sleeping", sleepTimeSec, "sec"))
Sys.sleep(sleepTimeSec)
})
}
if(length(res) == 0)
stop(paste("More than", N, "attempts with errors matchs the pattern", errorPattern))
return(res)
}
# GetBidAskBar <- function(GetBarMethod, symbol, periodicity = "M1", startTime, endTime, count) {
# bidBars <- GetBars(GetBarMethod, symbol, barsType = "Bid", periodicity, startTime, endTime, count)
# askBars <- GetBars(GetBarMethod, symbol, barsType = "Ask", periodicity, startTime, endTime, count)
# bidAskBars <- merge(bidBars, askBars)
# colnames(bidAskBars) <- c("Timestamp", "BidVolume", "BidClose", "BidLow", "BidHigh", "BidOpen",
# "AskVolume", "AskClose", "AskLow", "AskHigh", "AskOpen")
# return(bidAskBars)
# }
# Get current time in ms
getTimestamp = function() {
return(round(as.double(Sys.time()) * 1000))
}
# Generate HMAC Headers from
getHMACHeaders = function(url, id, key, secret, method = "GET", body = "") {
timestamp1 <- getTimestamp()
signature <- paste0(timestamp1, id, key, method, url, body)
hash_value <- base64enc::base64encode(hmac(secret, signature, algo = "sha256", raw = TRUE))
auth_value <- paste("HMAC",paste(id, key, timestamp1, hash_value, sep = ":"))
return(auth_value)
}
#RTTWebClient Class
#@name RTTWebClient
#@field web_api_address. Server address. Character
#@field web_api_port. Port. Integer
#@field web_api_id. Web Api Id. Character
#@field web_api_key. Web Api Key. Character
#@field web_api_secrer. Web Api Secret. Character
#' @import data.table
#' @import jsonlite
#' @import httr
#' @import digest
#' @import base64enc
RTTWebClient <- setRefClass("RTTWebClient",
fields = list(web_api_address = "character",
web_api_port = "integer",
web_api_id = "character",
web_api_key = "character",
web_api_secret= "character")
)
#Get All Dividend
#@name GetDividendsRawMethod
#@return a data.table with dividends.
RTTWebClient$methods(
GetDividendsRawMethod = function() {
#"Get All Dividend"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/dividend")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/dividend")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
dividends <- fromJSON(data)
dividends <- as.data.table(dividends)
return(dividends)
}
)
# #' Get All Current Quotes
# #' @name GetCurrentQuotesRawMethod
# #' @return a data.table with current quotes
RTTWebClient$methods(
GetCurrentQuotesRawMethod = function() {
"Get All Current Quotes"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/tick")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/tick")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
ticks <- fromJSON(data)
ticks <- data.table("Timestamp" = ticks$Timestamp, "Symbol" = ticks$Symbol, "BidPrice" = ticks$BestBid$Price,
"BidVolume" = ticks$BestBid$Volume, "BidType" = ticks$BestBid$Type, "AskPrice" = ticks$BestAsk$Price,
"AskVolume" = ticks$BestAsk$Volume, "AskType" = ticks$BestAsk$Type)
return(ticks)
}
)
# #' Get All Current Quotes
# #' @name GetPipsValueRawMethod
# #' @return a data.table with current quotes
RTTWebClient$methods(
GetPipsValueRawMethod = function(targetCurrency, symbols) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/pipsvalue")
url_rel <- paste0(url_rel,"?","targetCurrency=",targetCurrency,"&symbols=", symbols)
# url_abs <- utils::URLencode(paste0(address, url_rel), reserved = FALSE)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/pipsvalue")
url_rel <- paste0(url_rel,"?","targetCurrency=",targetCurrency,"&symbols=", symbols)
# url_abs <- utils::URLencode(paste0(address, url_rel), reserved = FALSE)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
pipsValue <- as.data.table(fromJSON(data))
return(pipsValue)
}
)
# #' Get All Symbols
# #' @name GetSymbolsInfoRawMethod
# #' @return data.table with symbol info
RTTWebClient$methods(
GetSymbolsInfoRawMethod = function(){
"Get All Symbols"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/symbol")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/symbol")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
data = fromJSON(data)
symbols <- as.data.table(data)
setkey(symbols, "Symbol")
return(symbols)
}
)
# #' Get All Currency Info
# #' @name GetCurrencyInfoRawMethod
# #' @return data.table with currency info
RTTWebClient$methods(
GetCurrencyInfoRawMethod = function(){
"Get All Symbols"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/currency")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/currency")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
data = fromJSON(data)
currency <- as.data.table(data)
setkey(currency, "Name")
return(currency)
}
)
# #' Get Bar History
# #' @name GetBarRawMethod
# #' @param symbol a character. Symbol Name.
# #' @param barsType. a character. Bars Type. One from c("Ask", "Bid").
# #' @param periodicity. a character. Periodicity. From c("S1", "S10", "M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1","MN1")
# #' @param startTimeMs. Long numeric. Timestamp from 1970-01-01 in ms.
# #' @param count. Integer. Count of returned Bars from startTimeMs. Max is 1000. Can be negative.
# #' @return data.table with Bar Info
RTTWebClient$methods(
GetBarRawMethod = function(symbol, barsType, periodicity, startTimeMs, count){
"Get Bar History"
# nonScienceFormat <- options(scipen = 999)
# on.exit(options(nonScienceFormat))
withr::local_options(list(scipen = 999))
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/quotehistory",symbol, periodicity, "bars", barsType, sep= "/")
url_rel <- paste0(url_rel,"?","timestamp=",round(startTimeMs, 0),"&count=", count)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/quotehistory",symbol, periodicity, "bars", barsType, sep= "/")
url_rel <- paste0(url_rel,"?","timestamp=",round(startTimeMs, 0),"&count=", count)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
bars <- fromJSON(data)
if(length(bars$Bars) < 1)
return(data.table("Volume"= numeric(),
"Close" = numeric(), "Low"= numeric(),"High"= numeric(), "Open"= numeric(),
"Timestamp"= numeric()))
return(as.data.table(bars$Bars))
}
)
# #'Get Ticks History
# #' @name GetTicksRawMethod
# #' @param symbol. A character. Symbol Name.
# #' @param startTimeMs. Long numeric. Timestamp from 1970-01-01 in ms.
# #' @param count. Integer. Count of returned Bars from startTimeMs. Max is 1000. Can be negative.
# #' @return data.table with Ticks Info.
RTTWebClient$methods(
GetTicksRawMethod = function(symbol, startTimeMs, count){
"Get Ticks History"
# nonScienceFormat <- options(scipen = 999)
# on.exit(options(nonScienceFormat))
withr::local_options(list(scipen = 999))
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$.self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/quotehistory",symbol, "ticks", sep= "/")
url_rel <- paste0(url_rel,"?","timestamp=",round(startTimeMs, 0),"&count=", count)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/quotehistory",symbol, "ticks", sep= "/")
url_rel <- paste0(url_rel,"?","timestamp=",round(startTimeMs, 0),"&count=", count)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
ticks <- fromJSON(data)
ticks <- ticks$Ticks
if(length(ticks) < 1)
return(data.table("Timestamp" = numeric(),
"BidPrice" = numeric() , "BidVolume" = numeric(),
"BidType" = character(), "AskPrice" = numeric(),
"AskVolume" = numeric(), "AskType" = character()))
return(data.table("Timestamp" = ticks$Timestamp, "BidPrice" = ticks$BestBid$Price,
"BidVolume" = ticks$BestBid$Volume, "BidType" = ticks$BestBid$Type, "AskPrice" = ticks$BestAsk$Price,
"AskVolume" = ticks$BestAsk$Volume, "AskType" = ticks$BestAsk$Type))
}
)
# #' Get Split Info
# #' @name GetSplitRawMethods
# #' @return a data.table with split info
# #'
RTTWebClient$methods(
GetSplitRawMethods = function(symbolFilter = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/split")
if(!is.null(symbolFilter))
url_rel <- paste0(url_rel,"/", "bysymbol","/", symbolFilter)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
url_rel <- paste("/api/v2/public/split")
if(!is.null(symbolFilter))
url_rel <- paste0(url_rel,"/", "bysymbol","/", symbolFilter)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
split <- as.data.table(fromJSON(data))
return(split)
}
)
# #' Get trade Info
# #' @name GetAccountTradeRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetAccountTradeRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/trade")
if(!is.null(id))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
return(setDT(trade))
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetAccountRawMethods = function() {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/account")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
trade["Throttling"] <- NULL
return(setDT(trade))
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerAccountsRawMethods = function() {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/account")
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
# trade["Throttling"] <- NULL
return(as.data.table(trade))
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerAccountDetailsRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/account/details")
if(!is.null(id)){
url_rel <- paste0(url_rel,"/",id)
}else{
stop(paste("AccountId required"))
}
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
# trade["Throttling"] <- NULL
return(as.data.table(trade))
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerAccountLoginRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/account/login")
if(is.null(id))
stop(paste("AccountId required"))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
# trade["Throttling"] <- NULL
return(as.data.table(trade))
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerAccountWebTokenRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/account/webapitoken")
if(is.null(id))
stop(paste("AccountId required"))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
# trade["Throttling"] <- NULL
return(as.data.table(trade))
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerGrossTradeRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/gross/trade")
if(is.null(id))
stop(paste("AccountId required"))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- as.data.table(fromJSON(data))
return(trade)
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerGrossAccTradesRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/gross/trades")
if(is.null(id))
stop(paste("AccountId required"))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- as.data.table(fromJSON(data))
return(trade)
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerNetTradeByIdRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/net/trade")
if(is.null(id))
stop(paste("TradeId required"))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
return(as.data.table(trade$NetPosition))
}
)
# #' Get Account Info
# #' @name GetAccountRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerNetAccTradesRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/net/trades")
if(is.null(id))
stop(paste("AccountId required"))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- as.data.table(fromJSON(data))
return(trade)
}
)
# #' Get Account Info
# #' @name GetManagerAccountSnapshotsRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerAccountSnapshotsRawMethods = function(loginVector, from, to, pagingDirection = as.character(NA), pagingSize = 100, pagingReportId = as.character(NA), groupFilter = character(0)) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/accountsnapshot")
url_abs <- paste0(address, url_rel)
body <- jsonlite::toJSON(list(
TimestampFrom = from,
TimestampTo = to,
Accounts = as.list(loginVector),
Groups = as.list(groupFilter),
PagingDirection = pagingDirection,
PagingSize = pagingSize,
PagingReportId = pagingReportId
), auto_unbox = TRUE, pretty = TRUE)
connect <- httr::POST(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE), body = body,
httr::add_headers(Authorization = getHMACHeaders(url_abs,
id = .self$web_api_id,
key = .self$web_api_key,
secret = .self$web_api_secret,
method = "POST",
body = body
)
), content_type_json())
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
dataList <- fromJSON(data)
reports <- as.data.table(dataList$Reports)
lastReportId <- dataList$LastReportId
isEOS <- dataList$IsEndOfStream
return(list(reports, lastReportId, isEOS))
}
)
# #' Get Account Info
# #' @name GetManagerAccountSnapshotsRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetManagerTradeHistoryRawMethods = function(loginVector, from, to, pagingDirection = as.character(NA), pagingSize = 1000, pagingReportId = as.character(NA), orderId = as.character(NA), skipCancelOrder = TRUE) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/manager/tradehistory")
url_abs <- paste0(address, url_rel)
body <- jsonlite::toJSON(list(
TimestampFrom = from,
TimestampTo = to,
Accounts = as.list(loginVector),
OrderId = orderId,
SkipCancelOrder = skipCancelOrder,
PagingDirection = pagingDirection,
PagingSize = pagingSize,
PagingReportId = pagingReportId
), auto_unbox = TRUE, pretty = TRUE)
connect <- httr::POST(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE), body = body,
httr::add_headers(Authorization = getHMACHeaders(url_abs,
id = .self$web_api_id,
key = .self$web_api_key,
secret = .self$web_api_secret,
method = "POST",
body = body
)
), content_type_json())
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
dataList <- fromJSON(data)
reports <- as.data.table(dataList$Reports)
lastReportId <- dataList$LastReportId
isEOS <- dataList$IsEndOfStream
return(list(reports, lastReportId, isEOS))
}
)
# #' Get trade Info
# #' @name GetAssetRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetAssetRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/asset")
if(!is.null(id))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
return(setDT(trade))
}
)
# #' Get position Info
# #' @name GetPositionRawMethods
# #' @return a data.table with trade Info
# #'
RTTWebClient$methods(
GetPositionRawMethods = function(id = NULL) {
"Get Pip Value"
address <- .self$web_api_address
if(!grepl("^https://", address))
address <- paste0("https://", address)
portPattern <- paste0(":", .self$web_api_port, "$")
if(!grepl(portPattern, address))
address <- paste0(address, ":", .self$web_api_port)
if(length(.self$web_api_id) != 0 && length(.self$web_api_key) != 0 && length(.self$web_api_secret) != 0){
url_rel <- paste("/api/v2/position")
if(!is.null(id))
url_rel <- paste0(url_rel,"/",id)
url_abs <- paste0(address, url_rel)
connect <- httr::GET(url_abs, httr::config(ssl_verifypeer = 0L, ssl_verifyhost = 0L, verbose = FALSE),
httr::add_headers(Authorization = getHMACHeaders(url_abs, .self$web_api_id, .self$web_api_key, .self$web_api_secret)))
}else{
stop(paste("Only private connection can be used. Enter HMAC Id, Key..."))
}
data <- httr::content(connect, "text", encoding = "UTF-8")
if(connect$status_code != 200) {
stop(paste("status_code is not OK", connect$status_code, as.character(data)))
}
# data <- content(connect, "text", encoding = "UTF-8")
trade <- fromJSON(data)
return(setDT(trade))
}
)
# #' Init Public Web Client Obj
# #'@name InitPublicWebClient
# #'@param server a character. Web Address.
# #'@param port an integer. Port Number. Default is 443
# #'@return rTTWebClient obj.
InitPublicWebClient <- function(server = "ttlivewebapi.fxopen.net", port=443L) {
return(RTTWebClient(web_api_address=server,
web_api_port = port))
}
# #' Init Private Web Client Obj
# #'@name InitPrivateWebClient
# #'@param server a character. Web Address.
# #'@param port an integer. Port Number. Default is 443
# #'@param id a character. HMAC client id.
# #'@param key a character. HMAC client key.
# #'@param secret a character. HMAC secret key.
# #'@return rTTWebClient obj.
InitPrivateWebClient <- function(server = "ttlivewebapi.fxopen.net", port=443L, id = "", key = "", secret = "") {
return(RTTWebClient(web_api_address=server,
web_api_port = port,
web_api_id = id,
web_api_key=key,
web_api_secret=secret))
}
#' RTTWebApiHost
#' @name RTTWebApiHost
#' @field client. RTTWebClient obj.
RTTWebApiHost <- setRefClass("RTTWebApiHost",
fields = list(
client = "RTTWebClient"
),
methods = list(
initialize = function(newWebClient){
.self$client <- newWebClient
}
))
#' Get All Dividend
#' @name GetDividends
#' @return a data.table with dividends.
RTTWebApiHost$methods(
GetDividends = function()
{
"Get All Dividends"
# return(.self$client$GetDividendsRawMethod())
dividends <- Throttling(.self$client$GetDividendsRawMethod)
dividends[, Time := as.POSIXct(Time / 1000, origin = "1970-01-01", tz = "GMT")]
return(dividends)
}
)
#' Get All Symbols
#' @name GetSymbolsInfo
#' @param getPipsValue a bool. Add PipsValue Info for symbols or not (targetCurrency is USD).
#' @param getLastPrice a bool. Add LastPrice Info for symbols or not.
#' @return data.table with symbol info
RTTWebApiHost$methods(
GetSymbolsInfo = function(getPipsValue = FALSE, getLastPrice = TRUE) {
"Get All Symbols"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
symbols <- Throttling(.self$client$GetSymbolsInfoRawMethod)
# symbols[!grepl("_L$", Symbol), PipsValue := tryCatch(.self$GetPipsValue("USD", Symbol)[,(Value)], error = function(e) {print(e); as.numeric(NA)})]
if(getPipsValue){
notLastSymbols <- symbols[!grepl("_L$", Symbol), Symbol]
pipsValue <- tryCatch(connection$GetPipsValue("USD", notLastSymbols), error = function(e) {print(e); NULL})
if(!is.null(pipsValue)){
symbols[pipsValue, on = .(Symbol), PipsValue := i.Value]
}else{
symbols[, PipsValue := as.numeric(NA)]
}
}
if(getLastPrice){
currentQuotes <- .self$GetCurrentQuotes()
symbols[currentQuotes, on = .(Symbol), c("LastTimeUpdate", "LastBidPrice", "LastBidVolume", "LastAskPrice", "LastAskVolume") := list(i.Timestamp, i.BidPrice, i.BidVolume, i.AskPrice, i.AskVolume)]
}
return(symbols)
}
)
#' Get All Currency
#' @name GetCurrencyInfo
#' @return data.table with currency info
RTTWebApiHost$methods(
GetCurrencyInfo = function() {
"Get All Currencies"
currency <- Throttling(.self$client$GetCurrencyInfoRawMethod)
return(currency)
}
)
#' Get All Current Quotes
#' @name GetCurrentQuotes
#' @return a data.table with current quotes
RTTWebApiHost$methods(
GetCurrentQuotes = function() {
"Get All Current Quotes"
# currentQuotes <- .self$client$GetCurrentQuotesRawMethod()
currentQuotes <- Throttling(.self$client$GetCurrentQuotesRawMethod)
currentQuotes[, Timestamp := as.POSIXct(Timestamp / 1000, origin = "1970-01-01", tz = "GMT")]
return(currentQuotes)
}
)
#' Get Pips Value
#' @name GetPipsValue
#' @param targetCurrency a character. Currency Name.
#' @param symbols a character vectors. Symbols vector.
#' @return a data.table with pips value in targetCurrency for every symbol in symbols vector
RTTWebApiHost$methods(
GetPipsValue = function(targetCurrency, symbols) {
"Get Pips Value"
symbols <- paste(sapply(symbols, URLencode, reserved = TRUE, USE.NAMES = FALSE), collapse = URLencode(" ", reserved = FALSE))
# pipsValue <- .self$client$GetPipsValueRawMethod(targetCurrency, symbols)
pipsValue <- Throttling(.self$client$GetPipsValueRawMethod, targetCurrency, symbols)
setcolorder(pipsValue, c(2,1))
setkey(pipsValue, "Symbol")
return(pipsValue)
}
)
#' Get Bar History
#' @name GetBarsHistory
#' @param symbol a character. Symbol Name.
#' @param barsType. a character. Bars Type. One from c("Ask", "Bid").
#' @param periodicity. a character. Periodicity. From c("S1", "S10", "M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1","MN1")
#' @param startTime a POSIXct obj. Start Time in UTC.
#' @param endTime a POSIXct obj. End Time in UTC.
#' @param count. Integer. Count of returned Bars from startTime. Max is 1000. Can be negative. If count == 0, use time interval between startTime and endTime.
#' @return data.table with Bar Info
RTTWebApiHost$methods(
GetBarsHistory = function(symbol, barsType = "Bid", periodicity = "M1", startTime, endTime = as.POSIXct(Sys.Date(), tz = "GMT"), count = 0L) {
"Get Bar History"
symbol <- sapply(symbol, URLencode, reserved = TRUE, USE.NAMES = FALSE)
if(barsType == "Bid" || barsType == "Ask"){
return(GetBars(.self$client$GetBarRawMethod, symbol, barsType, periodicity, startTime, endTime, count))
}
if(barsType == "BidAsk"){
bid <- GetBars(.self$client$GetBarRawMethod, symbol, "Bid", periodicity, startTime, endTime, count)
ask <- GetBars(.self$client$GetBarRawMethod, symbol, "Ask", periodicity, startTime, endTime, count)
res <- merge(bid, ask, by = "Timestamp", all = TRUE)
setnames(res, seq_along(res), c("Timestamp", "BidOpen", "BidLow", "BidHigh", "BidClose", "BidVolume",
"AskOpen", "AskLow", "AskHigh", "AskClose", "AskVolume"))
setnafill(res, type=c("locf"), cols=seq_along(res))
setnafill(res, type=c("nocb"), cols=seq_along(res))
return(res)
}
stop("Wrong Bar Type")
}
)
#'Get Ticks History
#' @name GetTickHistory
#' @param symbol. A character. Symbol Name.
#' @param startTime a POSIXct obj. Start Time in UTC.
#' @param endTime a POSIXct obj. End Time in UTC.
#' @param count. Integer. Count of returned Ticks from startTime. Max is 1000. Can be negative. If count == 0, use time interval between startTime and endTime.
#' @return data.table with Ticks Info.
RTTWebApiHost$methods(
GetTickHistory = function(symbol, startTime, endTime = as.POSIXct(Sys.Date(), tz = "GMT"), count = 0L) {
"Get Bar History"
symbol <- sapply(symbol, URLencode, reserved = TRUE, USE.NAMES = FALSE)
return(GetTicks(.self$client$GetTicksRawMethod, symbol, startTime, endTime, count))
}
)
#' Get Asset Info
#' @name GetAssetsInfo
#' @param id a bool. Assets id (optional)
#' @return data.table with asset info
RTTWebApiHost$methods(
GetAssetsInfo = function(id = NULL) {
"Get Asset Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
assets <- Throttling(.self$client$GetAssetRawMethods, id)
return(assets)
}
)
#' Get Asset Info
#' @name GetTradeInfo
#' @param id a bool. Assets id (optional)
#' @return data.table with asset info
RTTWebApiHost$methods(
GetTradeInfo = function(id = NULL) {
"Get Trade Info"
trade <- Throttling(.self$client$GetAccountTradeRawMethods, id)
return(trade)
}
)
#' Get Position Info
#' @name GetAssetsInfo
#' @param id a bool. Assets id (optional)
#' @return data.table with asset info
RTTWebApiHost$methods(
GetPositionInfo = function(id = NULL) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
position <- Throttling(.self$client$GetPositionRawMethods, id)
return(position)
}
)
#' Get Account Info
#' @name GetAccountInfo
#' @return data.table with asset info
RTTWebApiHost$methods(
GetAccountInfo = function() {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetAccountRawMethods)
return(account)
}
)
#' Get Account Info
#' @name GetAllAccountsInfo
#' @return data.table with asset info
RTTWebApiHost$methods(
GetAllAccountsInfo = function() {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerAccountsRawMethods)
return(account)
}
)
#' Get Account Info
#' @name GetAccountDetails
#' @return data.table with asset info
RTTWebApiHost$methods(
GetAccountDetails = function(id) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerAccountDetailsRawMethods, id)
return(account)
}
)
#' Get Account Info
#' @name GetAccountLogin
#' @return data.table with asset info
RTTWebApiHost$methods(
GetAccountLogin = function(id) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerAccountLoginRawMethods, id)
return(account)
}
)
#' Get Account Info
#' @name GetAccountWebToken
#' @return data.table with asset info
RTTWebApiHost$methods(
GetAccountWebToken = function(id) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerAccountWebTokenRawMethods, id)
return(account)
}
)
#' Get Account Info
#' @name GetGrossTradesByTradeId
#' @return data.table with asset info
RTTWebApiHost$methods(
GetGrossTradesByTradeId = function(id) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerGrossTradeRawMethods, id)
return(account)
}
)
#' Get Account Info
#' @name GetGrossTradesByAccountId
#' @return data.table with asset info
RTTWebApiHost$methods(
GetGrossTradesByAccountId = function(id) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerGrossAccTradesRawMethods, id)
return(account)
}
)
#' Get Account Info
#' @name GetNetTradesByTradeId
#' @return data.table with asset info
RTTWebApiHost$methods(
GetNetTradesByTradeId = function(id) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerNetTradeByIdRawMethods, id)
return(account)
}
)
#' Get Account Info
#' @name GetNetTradesByAccountId
#' @return data.table with asset info
RTTWebApiHost$methods(
GetNetTradesByAccountId = function(id) {
"Get Position Info"
# symbols <- .self$client$GetSymbolsInfoRawMethod()
account <- Throttling(.self$client$GetManagerNetAccTradesRawMethods, id)
return(account)
}
)
#' Get Splits Info
#' @name GetSplits
#' @return data.table with asset info
RTTWebApiHost$methods(
GetSplits = function(symbolFilter = NULL) {
"Get Split Info"
res <- Throttling(.self$client$GetSplitRawMethods, symbolFilter)
res[, StartTime := as.POSIXct(round(StartTime / 1000), tz = "UTC", origin = "1970-01-01")]
return(res)
}
)
#' Get Account Snapshots Info
#' @name GetManagerAccountSnapshots
#' @return data.table with asset info
RTTWebApiHost$methods(
GetManagerAccountSnapshots = function(loginVector, from, to, pagingDirection = as.character(NA), pagingSize = 100, groupFilter = character(0)) {
"Get Account Snapshots Info"
isEOS <- FALSE
fromInMs <- round(as.double(from) * 1000)
toInMs <- round(as.double(to) * 1000)
lastReportId <- as.character(NA)
currentIndex <- 0
max_batch <- 1000
list_dt <- vector("list", max_batch)
while(!isEOS) {
print(address(list_dt))
batch <- Throttling(.self$client$GetManagerAccountSnapshotsRawMethods, loginVector, fromInMs, toInMs, pagingSize = pagingSize, pagingReportId = lastReportId, groupFilter = groupFilter)
list_dt[[currentIndex + 1]] <- batch[[1]]
currentIndex <- currentIndex + 1
if(currentIndex > round(length(list_dt) / 2)){
print("Extend memory")
list_dt <- c(list_dt, rep(list(NULL), length(list_dt)))
}
lastReportId <- batch[[2]]
isEOS <- batch[[3]]
print(lastReportId)
print(isEOS)
}
res <- rbindlist(list_dt)
res[, Time := as.POSIXct(Timestamp / 1000, tz = "GMT", origin = "1970-01-01")]
return(res)
}
)
#' Get Account Snapshots Info
#' @name GetManagerTradeHistory
#' @return data.table with asset info
RTTWebApiHost$methods(
GetManagerTradeHistory = function(loginVector, from, to, pagingDirection = as.character(NA), pagingSize = 100, orderId = as.character(NA), skipCancelOrder = TRUE) {
"Get TradeHistoryReport Info"
isEOS <- FALSE
fromInMs <- round(as.double(from) * 1000)
toInMs <- round(as.double(to) * 1000)
lastReportId <- as.character(NA)
currentIndex <- 0
max_batch <- 1000
list_dt <- vector("list", max_batch)
while(!isEOS) {
print(address(list_dt))
batch <- Throttling(.self$client$GetManagerTradeHistoryRawMethods, loginVector, fromInMs, toInMs, pagingSize = pagingSize, pagingReportId = lastReportId, orderId = orderId, skipCancelOrder = skipCancelOrder)
list_dt[[currentIndex + 1]] <- batch[[1]]
currentIndex <- currentIndex + 1
if(currentIndex > round(length(list_dt) / 2)){
print("Extend memory")
list_dt <- c(list_dt, rep(list(NULL), length(list_dt)))
}
lastReportId <- batch[[2]]
isEOS <- batch[[3]]
print(lastReportId)
print(isEOS)
}
res <- rbindlist(list_dt)
res[, TrTime := as.POSIXct(TrTime / 1000, tz = "GMT", origin = "1970-01-01")][, PosClosed := as.POSIXct(PosClosed / 1000, tz = "GMT", origin = "1970-01-01")]
return(res)
}
)
#' Init RTTWebApiHost
#'@param server a character. Web Address.
#'@param port an integer. Port Number. Default is 443
#'@param id a character. HMAC client id.
#'@param key a character. HMAC client key.
#'@param secret a character. HMAC secret key.
#'@return RTTWebApiHost ref class.
#'@importFrom methods new
#'@importFrom withr local_options
#'@export
InitRTTWebApiHost <- function(server = "ttlivewebapi.fxopen.net", port=443L, id = NULL, key = NULL, secret = NULL){
if(length(id) != 0 && length(key) != 0 && length(secret) != 0)
return(RTTWebApiHost$new(InitPrivateWebClient(server, port, id, key, secret)))
return(RTTWebApiHost$new(InitPublicWebClient(server, port)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.