MT5 manager gRPC JavaScript TypeScript client example

Created with Sketch.

MT5 manager gRPC JavaScript TypeScript client example

Table of Contents

API methods browser

MT5 manager proto file

You need npm manager

Ready to run example

You need auto generated mt5 manager client libraries to run the code. The example has a script named "build:proto". This script creates libraries for you, and you should't care about this.

But, for those of you who has more experience and, for some reason, wants to generate libraries separately or download it directly, we provide direct link and generation script.

Direct download link for js typescript autogenerated libraries.

The script of generation typescript.
Windows:

docker run -v ${pwd}:/defs namely/protoc-all -f ./proto/*.proto -l typescript -o generated --ts_opt useOptionals=all

Linux:

docker run --user "$(id -u):$(id -g)" -v `pwd`:/defs namely/protoc-all -f ./proto/*.proto -l typescript -o generated --ts_opt useOptionals=all

Let's return to our ready to run example.
Run install packages

npm install

Run proto stubs building

npm run build:proto

Set your mt5 manager server credetials

Run build sources script

npm run build

Run mt5 manager grpc client

npm run start:client

import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';

import { ProtoGrpcType } from './proto/mng5grpc';
import { MainClient } from './proto/mng5grpc/Main';
import {Account__Output} from "./proto/mng5grpc/Account";

const host = 'mng5grpc.mtapi.io:443';

const packageDefinition = protoLoader.loadSync('./proto/mng5grpc.proto');
const proto = grpc.loadPackageDefinition(
    packageDefinition
) as unknown as ProtoGrpcType;

const mainClient: MainClient = new proto.mng5grpc.Main(
    host,
    grpc.credentials.createSsl()
);

const deadline = new Date();
deadline.setSeconds(deadline.getSeconds() + 5);

mainClient.waitForReady(deadline, (error?: Error) => {
    if (error) {
        console.log(`Client connect error: ${error.message}`);
    } else {
        onMainReady();
    }
})

function onMainReady() {
    mainClient.Connect(
        {
            server: '',
            user: 1,
            password: '',
        }, (err, value) => {
            if (err) {
                console.error(err.message);
            } else {
                if (value?.error) console.log(value?.error.message);
                else {
                    if (value?.result) {
                        console.log('Connection token:', value?.result);
                        OnConnectionEstablished(value?.result);
                    }
                }
            }
        }
    )
}

function OnConnectionEstablished(token: string) {
    mainClient.AccountsList(
        {
            id: token
        },
        (err, value) => {
            if (err) {
                console.error(err.message);
            } else {
                if (value?.error) console.log(value?.error.message);
                else {
                    if (value?.result) {
                        let accountIds : string[] = value?.result;
                        console.log("Accounts count:", accountIds.length)
                        let i = 0;
                        for (let accountId of accountIds)
                        {
                            GetAccountProperties(Number(accountId), token)
                            ++i;
                            if (i == 100)
                                break
                        }
                        // accountIds.forEach((accountId) => GetAccountProperties(Number(accountId), token));
                    }
                }
            }
        }
    )
}

function GetAccountProperties(accountId: number, token: string) {
    mainClient.accountDetails({
        id: token,
        login: accountId
    },(err, value) => {
        if (err) {
            console.error(err.message);
        } else {
            if (value?.error) console.log(value?.error.message);
            else {
                if (value?.result) {
                    console.log('Account id:', accountId);
                    let accountDetails: Account__Output | undefined = value?.result;
                    console.log('Account Balance:', accountDetails?.Balance);
                    console.log('Account Profit:', accountDetails?.Profit);
                }
            }
        }
    })
}

Leave a Reply

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