MT5 manager gRPC API Go language client example

Created with Sketch.

MT5 manager gRPC API Go language client example

Table of Contents

MT5 manager gRPC API methods

MT5 manager gRPC API proto file

Ready to run example

Install autogenerated cleint libraries:

go get git.mtapi.io/root/grpc-proto.git/mng5grpc/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/mt5mng.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

package main

import (
    "context"
    "crypto/tls"
    "fmt"
    mt5mng "git.mtapi.io/root/grpc-proto.git/mng5grpc/go"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials"
    "log"
)

func main() {
    config := &tls.Config{
        InsecureSkipVerify: false,
    }
    channel, channelError := grpc.Dial("mng5grpc.mtapi.io:443", grpc.WithTransportCredentials(credentials.NewTLS(config)))
    if channelError != nil {
        log.Fatalf("failed to connect: %v", channelError)
    }
    defer channel.Close()

    mainClient := mt5mng.NewMainClient(channel)
    var connectResponse, connectionError = mainClient.Connect(context.Background(), &mt5mng.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 = mainClient.AccountsList(context.Background(), &mt5mng.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 = mainClient.AccountDetails(context.Background(), &mt5mng.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)
    }
}

Leave a Reply

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