MT4 manager gRPC API Go language client example
Table of Contents
MT4 manager gRPC API proto file
Install autogenerated cleint libraries:
go get git.mtapi.io/root/grpc-proto.git/mt4mng/go
go get github.com/davecgh/go-spew
Alternatively, use this command for your own libraries' generation, if needed:
docker run --user "$(id -u):$(id -g)" -v `pwd`:/defs namely/protoc-all -f ./$PROJECT/Protos/mt4mng.proto -l go -o ./golib
Put you credentials for mt4 server:
Use commands to run the example:
go build main.go
go run main.go
The script:
package main
import (
"context"
"crypto/tls"
"fmt"
mt4mng "git.mtapi.io/root/grpc-proto.git/mt4mng/go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"log"
)
func main() {
config := &tls.Config{
InsecureSkipVerify: false,
}
channel, channelError := grpc.Dial("mng4grpc.mtapi.io:443", grpc.WithTransportCredentials(credentials.NewTLS(config)))
if channelError != nil {
log.Fatalf("failed to connect: %v", channelError)
}
defer channel.Close()
mainControllerClient := mt4mng.NewMainControllerClient(channel)
var connectResponse, connectionError = mainControllerClient.Connect(context.Background(), &mt4mng.ConnectRequest{
Server: "",
Password: "",
User: 1,
})
if connectionError != nil {
log.Fatalf("gRPC protocol error: %v", connectionError)
}
if connectResponse.Error != nil {
log.Fatalf("Server error: %v", connectResponse.Error.Message)
}
fmt.Println(connectResponse)
id := connectResponse.Result
var accountsResponse, accountsError = mainControllerClient.AccountsList(context.Background(), &mt4mng.AccountsRequest{Id: id})
if accountsError != nil {
log.Fatalf("gRPC protocol error: %v", accountsError)
}
if accountsResponse.Error != nil {
log.Fatalf("Server error: %v", accountsResponse.Error.Message)
}
for _, accountId := range accountsResponse.Result {
var accountResponse, accountError = mainControllerClient.AccountDetails(context.Background(), &mt4mng.AccountDetailsRequest{
Id: id,
Login: accountId,
})
if accountError != nil {
log.Fatalf("gRPC protocol error: %v", accountError)
}
if accountResponse.Error != nil {
log.Fatalf("Server error: %v", accountResponse.Error.Message)
}
fmt.Println(accountResponse.Result.Balance)
}
}