MT4 Manager gRPC example for Python
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 mt4mng.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=. ./mt4mng.proto
Add your credentials in the script:
Now we are ready to run the script example.
The script
from mt4mng_pb2 import *
from mt4mng_pb2_grpc import *
channel = grpc.secure_channel('mng4grpc.mtapi.io:443', grpc.ssl_channel_credentials())
mainController = MainControllerStub(channel)
# TODO add your credentials here
connectionRequest = ConnectRequest(
user=1,
password='',
server="")
try:
connectionResult = mainController.Connect(connectionRequest)
if connectionResult.error.message:
print(connectionResult.error.message)
exit()
token = connectionResult.result
print(token)
except Exception as exc:
print(exc)
exit()
try:
accountsSummaryRequest = AccountsSummaryRequest(
id=token
)
accountsListResponse = mainController.AccountsList(accountsSummaryRequest)
if accountsListResponse.error.message:
print(accountsListResponse.error.message)
exit()
for accountId in accountsListResponse.result:
print('AccountID:', accountId)
accountDetailsResponse = mainController.AccountDetails(AccountDetailsRequest(
id=token,
login=accountId
))
if accountDetailsResponse.error.message:
print(accountDetailsResponse.error.message)
exit()
accountDetails = accountDetailsResponse.result
print('Account name:', accountDetails.Name)
except Exception as exc:
print(exc)
exit()