MT5 manager gRPC client Java example
Table of Contents
Install before run an example
- JAVA JDK open JDK 21.0.1 is recommended
- MAVEN plugin
Do maven clean installation with extended debug info.
It generates libraries with stubs we need for method invocation.
mvn clean install -X
Clases were generated:
If you need generated classes separately from the example project, you can download it from repository.
Input your credential information:
Now we are ready to run.
import io.grpc.ManagedChannelBuilder;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
String host = "mng5grpc.mtapi.io";
int port = 443;
ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port);
MT5mngGrpcApiClient client = new MT5mngGrpcApiClient(channelBuilder.build());
client.Connect();
}
}
import io.grpc.Channel;
import io.grpc.StatusRuntimeException;
import mng5grpc.MainGrpc;
import mng5grpc.Mng5Grpc;
import java.util.List;
public class MT5mngGrpcApiClient {
private final MainGrpc.MainBlockingStub mainControllerBlockingStub;
private String token;
public void setToken(String token) {
this.token = token;
}
public MT5mngGrpcApiClient(Channel channel) {
mainControllerBlockingStub = mng5grpc.MainGrpc.newBlockingStub(channel);
}
public void Connect()
{
mng5grpc.Mng5Grpc.ConnectRequest connectRequest = Mng5Grpc.ConnectRequest.newBuilder()
.setPassword("")
.setServer("")
.setUser(1)
.build();
try
{
Mng5Grpc.ConnectReply connectReply = mainControllerBlockingStub.connect(connectRequest);
if (connectReply.getError().getCodeValue() != 0)
{
System.out.println("Connection failed: ".concat(connectReply.getError().getMessage()));
}
System.out.println("Connection response id: ".concat(connectReply.getResult()));
setToken(connectReply.getResult());
}
catch (StatusRuntimeException e){
System.out.println("RPC failed:".concat(e.getStatus().toString()));
return;
}
try {
Mng5Grpc.AccountsReply accountsReply = mainControllerBlockingStub.accountsList(Mng5Grpc.AccountsRequest.newBuilder().setId(token).build());
if (accountsReply.getError().getCodeValue() != 0)
{
System.out.println("Connection failed: ".concat(accountsReply.getError().getMessage()));
}
List<Long> accounts = accountsReply.getResultList();
for (Long accountId : accounts) {
System.out.println("Account Id: ".concat(String.valueOf(accountId)));
Mng5Grpc.AccountDetailsReply accountReply = mainControllerBlockingStub.accountDetails(Mng5Grpc.AccountDetailsRequest.newBuilder()
.setId(token)
.setLogin(accountId)
.build());
if (accountReply.getError().getCodeValue() != 0)
{
System.out.println("Connection failed: ".concat(accountReply.getError().getMessage()));
}
System.out.println("Account Equity: ".concat(String.valueOf(accountReply.getResult().getEquity())));
System.out.println("Account Balance: ".concat(String.valueOf(accountReply.getResult().getBalance())));
}
}
catch (StatusRuntimeException e){
System.out.println("RPC failed:".concat(e.getStatus().toString()));
}
}
}