NodeJS
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 NodeJS
Get actual EURUSD price from MT5.js
// Include Nodejs' net module.
const Net = require('net');
// The port number and hostname of the server.
const port = 71;
const host = 'localhost';
// Create a new TCP client.
const client = new Net.Socket();
// Send a connection request to the server.
client.connect(port, host, function() {
// The client can now send data to the server by writing to its socket.
client.write('{"MSG":"QUOTE","SYMBOL":"EURUSD"}' + '\r\n');
});
// The client can also receive data from the server by reading from its socket.
client.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
client.end();
});
Reply
{
"MSG":"QUOTE",
"SYMBOL":"EURUSD",
"ASK":1.08263,
"BID":1.08261,
"FLAGS":6,
"TIME":"2022.04.13 12:01:48.0",
"VOLUME":0,
"ERROR_ID":0,
"ERROR_DESCRIPTION":"The operation completed successfully"
}
Example 2: Send Order to MT5 using NodeJS
Send Order to MT5.js
const Net = require('net');
const client_cmd = new Net.Socket();
client_cmd.connect(71, "localhost", function() {
client_cmd.write('{"MSG":"ORDER_SEND","SYMBOL":"EURUSD","VOLUME":0.02,"TYPE":"ORDER_TYPE_BUY"}' + '\r\n');
});
client_cmd.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
client_cmd.end();
});
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 NodeJS
Stream actual EURUSD price from MT5.js
const Net = require('net');
const client_cmd = new Net.Socket();
const client_data = new Net.Socket();
client_cmd.connect(71, "localhost", function() {
client_cmd.write('{"MSG":"TRACK_PRICES","SYMBOLS":["EURUSD"]}' + '\r\n');
});
client_cmd.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
client_cmd.end();
});
client_data.connect(72, "localhost");
client_data.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
});
Reply
{"MSG":"TRACK_PRICES","SUCCESS":["EURUSD"],"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully"}
Advanced Examples
Example 1: Get actual EURUSD price from MT5 (using JSON library) with NodeJS
Get actual EURUSD price (using JSON library) from MT5.js
const Net = require('net');
const client_cmd = new Net.Socket();
client_cmd.connect(71, "localhost", function() {
const JSONobj = {};
JSONobj["MSG"]="QUOTE";
JSONobj["SYMBOL"]="EURUSD";
client_cmd.write(JSON.stringify(JSONobj) + '\r\n');
});
client_cmd.on('data', function(chunk) {
//console.log(`${chunk.toString()}`);
const JSONresult = JSON.parse(chunk.toString());
console.log(`Time: ${JSONresult["TIME"]} Symbol: ${JSONresult["SYMBOL"]} Ask: ${JSONresult["ASK"]} Bid: ${JSONresult["BID"]}`);
client_cmd.end();
});
Example 2: Stream OHLC data from MT5 (using JSON library) with NodeJS
Stream OHLC data (using JSON library) from MT5.js
const Net = require('net');
const client_cmd = new Net.Socket();
const client_data = new Net.Socket();
client_cmd.connect(71, "localhost", function() {
const JSONobj = {};
JSONobj["MSG"]="TRACK_OHLC";
JSONobj["SYMBOLS"]=["EURUSD"];
JSONobj["TIMEFRAME"]="PERIOD_M1";
client_cmd.write(JSON.stringify(JSONobj) + '\r\n');
});
client_cmd.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
client_cmd.end();
});
client_data.connect(72, "localhost");
client_data.on('data', function(chunk) {
//console.log(`${chunk.toString()}`);
var str = chunk.toString();
var lines = str.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lines[i].search("OHLC")>0) {
const JSONresult = JSON.parse(lines[i]);
console.log(`Time: ${JSONresult["OHLC"][0]["TIME"]} Symbol: ${JSONresult["SYMBOL"]} Close Price: ${JSONresult["OHLC"][0]["CLOSE"]}`);
}
}
});
Reply
{"MSG":"TRACK_OHLC","SUCCESS":["EURUSD"],"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully","DEMO":"MTsocketAPI running in DEMO mode (www.mtsocketapi.com)"}
Time: 2022.09.20 11:23:00 Symbol: EURUSD Close Price: 1.00055
Time: 2022.09.20 11:24:00 Symbol: EURUSD Close Price: 1.00033
Time: 2022.09.20 11:25:00 Symbol: EURUSD Close Price: 1.00009
Time: 2022.09.20 11:26:00 Symbol: EURUSD Close Price: 1.00048
Time: 2022.09.20 11:27:00 Symbol: EURUSD Close Price: 1.00051
Time: 2022.09.20 11:28:00 Symbol: EURUSD Close Price: 1.00056
Example 3: Export Trade History from MT5 to a CSV file using NodeJS
Export Trade History from MT5 to CSV.js
const Net = require('net');
const fs = require('fs');
const delimiter = ',';
const client_cmd = new Net.Socket();
client_cmd.connect(71, "localhost", function() {
const JSONobj = {};
JSONobj["MSG"]="TRADE_HISTORY";
JSONobj["FROM_DATE"]="2022/09/19 13:00:00";
JSONobj["TO_DATE"]="2022/09/20 00:00:00";
JSONobj["MODE"]="POSITIONS";
client_cmd.write(JSON.stringify(JSONobj) + '\r\n');
});
client_cmd.on('data', function(chunk) {
const JSONresult = JSON.parse(chunk.toString());
console.log(chunk.toString());
console.log(`Number of trades: ${Object.keys(JSONresult['POSITIONS']).length}`);
fs.writeFileSync('tradeHistory.csv','OPEN_TIME,CLOSE_TIME,TICKET,SYMBOL,PRICE_OPEN,PRICE_CLOSE,PROFIT\r\n',{ flag: 'a+' });
for(let i = 0; i < Object.keys(JSONresult['POSITIONS']).length; i++) {
//console.log(JSONresult['TRADES'][i]['OPEN_TIME']);
const OPEN_TIME = JSONresult['POSITIONS'][i]['OPEN_TIME'];
const CLOSE_TIME = JSONresult['POSITIONS'][i]['CLOSE_TIME'];
const TICKET = JSONresult['POSITIONS'][i]['TICKET'];
const SYMBOL = JSONresult['POSITIONS'][i]['SYMBOL'];
const PRICE_OPEN = JSONresult['POSITIONS'][i]['PRICE_OPEN'];
const PRICE_CLOSE = JSONresult['POSITIONS'][i]['PRICE_CLOSE'];
const PROFIT = JSONresult['POSITIONS'][i]['PROFIT'];
fs.writeFileSync('tradeHistory.csv', OPEN_TIME + delimiter + CLOSE_TIME + delimiter + TICKET + delimiter + SYMBOL + delimiter + PRICE_OPEN + delimiter + PRICE_CLOSE + delimiter + PROFIT + '\r\n',{ flag: 'a+' });
}
console.log('Finished!');
client_cmd.end();
});
Reply
{"MSG":"TRADE_HISTORY","POSITIONS":[{"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.19 13:40:55.999","PRICE_CLOSE":0.99769000,"PROFIT":-0.21,"COMMISSION":-0.23,"SWAP":0.00,"SL":0.00000,"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.19 14:46:16.477","PRICE_CLOSE":0.99925000,"PROFIT":0.00,"COMMISSION":-0.08,"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.19 14:47:43.642","PRICE_CLOSE":0.99974000,"PROFIT":0.00,"COMMISSION":-0.08,"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.19 14:48:40.278","PRICE_CLOSE":0.99974000,"PROFIT":0.00,"COMMISSION":-0.08,"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.19 14:57:32.452","PRICE_CLOSE":0.99860000,"PROFIT":0.00,"COMMISSION":-0.05,"SWAP":0.00,"SL":0.00000,"TP":0.00000}],"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully"}
Number of trades: 5
Finished!
Info
Have you found any bug or error? Please notify us.