MT5 manager gRPC Python client example

MT5 manager gRPC Python client example

Methods browser

MT5 manager proto file

Python installation

PIP package manager installation

Install grpcio-tools package:

pip install grpcio-tools

To run the code, you need to create an autogenerated libraries. You can find it in the repository, or generate it by yourself. Generation instructions are followed below.

Grpc python libraries generation:

Once grpcio-tools is installed, you can get google protobuf python libraries form that package. Usually it is located in the python folder

Python\Python312\site-packages\google

Copy google folder to the project folder. It is needed for further library autogeneration.

Put mng5grpc.proto file to the project folder. Or check if it exists in you project folder.

Run libraries generator:

python -m grpc_tools.protoc --proto_path=.  --python_out=. --pyi_out=. --grpc_python_out=. ./mng5grpc.proto

Ready to run example

Add your credentials in the script:

Now we are ready for execution

from grpc._channel import Channel

from mng5grpc_pb2 import *
from mng5grpc_pb2_grpc import *
from mng5grpc_pb2_grpc import MainStub

channel: Channel = grpc.secure_channel('mng5grpc.mtapi.io:443', grpc.ssl_channel_credentials())
mainStub: MainStub = MainStub(channel)

try:
    # TODO add your credentials here
    connectionRequest = ConnectRequest(
        user=1,
        password='',
        server=''
    )
    connectionResponse: ConnectReply = mainStub.Connect(connectionRequest)
    if connectionResponse.error.message:
        print(connectionResponse.error.message)
        exit()
    token: str = connectionResponse.result
    print(token)
except Exception as exc:
    print(exc)
    exit()

try:
    accountsRequest: AccountsRequest = AccountsRequest(
        id=token
    )
    accountsListResponse: AccountsReply = mainStub.AccountsList(accountsRequest)
    if accountsListResponse.error.message:
        print(accountsListResponse.error.message)
        exit()
    for accountId in accountsListResponse.result:
        print('AccountID:', accountId)
        accountDetailsRequest: AccountDetailsRequest = AccountDetailsRequest(
            login=accountId,
            id=token
        )
        accountDetailsResponse: AccountDetailsReply = mainStub.AccountDetails(accountDetailsRequest)
        if accountDetailsResponse.error.message:
            print(accountDetailsResponse.error.message)
            exit()
        account: Account = accountDetailsResponse.result
        print('Account login:', account.Login)
        print('Account balance:', account.Balance)
        print('Account profit:', account.Profit)
except Exception as exc:
    print(exc)
    exit()

Leave a Reply

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