MT5 gRPC Client NodeJS example

Created with Sketch.

MT5 gRPC Client NodeJS example

Table of Contents

Methods browser

MT5 proto file

You need npm installed

Ready to run example

This example has a dynamic code generation
You can find satic code generation in the typescript example.

Run install packages installation:

npm install

Run an example

node app.js

'use strict';

const PROTO_PATH = __dirname + '/mt5.proto';
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');

const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true,
});

const mt5grpc = grpc.loadPackageDefinition(packageDefinition).mt5grpc;

function main() {
    const connection = new mt5grpc.Connection(
        "mt5grpc.mtapi.io",
        grpc.credentials.createSsl()
        //grpc.credentials.createInsecure()
    );

    const mt5 = new mt5grpc.MT5(
        "mt5grpc.mtapi.io",
        grpc.credentials.createSsl()
    );

    const subscriptions = new mt5grpc.Subscriptions(
        "mt5grpc.mtapi.io",
        grpc.credentials.createSsl()
    );

    const streams = new mt5grpc.Streams(
        "mt5grpc.mtapi.io",
        grpc.credentials.createSsl()
    );

    connection.Connect({ user: 62333850, password: "tecimil4", host: "78.140.180.198", port: 443 }, function (err, response) {
        const token = response.result;
        console.log('id:', token);

        mt5.AccountSummary({ id: token }, function (err, response) {
            console.log('AccountSummary:', response.result);
        });

        subscriptions.Subscribe({ id: token, symbol: "EURUSD" }, function (err, response) {
            console.log('Subscribe:', response.result);
        });

        var onquote = streams.OnQuote({ id: token });
        onquote.on("data", function (st) {
            console.log("OnQuote message :", st.result);
        });
        onquote.on("end", function () {
            console.log("OnQuote end:");
        });
    });
}

main();
setTimeout(function () {
    console.log('Done');
}, 30000);

Leave a Reply

Your email address will not be published. Required fields are marked *