MT5 Manager gRPC example for NodeJS
Table of Contents
Run install packages installation:
npm install
Input your server credentials:
This example has a dynamic code generation
You can find static code generation in the typescript example.
Run an example:
node app.js
After that you can run this simple example:
'use strict';
const PROTO_PATH = __dirname + '/mt5mng.proto';
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const async = require('async');
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const hostAddress = "mng5grpc.mtapi.io:443";
const mng5grpc = grpc.loadPackageDefinition(packageDefinition).mng5grpc;
console.log('Creating main client');
const mainClient = new mng5grpc.Main(
hostAddress,
grpc.credentials.createSsl()
);
function main() {
console.log('Main function processing');
async.waterfall([
connectToServer,
getAccounts,
getIndividualAccountDataForEachAccount,
]);
}
function connectToServer(callback) {
console.log('Connect to server function processing');
mainClient.Connect({
server: '',
user: 1,
password: '',
}, (err, value) => {
console.log('Connection processing');
if (err) {
callback(err)
console.error(err.message);
} else {
if (value?.error) console.log(value?.error.message);
else {
if (value?.result) {
console.log('Connection token:', value?.result);
callback(null, value?.result)
}
}
}
})
}
function getAccounts(token, callback) {
mainClient.AccountsList(
{
id: token
},
(err, value) => {
if (err) {
callback(err)
console.error(err.message);
} else {
if (value?.error) console.log(value?.error.message);
else {
if (value?.result) {
callback(null, token, value?.result)
}
}
}
}
)
}
function getIndividualAccountDataForEachAccount(token, accountIds, callback) {
let i = 0;
for (let accountId of accountIds)
{
mainClient.accountDetails({
id: token,
login: accountId
},(err, value) => {
console.log('Account id:', accountId);
if (err) {
callback(err)
console.error(err.message);
} else {
if (value?.error) console.log(value?.error.message);
else {
if (value?.result) {
let accountDetails = value?.result;
console.log('Account zip code:', accountDetails.Zipcode);
console.log('Account Name:', accountDetails.Name);
console.log('Account Balance:', accountDetails.Balance);
console.log('Account State:', accountDetails.State);
}
}
}
})
++i;
if (i === 100)
break
}
}
if (require.main === module) {
main();
}