Java
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.
Examples
Example 1: Get actual EURUSD price from MT5 using Java
getQuote.java
import java.net.*;
import java.io.*;
/**
*
* $ javac getQuote.java (Compile)
* $ java getQuote (Run)
*
**/
class getQuote
{
public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost", 71);
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
String jsonString;
writer.write("{\"MSG\":\"QUOTE\",\"SYMBOL\":\"EURUSD\"}\r\n");
writer.flush();
jsonString = reader.readLine();
System.out.println(jsonString);
}
catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
}
catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
MTsocketAPI reply
{"MSG":"QUOTE","SYMBOL":"EURUSD","ASK":0.99891,"BID":0.99890,"FLAGS":4,"TIME":"2022.09.19 15:37:14.45","VOLUME":0,"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully"}
Example 2: Send Order to MT5 using Java
sendOrder.java
import java.net.*;
import java.io.*;
/**
* $ javac sendOrder.java (Compile)
* $ java sendOrder (Run)
*
* */
class sendOrder
{
public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost", 71);
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
String jsonString;
writer.write("{\"MSG\":\"ORDER_SEND\",\"SYMBOL\":\"EURUSD\",\"VOLUME\":0.02,\"TYPE\":\"ORDER_TYPE_BUY\"}\r\n");
writer.flush();
jsonString = reader.readLine();
System.out.println(jsonString);
}
catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
}
catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
MTsocketAPI reply
{"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 Java
streamPrices.java
import java.net.*;
import java.io.*;
/**
* $ javac streamPrices.java (Compile)
* $ java streamPrices (Run)
**/
class streamPrices
{
public static void main(String[] args)
{
try
{
Socket socket_cmd = new Socket("localhost", 71);
Socket socket_data = new Socket("localhost", 72);
OutputStream output_cmd = socket_cmd.getOutputStream();
InputStream input_cmd = socket_cmd.getInputStream();
InputStream input_data = socket_data.getInputStream();
BufferedReader reader_cmd = new BufferedReader(new InputStreamReader(input_cmd));
BufferedReader reader_data = new BufferedReader(new InputStreamReader(input_data));
BufferedWriter writer_cmd = new BufferedWriter(new OutputStreamWriter(output_cmd));
String jsonString;
writer_cmd.write("{\"MSG\":\"TRACK_PRICES\",\"SYMBOLS\":[\"EURUSD\"]}\r\n");
writer_cmd.flush();
jsonString = reader_cmd.readLine();
System.out.println(jsonString);
while ((jsonString = reader_data.readLine()) != null) {
System.out.println(jsonString);
}
}
catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
}
catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
MTsocketAPI reply
{"MSG":"TRACK_PRICES","SUCCESS":["EURUSD"],"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfullyr"}
{"TIME":"2022.04.18 12:28:31","SYMBOL":"EURUSD","ASK":1.08088,"BID":1.08085,"VOLUME":24073}
{"TIME":"2022.04.18 12:28:36","SYMBOL":"EURUSD","ASK":1.08088,"BID":1.08084,"VOLUME":24073}
{"TIME":"2022.04.18 12:28:37","SYMBOL":"EURUSD","ASK":1.08087,"BID":1.08083,"VOLUME":24075}
{"TIME":"2022.04.18 12:28:43","SYMBOL":"EURUSD","ASK":1.08087,"BID":1.08084,"VOLUME":24076}
{"TIME":"2022.04.18 12:28:44","SYMBOL":"EURUSD","ASK":1.08088,"BID":1.08084,"VOLUME":24076}
{"TIME":"2022.04.18 12:28:45","SYMBOL":"EURUSD","ASK":1.08087,"BID":1.08084,"VOLUME":24078}
Advanced Examples
Example 1: Get actual EURUSD price from MT5 (using JSON library) using Java
getQuoteJSON.java
import java.net.*;
import java.io.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* $ javac -cp .:json-simple-1.1.1.jar getQuoteJSON.java (Compile)
* $ java -cp .:json-simple-1.1.1.jar getQuoteJSON (Run)
* */
class getQuoteJSON
{
public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost", 71);
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
String jsonString;
JSONObject jo = new JSONObject();
jo.put("MSG","QUOTE");
jo.put("SYMBOL","EURUSD");
writer.write(jo.toString() + "\r\n");
writer.flush();
jsonString = reader.readLine();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(jsonString);
System.out.println("Time: " + json.get("TIME") + " Symbol: " + json.get("SYMBOL") + " Ask: " + json.get("ASK"));
}
catch(ParseException pe) {
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
}
catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
Example 2: Stream OHLC data from MT5 (using JSON library) with Java
getStreamJSON.java
import java.net.*;
import java.io.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* $ javac -cp .:json-simple-1.1.1.jar getStreamJSON.java
* $ java -cp .:json-simple-1.1.1.jar getStreamJSON
* */
class getStreamJSON
{
public static void main(String[] args)
{
try
{
Socket socket_cmd = new Socket("localhost", 71);
Socket socket_data = new Socket("localhost", 72);
OutputStream output_cmd = socket_cmd.getOutputStream();
InputStream input_cmd = socket_cmd.getInputStream();
InputStream input_data = socket_data.getInputStream();
BufferedReader reader_cmd = new BufferedReader(new InputStreamReader(input_cmd));
BufferedReader reader_data = new BufferedReader(new InputStreamReader(input_data));
BufferedWriter writer_cmd = new BufferedWriter(new OutputStreamWriter(output_cmd));
JSONArray ja = new JSONArray();
ja.add("EURUSD");
JSONObject jo = new JSONObject();
jo.put("MSG","TRACK_OHLC");
jo.put("SYMBOLS",ja);
jo.put("TIMEFRAME","PERIOD_M1");
//System.out.println(jo.toString());
writer_cmd.write(jo.toString() + "\r\n");
writer_cmd.flush();
String jsonString;
while ((jsonString = reader_data.readLine()) != null) {
//System.out.println(jsonString);
JSONParser parser = new JSONParser();
JSONObject jso = (JSONObject) parser.parse(jsonString);
if (jso.containsKey("OHLC")) {
JSONArray jarr= (JSONArray)jso.get("OHLC");
JSONObject jelem= (JSONObject)jarr.get(0);
System.out.println("Time: " + jelem.get("TIME") + " Symbol: " + jso.get("SYMBOL") + " Open: " + jelem.get("OPEN") + " Close: " + jelem.get("CLOSE") + " High: " + jelem.get("HIGH") + " Low: " + jelem.get("LOW"));
}
}
}
catch(ParseException pe) {
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
}
catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
MTsocketAPI reply when a new candle closes
Time: 2022.04.18 13:24:00 Symbol: EURUSD Open: 1.08060 Close: 1.08050 High: 1.08061 Low: 1.08043
Time: 2022.04.18 13:25:00 Symbol: EURUSD Open: 1.08049 Close: 1.08053 High: 1.08061 Low: 1.08049
Time: 2022.04.18 13:26:00 Symbol: EURUSD Open: 1.08052 Close: 1.08059 High: 1.08059 Low: 1.08050
Example 3: Export Trade History from MT5 to a CSV file using Java
getHistory.java
import java.net.*;
import java.io.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* $ javac -cp .:json-simple-1.1.1.jar getHistory.java
* $ java -cp .:json-simple-1.1.1.jar getHistory
* */
class getHistory
{
public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost", 71);
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
String jsonString;
JSONObject jo = new JSONObject();
jo.put("MSG","TRADE_HISTORY");
jo.put("FROM_DATE","2022/09/19 07:00:00");
jo.put("TO_DATE","2022/09/21 00:00:00");
jo.put("MODE","POSITIONS"); //MODES: POSITIONS, ORDERS, DEALS and ORDERS_DEALS
//System.out.println(jo.toString());
writer.write(jo.toString() + "\r\n");
writer.flush();
jsonString = reader.readLine();
System.out.println(jsonString);
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(jsonString);
//JSONArray jarr= (JSONArray)json.get("POSITIONS");
JSONArray jarr= (JSONArray)json.get(jo.get("MODE").toString());
System.out.println("Creating file...");
FileWriter myFile = new FileWriter("tradeHistory.csv");
myFile.write("OPEN_TIME,CLOSE_TIME,TICKET,SYMBOL,MAGIC,PRICE_OPEN,PRICE_CLOSE,TYPE,LOTS,STOP_LOSS,TAKE_PROFIT,SWAP,COMMISSION,COMMENT,PROFIT\r\n");
for(int i = 0; i < jarr.size(); i++) {
JSONObject item = (JSONObject) jarr.get(i);
//System.out.println(Integer.toString(i) + " " + item.get("OPEN_TIME"));
myFile.write(item.get("OPEN_TIME").toString() + "," +
item.get("CLOSE_TIME").toString() + "," +
item.get("TICKET").toString() + "," +
item.get("SYMBOL").toString() + "," +
item.get("MAGIC").toString() + "," +
item.get("PRICE_OPEN").toString() + "," +
item.get("PRICE_CLOSE").toString() + "," +
item.get("TYPE").toString() + "," +
item.get("VOLUME").toString() + "," +
item.get("SL").toString() + "," +
item.get("TP").toString() + "," +
item.get("SWAP").toString() + "," +
item.get("COMMISSION").toString() + "," +
item.get("COMMENT") + "," +
item.get("PROFIT").toString() +
"\r\n");
}
myFile.close();
System.out.println("tradeHistory.csv created!");
}
catch(ParseException pe) {
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
}
catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
MTsocketAPI
{"MSG":"TRADE_HISTORY","POSITIONS":[{"OPEN_TIME":"2022.09.19 12:49:05.146","SYMBOL":"EURUSD","TICKET":2012142352,"TYPE":"BUY","VOLUME":0.01,"PRICE_OPEN":0.99746000,"MAGIC":0,"COMMENT":null,"CLOSE_TIME":"2022.09.19 12:50:22.145","PRICE_CLOSE":0.99718000,"PROFIT":-0.28,"COMMISSION":-0.06,"SWAP":0.00,"SL":0.00000,"TP":0.00000},{"OPEN_TIME":"2022.09.19 13:40:13.563","SYMBOL":"EURUSD","TICKET":2012142959,"TYPE":"BUY","VOLUME":0.06,"PRICE_OPEN":0.99776000,"MAGIC":0,"COMMENT":null,"CLOSE_TIME":"2022.09.20 09:37:33.573","PRICE_CLOSE":1.00064500,"PROFIT":17.25,"COMMISSION":-0.31,"SWAP":0.00,"SL":0.99665,"TP":0.00000},{"OPEN_TIME":"2022.09.19 14:46:16.477","SYMBOL":"EURUSD","TICKET":2012143954,"TYPE":"BUY","VOLUME":0.03,"PRICE_OPEN":0.99925000,"MAGIC":0,"COMMENT":null,"CLOSE_TIME":"2022.09.20 09:37:33.55","PRICE_CLOSE":1.00360000,"PROFIT":13.00,"COMMISSION":-0.16,"SWAP":0.00,"SL":0.00000,"TP":0.00000},{"OPEN_TIME":"2022.09.19 14:47:43.642","SYMBOL":"EURUSD","TICKET":2012143985,"TYPE":"BUY","VOLUME":0.03,"PRICE_OPEN":0.99974000,"MAGIC":0,"COMMENT":null,"CLOSE_TIME":"2022.09.20 09:37:32.518","PRICE_CLOSE":1.00361000,"PROFIT":11.57,"COMMISSION":-0.16,"SWAP":0.00,"SL":0.00000,"TP":0.00000},{"OPEN_TIME":"2022.09.19 14:48:40.278","SYMBOL":"EURUSD","TICKET":2012144006,"TYPE":"BUY","VOLUME":0.03,"PRICE_OPEN":0.99974000,"MAGIC":0,"COMMENT":null,"CLOSE_TIME":"2022.09.20 09:37:31.968","PRICE_CLOSE":1.00361000,"PROFIT":11.57,"COMMISSION":-0.16,"SWAP":0.00,"SL":0.00000,"TP":0.00000},{"OPEN_TIME":"2022.09.19 14:57:32.452","SYMBOL":"EURUSD","TICKET":2012144150,"TYPE":"BUY","VOLUME":0.02,"PRICE_OPEN":0.99860000,"MAGIC":0,"COMMENT":null,"CLOSE_TIME":"2022.09.20 09:37:31.206","PRICE_CLOSE":1.00359000,"PROFIT":9.94,"COMMISSION":-0.10,"SWAP":0.00,"SL":0.00000,"TP":0.00000}],"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully","DEMO":"MTsocketAPI running in DEMO mode (www.mtsocketapi.com)"}
Creating file...
tradeHistory.csv created!
tradeHistory.csv
OPEN_TIME,CLOSE_TIME,TICKET,SYMBOL,MAGIC,PRICE_OPEN,PRICE_CLOSE,TYPE,LOTS,STOP_LOSS,TAKE_PROFIT,SWAP,COMMISSION,COMMENT,PROFIT
2022.09.19 12:49:05.146,2022.09.19 12:50:22.145,2012142352,EURUSD,0,0.99746,0.99718,BUY,0.01,0.0,0.0,0.0,-0.06,null,-0.28
2022.09.19 13:40:13.563,2022.09.20 09:37:33.573,2012142959,EURUSD,0,0.99776,1.000645,BUY,0.06,0.99665,0.0,0.0,-0.31,null,17.25
2022.09.19 14:46:16.477,2022.09.20 09:37:33.55,2012143954,EURUSD,0,0.99925,1.0036,BUY,0.03,0.0,0.0,0.0,-0.16,null,13.0
2022.09.19 14:47:43.642,2022.09.20 09:37:32.518,2012143985,EURUSD,0,0.99974,1.00361,BUY,0.03,0.0,0.0,0.0,-0.16,null,11.57
2022.09.19 14:48:40.278,2022.09.20 09:37:31.968,2012144006,EURUSD,0,0.99974,1.00361,BUY,0.03,0.0,0.0,0.0,-0.16,null,11.57
2022.09.19 14:57:32.452,2022.09.20 09:37:31.206,2012144150,EURUSD,0,0.9986,1.00359,BUY,0.02,0.0,0.0,0.0,-0.1,null,9.94
Info
Have you found any bug or error? Please notify us.