Help Needed: Unable to Create User Account with MT5Manager in Python

 

Hello everyone,

I’m trying to create a new user account on the MT5 server using the following Python code, but it’s not working as expected. The connection to the server seems to succeed, but the user creation process either fails or does not return the expected result.

Here is the full code I’m using:

import logging
from MT5Manager import ManagerAPI

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

MT5_SERVER_ADDRESS = "***"
MT5_LOGIN_ID = 1011
MT5_PASSWORD = "***"

def connect_to_mt5():
    """Connect to the MT5 server."""
    manager = ManagerAPI()
    try:
        connected = manager.Connect(
            MT5_SERVER_ADDRESS,
            MT5_LOGIN_ID,
            MT5_PASSWORD,
            ManagerAPI.EnPumpModes.PUMP_MODE_FULL,
            1000000
        )
        if connected:
            logger.info("Connected to MT5 server.")
            return manager
        else:
            logger.error("Failed to connect to MT5 server.")
            return None
    except Exception as e:
        logger.exception("Exception during connection to MT5 server")
        return None

def create_user(manager, user_info):
    """Create a user on the MT5 server."""
    try:
        user_request = {
            "Login": 0,
            "Group": user_info['group'],
            "Name": user_info['name'],
            "Password": user_info['pass_main'],
            "PasswordInvestor": user_info['pass_investor'],
            "EMail": user_info.get('email', 'default@example.com'),
            "Phone": user_info.get('phone', ''),
            "Country": "website",
            "City": "website",
            "State": "website",
            "ZIPCode": "1470",
            "Leverage": user_info.get('leverage', 100),
            "Language": 0,
            "Rights": 0,
        }
        logger.debug(f"User request data: {user_request}")
        result = manager.UserAdd(user_request)
        if result == 0:
            logger.info("User creation request successful.")
        else:
            logger.error(f"UserAdd failed with result: {result}")
            return None

        logins = manager.UserLogins()
        if logins:
            for login in logins:
                user = manager.UserGet({"Login": login})
                if user and user.get("EMail") == user_request["EMail"]:
                    logger.info(f"User retrieved successfully with Login ID: {user['Login']}")
                    return user['Login']
            logger.error("Failed to find the created user via UserLogins.")
        else:
            logger.error("UserLogins failed to retrieve any users.")
        return None
    except Exception as e:
        logger.exception("Exception during user creation")
        return None

if __name__ == "__main__":
    user_info = {
        "name": "omid",
        "group": "1",
        "pass_main": "Password123",
        "pass_investor": "forex-hedge",
        "email": "user@example.com",
        "phone": "1234567890",
        "leverage": 100
    }

    manager = connect_to_mt5()
    if manager:
        login_id = create_user(manager, user_info)
        if login_id:
            logger.info(f"User created successfully with Login ID: {login_id}")
        else:
            logger.error("Failed to create user.")
        manager.Disconnect()
    else:
        logger.error("Could not establish a connection to the MT5 server.")

I’d greatly appreciate it if anyone could help identify what’s wrong or suggest any improvements. Is there something I’m missing in the user creation process?

Thank you in advance for your help!

 

Please note: traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members of this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.11.18
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Omid Goudarzi:

Hello everyone,

I’m trying to create a new user account on the MT5 server using the following Python code, but it’s not working as expected. The connection to the server seems to succeed, but the user creation process either fails or does not return the expected result.

Here is the full code I’m using:


I’d greatly appreciate it if anyone could help identify what’s wrong or suggest any improvements. Is there something I’m missing in the user creation process?

Thank you in advance for your help!

Some brokers only allow you to create accounts through their website interface. Make sure you can manually create an account.