MT5 client GRPC example for JAVA app

Created with Sketch.

MT5 client GRPC example for JAVA app

Table of Contents

Methods browser

MT5 proto file

Install before running an example

Ready to run example

Do maven clean installation with extended debug info

mvn clean install -X

Clases should be generated.

If you need generated classes separately from the example project, you can download it from repository.

Code examples.
Client class:

import io.grpc.Channel;
import io.grpc.StatusRuntimeException;
import mt5grpc.ConnectionGrpc;
import mt5grpc.MT5Grpc;
import mt5grpc.Mt5.AccountSummaryReply;
import mt5grpc.Mt5.AccountSummaryRequest;
import mt5grpc.Mt5.OnQuoteRequest;
import mt5grpc.Mt5.SubscribeRequest;
import mt5grpc.StreamsGrpc;
import mt5grpc.SubscriptionsGrpc;

public class MT5GrpcApiClient {

    private final MT5Grpc.MT5BlockingStub mt5GrpcBlockingStub;
    private final ConnectionGrpc.ConnectionBlockingStub connectionBlockingStub;
    private final SubscriptionsGrpc.SubscriptionsBlockingStub subscriptionsBlockingStub;
    private final StreamsGrpc.StreamsBlockingStub streamsBlockingStub;

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }
    private String clientId;

    public MT5GrpcApiClient(Channel channel) {
        mt5GrpcBlockingStub = MT5Grpc.newBlockingStub(channel);
        connectionBlockingStub = ConnectionGrpc.newBlockingStub(channel);
        subscriptionsBlockingStub = SubscriptionsGrpc.newBlockingStub(channel);
        streamsBlockingStub = StreamsGrpc.newBlockingStub(channel);
    }

    public void Connect()
    {
        mt5grpc.Mt5.ConnectRequest connectRequest = mt5grpc.Mt5.ConnectRequest.newBuilder()
                .setHost("78.140.180.198")
                .setPassword("tecimil4")
                .setPort(443)
                .setUser(62333850)
                .build();
        try{
            mt5grpc.Mt5.ConnectReply connetReply = connectionBlockingStub.connect(connectRequest);
            if (connetReply.getError().getCodeValue() != 0)
            {
                System.out.println("Connection failed: ".concat(connetReply.getError().getMessage())); 
            }
            System.out.println("Connection rersponse id: ".concat(connetReply.getResult()));     
            setClientId(connetReply.getResult());
        }
        catch (StatusRuntimeException e){
            System.out.println("RPC failed:".concat(e.getStatus().toString()));
            return;
        }

        mt5grpc.Mt5.AccountSummaryRequest accountSummaryRequest =  mt5grpc.Mt5.AccountSummaryRequest.newBuilder().setId(clientId).build();
        try {
            mt5grpc.Mt5.AccountSummaryReply accountSummaryReply = mt5GrpcBlockingStub.accountSummary(accountSummaryRequest); 
            if (accountSummaryReply.getError().getCodeValue() != 0)
            {
                System.out.println("Getting account summary failed: ".concat(accountSummaryReply.getError().getMessage())); 
            }   
            System.out.println("AccountBalance:: ".concat(Double.toString(accountSummaryReply.getResult().getBalance()))); 
        } catch (StatusRuntimeException e) {
             System.out.println("RPC failed:".concat(e.getStatus().toString()));
                return;
        }

        mt5grpc.Mt5.SubscribeRequest subscribeRequest = mt5grpc.Mt5.SubscribeRequest.newBuilder()
        .setId(clientId)
        .setInterval(0)
        .setSymbol("EURUSD")
        .build();

        try {
            mt5grpc.Mt5.SubscribeReply subscribeReply = subscriptionsBlockingStub.subscribe(subscribeRequest);
            if (subscribeReply.getError().getCodeValue() != 0)
            {
                System.out.println("Getting account summary failed: ".concat(subscribeReply.getError().getMessage())); 
            }   
            System.out.println("Subscribe response: ".concat(subscribeReply.getResult().toString())); 
        } catch (StatusRuntimeException e) {
             System.out.println("RPC failed:".concat(e.getStatus().toString()));
                return;
        }

        mt5grpc.Mt5.OnQuoteRequest onQuoteRequest = mt5grpc.Mt5.OnQuoteRequest.newBuilder().setId(clientId).build();
        try {
            java.util.Iterator<mt5grpc.Mt5.OnQuoteReply> onQuoteResponse = streamsBlockingStub.onQuote(onQuoteRequest);
            while (onQuoteResponse.hasNext())
            {
                mt5grpc.Mt5.OnQuoteReply onQuoteReply = onQuoteResponse.next();
                if (onQuoteReply.getError().getCodeValue() != 0)
                    {
                        System.out.println("Getting quote failed: ".concat(onQuoteReply.getError().getMessage())); 
                    }
                System.out.println("Quote :".concat(onQuoteReply.toString()));
            }
        } catch (StatusRuntimeException e) {
             System.out.println("RPC failed:".concat(e.getStatus().toString()));
                return;
        }

    }

}

Main class:

import io.grpc.ManagedChannelBuilder;

public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        String host = "mt5grpc.mtapi.io";
        int port = 443;
        ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port);
        MT5GrpcApiClient client = new MT5GrpcApiClient(channelBuilder.build());
        client.Connect();
    }
}

Leave a Reply

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