MT4 client GRPC example for JAVA app

Created with Sketch.

MT4 client GRPC example for JAVA app

Table of Contents

Methods browser

MT4 proto file

Install before run 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 mt4grpc.ConnectionGrpc;
import mt4grpc.MT4Grpc;
import mt4grpc.StreamsGrpc;
import mt4grpc.SubscriptionsGrpc;
import mt4grpc.Mt4.AccountSummaryReply;
import mt4grpc.Mt4.AccountSummaryRequest;
import mt4grpc.Mt4.ConnectReply;
import mt4grpc.Mt4.ConnectRequest;
import mt4grpc.Mt4.OnQuoteRequest;
import mt4grpc.Mt4.SubscribeRequest;

public class Mt4GrpcApiClient {

    private final MT4Grpc.MT4BlockingStub mt4GrpcBlockingStub;
    private final MT4Grpc.MT4Stub mt4GrpcAsyncStub;
    private final ConnectionGrpc.ConnectionBlockingStub connectionBlockingStub;
    private final ConnectionGrpc.ConnectionStub connectionStub;
    private final SubscriptionsGrpc.SubscriptionsBlockingStub subscriptionsBlockingStub;
    private final StreamsGrpc.StreamsBlockingStub streamsBlockingStub;

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

    private String clientId;

    public Mt4GrpcApiClient(Channel channel) {
        mt4GrpcBlockingStub = MT4Grpc.newBlockingStub(channel);
        mt4GrpcAsyncStub = MT4Grpc.newStub(channel);
        connectionBlockingStub = ConnectionGrpc.newBlockingStub(channel);
        connectionStub = ConnectionGrpc.newStub(channel);
        subscriptionsBlockingStub = SubscriptionsGrpc.newBlockingStub(channel);
        streamsBlockingStub = StreamsGrpc.newBlockingStub(channel);
    }

    public void Connect()
    {
        ConnectRequest connectRequest = ConnectRequest.newBuilder()
        .setHost("mt4-demo.roboforex.com")
        .setPassword("ehj4bod")
        .setPort(443)
        .setUser(500476959)
        .build();

        try {
            ConnectReply connectionResponse = connectionBlockingStub.connect(connectRequest); 

            if (connectionResponse.getError().getCodeValue() != 0)
            {
                System.out.println("Connection failed: ".concat(connectionResponse.getError().getMessage())); 
            }
            System.out.println("Connection rersponse id: ".concat(connectionResponse.getResult()));     
            setClientId(connectionResponse.getResult());

        }    
        catch (StatusRuntimeException e) {
             System.out.println("RPC failed:".concat(e.getStatus().toString()));
                return;
        }

        AccountSummaryRequest accountSummaryRequest = AccountSummaryRequest.newBuilder().setId(clientId).build();
        try {
            AccountSummaryReply accountSummaryReply = mt4GrpcBlockingStub.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;
        }

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

        try {
            mt4grpc.Mt4.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;
        }

        OnQuoteRequest onQuoteRequest = OnQuoteRequest.newBuilder().setId(clientId).build();
        try {
            java.util.Iterator<mt4grpc.Mt4.OnQuoteReply> onQuoteResponse = streamsBlockingStub.onQuote(onQuoteRequest);
            while (onQuoteResponse.hasNext())
            {
                mt4grpc.Mt4.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 Main {
    public static void main(String[] args)
    {
       String host = "mt4grpc.mtapi.io";
       int port = 443;
       ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port);
       Mt4GrpcApiClient client = new Mt4GrpcApiClient(channelBuilder.build());
       client.Connect();
   }
}

Leave a Reply

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