MT5 gRPC JavaScript TypeScript client example
Table of Contents
You need auto generated mt5 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 for 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
Install npm package manager first.
Run install packages
npm install
Run proto stubs building
npm run build:proto
Run build sources script
npm run build
Run mt5 grpc client
npm run start:client
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { ProtoGrpcType } from './proto/mt5';
import { OnQuoteReply } from './proto/mt5grpc/OnQuoteReply';
const host = 'mt5grpc.mtapi.io:443';
const packageDefinition = protoLoader.loadSync('./proto/mt5.proto');
const proto = grpc.loadPackageDefinition(
packageDefinition
) as unknown as ProtoGrpcType;
const connectionClient = new proto.mt5grpc.Connection(
host,
grpc.credentials.createSsl()
);
const mt5Client = new proto.mt5grpc.MT5(host, grpc.credentials.createSsl());
const subscriptionClient = new proto.mt5grpc.Subscriptions(
host,
grpc.credentials.createSsl()
);
const streamClient = new proto.mt5grpc.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: '78.140.180.198',
port: 443,
password: 'tecimil4',
user: 62333850,
},
(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) {
mt5Client.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);
}
}
});
}