MT4 gRPC JavaScript TypeScript client example

Created with Sketch.

MT4 gRPC JavaScript TypeScript client example

Table of Contents

Methods browser

Proto file

Ready to run example

You need auto generated mt4 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 link to the generated ts files

The script of typescript generation.
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.

Install npm package manager

Run install packages

npm install

Run proto stubs building

npm run build:proto

Run build sources script

npm run build

Run mt4 grpc client

npm run start:client

import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { ProtoGrpcType } from './proto/mt4';
import { OnQuoteReply } from './proto/mt4grpc/OnQuoteReply';

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

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

const connectionClient = new proto.mt4grpc.Connection(
  host,
  grpc.credentials.createSsl()
);
const mt4Client = new proto.mt4grpc.MT4(host, grpc.credentials.createSsl());
const subscriptionClient = new proto.mt4grpc.Subscriptions(
  host,
  grpc.credentials.createSsl()
);
const streamClient = new proto.mt4grpc.Streams(
  host,
  grpc.credentials.createSsl()
);

const deadline = new Date();
deadline.setSeconds(deadline.getSeconds() + 5);
connectionClient.waitForReady(deadline, (error?: Error) => {
  if (error) {
    console.log(`Client connect error: ${error.message}`);
  } else {
    onClientReady();
  }
});

function onClientReady() {
  connectionClient.connect(
    {
      host: 'mt4-demo.roboforex.com',
      port: 443,
      password: 'ehj4bod',
      user: 500476959,
    },
    (err, value) => {
      if (err) {
        console.error(err.message);
      } else {
        if (value?.error) console.log(value?.error.message);
        else {
          if (value?.result) {
            console.log(value?.result);
            OnConnectionEstablished(value?.result);
          }
        }
      }
    }
  );
}

function OnConnectionEstablished(connectionId: string) {
  mt4Client.AccountSummary(
    {
      id: connectionId,
    },
    (err, value) => {
      if (err) {
        console.error(err.message);
      } else {
        if (value?.error) console.log(value?.error.message);
        else {
          if (value?.result) {
            console.log(value?.result.Balance);
            OnBalanceReceived(connectionId);
          }
        }
      }
    }
  );
}

function OnBalanceReceived(connectionId: string) {
  subscriptionClient.Subscribe(
    {
      id: connectionId,
      interval: 0,
      symbol: 'EURUSD',
    },
    (err, value) => {
      if (err) {
        console.error(err.message);
      } else {
        if (value?.error) console.log(value?.error.message);
        else {
          if (value?.result) {
            console.log(value?.result);
            OnSubscriptionDone(connectionId);
          }
        }
      }
    }
  );
}

function OnSubscriptionDone(connectionId: string) {
  const streamOnQuote = streamClient.onQuote({
    id: connectionId,
  });
  streamOnQuote.on('data', (data: OnQuoteReply) => {
    if (data?.error) console.log(data?.error.message);
    else {
      if (data?.result) {
        console.log(data?.result);
      }
    }
  });
}

Leave a Reply

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