NodeJS
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.s
Basic Examples
Example 1: Get actual EURUSD price from MT4 using NodeJS
const Net = require('net');
const client = new Net.Socket();
client.connect(77, "localhost", function() {
client.write('{"MSG":"QUOTE","SYMBOL":"EURUSD"}' + '\r\n');
});
client.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
client.end();
});
Result:
{
"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":"no error"
}
Example 2: Send Order to MT4 using NodeJS
const Net = require('net');
const client_cmd = new Net.Socket();
client_cmd.connect(77, "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();
});
Result:
Example 3: Stream actual EURUSD price from MT4 using NodeJS
const Net = require('net');
const client_cmd = new Net.Socket();
const client_data = new Net.Socket();
client_cmd.connect(77, "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(78, "localhost");
client_data.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
});
Result:
{"MSG":"TRACK_PRICES","SUCCESS":["EURUSD"],"ERROR_ID":0,"ERROR_DESCRIPTION":"no error"}
{"TIME":"2022.04.13 12:20:04","SYMBOL":"EURUSD","ASK":1.08373,"BID":1.08371,"VOLUME":19596}
{"TIME":"2022.04.13 12:20:13","SYMBOL":"EURUSD","ASK":1.08373,"BID":1.08370,"VOLUME":19596}
{"TIME":"2022.04.13 12:20:14","SYMBOL":"EURUSD","ASK":1.08373,"BID":1.08371,"VOLUME":26891}
{"TIME":"2022.04.13 12:20:14","SYMBOL":"EURUSD","ASK":1.08373,"BID":1.08370,"VOLUME":26892}
{"TIME":"2022.04.13 12:20:17","SYMBOL":"EURUSD","ASK":1.08373,"BID":1.08371,"VOLUME":26893}
Advanced Examples
Example 1: Get actual EURUSD price from MT4 (using JSON library) with NodeJS
const Net = require('net');
const client_cmd = new Net.Socket();
client_cmd.connect(77, "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();
});
Result:
Example 2: Stream OHLC data from MT4 (using JSON library) with NodeJS
const Net = require('net');
const client_cmd = new Net.Socket();
const client_data = new Net.Socket();
client_cmd.connect(77, "localhost", function() {
const JSONobj = {};
const subJSONobj = {};
JSONobj["MSG"] = "TRACK_OHLC";
subJSONobj["SYMBOL"] = "EURUSD";
subJSONobj["TIMEFRAME"] = "PERIOD_M1";
subJSONobj["DEPTH"] = 1;
JSONobj["OHLC"] = [subJSONobj];
client_cmd.write(JSON.stringify(JSONobj) + '\r\n');
});
client_cmd.on('data', function(chunk) {
console.log(`${chunk.toString()}`);
client_cmd.end();
});
client_data.connect(78, "localhost");
client_data.on('data', function(chunk) {
const JSONresult = JSON.parse(chunk.toString());
console.log(`Time: ${JSONresult["OHLC"][0]["TIME"]} Symbol: ${JSONresult["SYMBOL"]} Close Price: ${JSONresult["OHLC"][0]["CLOSE"]}`);
});
Result:
{"MSG":"TRACK_OHLC","SUCCESS":["EURUSD"],"ERROR_ID":0,"ERROR_DESCRIPTION":"no error"}
Time: 2022.04.13 13:05:00 Symbol: EURUSD Close Price: 1.08264
Time: 2022.04.13 13:06:00 Symbol: EURUSD Close Price: 1.08271
Time: 2022.04.13 13:07:00 Symbol: EURUSD Close Price: 1.08287
Example 3: Export Trade History from MT4 to a CSV file using NodeJS
const Net = require('net');
const fs = require('fs');
const delimiter = ',';
const exportedFile = 'tradeHistory.csv';
const fromDate = "2022/04/12 13:00:00";
const toDate = "2022/04/15 00:00:00";
const client_cmd = new Net.Socket();
client_cmd.connect(77, "localhost", function() {
const JSONobj = {};
JSONobj["MSG"]="TRADE_HISTORY";
JSONobj["FROM_DATE"]=fromDate;
JSONobj["TO_DATE"]=toDate;
client_cmd.write(JSON.stringify(JSONobj) + '\r\n');
});
client_cmd.on('data', function(chunk) {
const JSONresult = JSON.parse(chunk.toString());
const numTrades = Object.keys(JSONresult['TRADES']).length;
console.log(`Number of trades: ${numTrades}`);
//First we must write the CSV HEADER
fs.writeFileSync('tradeHistory.csv','OPEN_TIME,CLOSE_TIME,TICKET,SYMBOL,LOTS,OPEN_PRICE,CLOSE_PRICE,PROFIT\r\n',{ flag: 'a+' });
//Loop JSON array
for(let i = 0; i < numTrades; i++) {
//You can add more files like COMMENT, SWAP, COMMISSION...
const OPEN_TIME = JSONresult['TRADES'][i]['OPEN_TIME'];
const CLOSE_TIME = JSONresult['TRADES'][i]['CLOSE_TIME'];
const TICKET = JSONresult['TRADES'][i]['TICKET'];
const SYMBOL = JSONresult['TRADES'][i]['SYMBOL'];
const LOTS = JSONresult['TRADES'][i]['LOTS'];
const OPEN_PRICE = JSONresult['TRADES'][i]['OPEN_PRICE'];
const CLOSE_PRICE = JSONresult['TRADES'][i]['CLOSE_PRICE'];
const PROFIT = JSONresult['TRADES'][i]['PROFIT'];
fs.writeFileSync(exportedFile, OPEN_TIME + delimiter + CLOSE_TIME + delimiter + TICKET + delimiter + SYMBOL + delimiter + LOTS + delimiter + OPEN_PRICE + delimiter + CLOSE_PRICE + delimiter + PROFIT + '\r\n',{ flag: 'a+' });
}
console.log(`Created the file ${exportedFile}!`);
client_cmd.end();
});
Result:
Have you found any bug or error? Please notify us.