Skip to content

R

Warning

Please note that all source codes provided are for testing purposes only. No guarantee or responsibility is provided for their use. Use of these examples is at your own risk.

Basic Examples

Example 1: Get actual EURUSD price from MT4 using R

con <- socketConnection(host="localhost", port = 77, blocking = TRUE)
writeLines("{\"MSG\":\"QUOTE\",\"SYMBOL\":\"EURUSD\"}\r\n", con)
server_resp <- readLines(con, 1)
close(con)
print(server_resp)

Reply:

[1] "{\"MSG\":\"QUOTE\",\"SYMBOL\":\"EURUSD\",\"ASK\":0.99080,\"BID\":0.99074,\"FLAGS\":6,\"TIME\":\"2022.09.21 08:22:28.0\",\"VOLUME\":0,\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"no error\"}"

Example 2: Send Order to MT4 using R

con <- socketConnection(host="localhost", port = 77, blocking = TRUE)
writeLines("{\"MSG\":\"ORDER_SEND\",\"SYMBOL\":\"EURUSD\",\"VOLUME\":0.03,\"TYPE\":\"ORDER_TYPE_BUY\"}\r\n", con)
server_resp <- readLines(con, 1)
close(con)
print(server_resp)

Reply:

[1] "{\"MSG\":\"ORDER_SEND\",\"TICKET\":47587709,\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"no error\",\"DEMO\":\"MTsocketAPI running in DEMO mode (www.mtsocketapi.com)\"}"

Example 3: Stream actual EURUSD price from MT4 using R

conCMD <- socketConnection(host="localhost", port = 77, blocking = TRUE)
conDATA <- socketConnection(host="localhost", port = 78, blocking = TRUE)
writeLines("{\"MSG\":\"TRACK_PRICES\",\"SYMBOLS\":[\"EURUSD\"]}\r\n", conCMD)
server_resp <- readLines(conCMD, 1)
print(server_resp)
repeat {
    print(readLines(conDATA, 1))
}
close(conCMD)
close(conDATA)

Reply:

[1] "{\"MSG\":\"TRACK_PRICES\",\"SUCCESS\":[\"EURUSD\"],\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"no error\"}"
[1] "{\"TIME\":\"2022.09.21 08:26:13\",\"SYMBOL\":\"EURUSD\",\"ASK\":0.99092,\"BID\":0.99087,\"VOLUME\":63164}"
[1] "{\"TIME\":\"2022.09.21 08:26:15\",\"SYMBOL\":\"EURUSD\",\"ASK\":0.99094,\"BID\":0.99089,\"VOLUME\":63164}"
[1] "{\"TIME\":\"2022.09.21 08:26:15\",\"SYMBOL\":\"EURUSD\",\"ASK\":0.99094,\"BID\":0.99090,\"VOLUME\":63164}"
[1] "{\"TIME\":\"2022.09.21 08:26:15\",\"SYMBOL\":\"EURUSD\",\"ASK\":0.99096,\"BID\":0.99091,\"VOLUME\":63164}"

Advanced Examples

Example 1: Get actual EURUSD price to MT4 (using JSON library) with R

library("rjson")

con <- socketConnection(host="localhost", port = 77, blocking = TRUE)
df <- data.frame(MSG = c("QUOTE"), SYMBOL = c("EURUSD"))
writeLines(paste0(toJSON(df),"\r\n"), con)
server_resp <- readLines(con, 1)
close(con)
print(server_resp)

Reply:

[1] "{\"MSG\":\"QUOTE\",\"SYMBOL\":\"EURUSD\",\"ASK\":0.99155,\"BID\":0.99155,\"FLAGS\":6,\"TIME\":\"2022.09.21 08:28:59.0\",\"VOLUME\":0,\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"no error\"}"

Example 2: Stream OHLC data from MT4 (using JSON library) with R

library("rjson")

conCMD <- socketConnection(host="localhost", port = 77, blocking = TRUE)
conDATA <- socketConnection(host="localhost", port = 78, blocking = TRUE)

symbol <- list(SYMBOL = c("EURUSD"), TIMEFRAME = c("PERIOD_M1"))
array <- list(symbol)
result <- list(MSG = c("TRACK_OHLC"), "OHLC" = array)

writeLines(paste0(toJSON(result),"\r\n"), conCMD)

server_resp <- readLines(conCMD, 1)
#print(server_resp)
jsonReply <- fromJSON(server_resp)
if (jsonReply["ERROR_ID"] == 0) {
  print("Waiting for OHLC data. Please wait...")
  repeat {
      JSONdata <- fromJSON(readLines(conDATA, 1))
      if (is.null(JSONdata[["OHLC"]]) == FALSE) {
        print(paste("OHLC Prices:",readLines(conDATA, 1)))  
      }
  }
} else {
  print(jsonReply["ERROR_DESCRIPTION"])
  }
close(conCMD)
close(conDATA)

Reply:

[1] "Waiting for OHLC data. Please wait..."
[1] "OHLC Prices: {\"TIME\":\"2022.09.21 08:58:02\",\"SYMBOL\":\"EURUSD.pro\",\"ASK\":0.99112,\"BID\":0.99112,\"VOLUME\":39595}"
[1] "OHLC Prices: {\"TIME\":\"2022.09.21 08:59:01\",\"SYMBOL\":\"EURUSD.pro\",\"ASK\":0.99103,\"BID\":0.99101,\"VOLUME\":39684}"
[1] "OHLC Prices: {\"TIME\":\"2022.09.21 09:00:03\",\"SYMBOL\":\"EURUSD.pro\",\"ASK\":0.99122,\"BID\":0.99119,\"VOLUME\":39726}"

Example 3: Export Trade History from MT4 to a CSV file using R

library("rjson")

con <- socketConnection(host="localhost", port = 77, blocking = TRUE)
df <- data.frame(MSG = c("TRADE_HISTORY"), FROM_DATE = c("2022.09.09 10:00:00"), TO_DATE = c("2022.09.20 23:59:59"))
writeLines(paste0(toJSON(df),"\r\n"), con)
server_resp <- readLines(con, 1)
json_resp <- fromJSON(server_resp)

if (json_resp["ERROR_ID"] == 0) {
  #print(server_resp)
  open_time <- c()
  close_time <- c()
  symbol <- c()
  ticket <- c()
  price_open <- c()
  price_close <- c()
  profit <- c()
  for (row in json_resp[["TRADES"]]) {
    open_time <- append(open_time,row[["OPEN_TIME"]])
    close_time <- append(close_time,row[["CLOSE_TIME"]])
    symbol <- append(symbol,row[["SYMBOL"]])
    ticket <- append(ticket,row[["TICKET"]])
    price_open <- append(price_open,row[["PRICE_OPEN"]])
    price_close <- append(price_close,row[["PRICE_CLOSE"]])
    profit <- append(profit,row[["PROFIT"]])
  }
  df <- data.frame(OPEN_TIME=open_time,CLOSE_TIME=close_time,SYMBOL=symbol,TICKET=ticket,PRICE_OPEN=price_open,PRICE_CLOSE=price_close,PROFIT=profit)
  write.csv(df,file="tradeHistoryFinal.csv",row.names = FALSE)
  print("File created successfully!")
} else {
  print(json_resp["ERROR_DESCRIPTION"])
}
close(con)

Reply:

[1] "File created successfully!"

Have you found any bug or error? Please notify us.