R
WARNING: All these source codes are only examples used for testing. We don’t provide any guarantee or responsibility about it. Use these examples at your own risk.
Basic Examples for MT5 (for MT4 click here)
Example 1: Get actual EURUSD price from MT5 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\":1.00062,\"BID\":1.00058,\"FLAGS\":6,\"TIME\":\"2022.09.20 12:49:31.377\",\"VOLUME\":0,\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"The operation completed successfully\",\"DEMO\":\"MTsocketAPI running in DEMO mode (www.mtsocketapi.com)\"}"
Example 2: Send Order to MT5 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\",\"RETCODE\":10009,\"DEAL\":2015119247,\"ORDER\":2020570871,\"VOLUME\":0.10,\"PRICE\":1.064860,\"BID\":1.064830,\"ASK\":1.064860,\"REQUEST_ID\":3136973155,\"RETCODE_EXTERNAL\":0,\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"The operation completed successfully\"}"
Example 3: Stream actual EURUSD price from MT5 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\":\"The operation completed successfully\",\"DEMO\":\"MTsocketAPI running in DEMO mode (www.mtsocketapi.com)\"}"
[1] "{\"TIME\":\"2022.09.20 13:12:41.288\",\"SYMBOL\":\"EURUSD\",\"ASK\":1.00013,\"BID\":1.00009,\"VOLUME\":49713"
[1] "{\"TIME\":\"2022.09.20 13:12:45.63\",\"SYMBOL\":\"EURUSD\",\"ASK\":1.00009,\"BID\":1.00006,\"VOLUME\":49714}"
[1] "{\"TIME\":\"2022.09.20 13:12:45.112\",\"SYMBOL\":\"EURUSD\",\"ASK\":1.00008,\"BID\":1.00005,\"VOLUME\":49715}"
[1] "{\"TIME\":\"2022.09.20 13:12:45.166\",\"SYMBOL\":\"EURUSD\",\"ASK\":1.00013,\"BID\":1.00009,\"VOLUME\":49716}"
[1] "{\"TIME\":\"2022.09.20 13:12:45.268\",\"SYMBOL\":\"EURUSD\",\"ASK\":1.00014,\"BID\":1.00011,\"VOLUME\":49717}"
[1] "{\"TIME\":\"2022.09.20 13:12:45.316\",\"SYMBOL\":\"EURUSD\",\"ASK\":1.00014,\"BID\":1.00010,\"VOLUME\":49718}"
Advanced Examples
Example 1: Get actual EURUSD price to MT5 (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\":1.00104,\"BID\":1.00100,\"FLAGS\":6,\"TIME\":\"2022.09.20 13:28:24.379\",\"VOLUME\":0,\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"The operation completed successfully\"}"
Example 2: Stream OHLC data from MT5 (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 {
print(paste("OHLC Prices:",readLines(conDATA, 1)))
}
} else {
print(jsonReply["ERROR_DESCRIPTION"])
}
close(conCMD)
close(conDATA)
Reply:
[1] "{\"MSG\":\"TRACK_OHLC\",\"SUCCESS\":[\"EURUSD\"],\"ERROR_ID\":0,\"ERROR_DESCRIPTION\":\"The operation completed successfully\",\"DEMO\":\"MTsocketAPI running in DEMO mode (www.mtsocketapi.com)\"}"
[1] "Waiting for OHLC data. Please wait..."
[1] "OHLC Prices: {\"SYMBOL\":\"EURUSD\",\"PERIOD\":\"PERIOD_M1\",\"OHLC\":[{\"TIME\":\"2022.09.20 15:16:00\",\"OPEN\":0.99830,\"HIGH\":0.99843,\"LOW\":0.99821,\"CLOSE\":0.99826,\"TICK_VOLUME\":163}]}"
[1] "OHLC Prices: {\"SYMBOL\":\"EURUSD\",\"PERIOD\":\"PERIOD_M1\",\"OHLC\":[{\"TIME\":\"2022.09.20 15:17:00\",\"OPEN\":0.99826,\"HIGH\":0.99844,\"LOW\":0.99812,\"CLOSE\":0.99827,\"TICK_VOLUME\":170}]}"
[1] "OHLC Prices: {\"SYMBOL\":\"EURUSD\",\"PERIOD\":\"PERIOD_M1\",\"OHLC\":[{\"TIME\":\"2022.09.20 15:18:00\",\"OPEN\":0.99828,\"HIGH\":0.99828,\"LOW\":0.99783,\"CLOSE\":0.99800,\"TICK_VOLUME\":176}]}"
Example 3: Export Trade History from MT5 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.19 10:00:00"), TO_DATE = c("2022.09.20 23:59:59"), MODE = "POSITIONS")
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[["POSITIONS"]]) {
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("CSV file created successfully!")
} else {
print(json_resp["ERROR_DESCRIPTION"])
}
close(con)
Reply:
[1] "CSV file created successfully!"
Have you found any bug or error? Please notify us.