Metatrader5 lot_size computation with stop_loss as price

 
I am trying to compute lot size having stop loss (as price) but the lot size ends up quite big and eventually, I get an error:

Error code: 10014 Error message: Invalid volume

My current code is:

import MetaTrader5 as mt5

def calculate_lot_size(symbol, stop_loss_price):
    # Connect to the MetaTrader 5 terminal
    if not mt5.initialize():
        print("Failed to initialize MetaTrader 5")
        return None

    # Get the current account balance
    account_info = mt5.account_info()
    if account_info is None:
        print("Failed to retrieve account information")
        mt5.shutdown()
        return None

    account_balance = account_info.balance

    # Get the symbol's point value (minimum price change)
    symbol_info = mt5.symbol_info(symbol)
    if symbol_info is None:
        print(f"Failed to retrieve symbol information for {symbol}")
        mt5.shutdown()
        return None

    point_value = symbol_info.point

    # Calculate the maximum acceptable loss based on the account balance and stop loss price
    max_loss = account_balance * (symbol_info.last / stop_loss_price - 1)

    # Calculate the lot size based on the maximum acceptable loss and the point value
    lot_size = max_loss / point_value

    # Print the calculated lot size
    print(f"Lot size for {symbol}: {lot_size} lots")

    # Disconnect from the MetaTrader 5 terminal
    mt5.shutdown()

    return lot_size

# Example usage
symbol = "EURUSD"
stop_loss_price = 1.2000  # Stop loss price
lot_size = calculate_lot_size(symbol, stop_loss_price)

I believe that I am using the latest version of Metatrader5 which is 5.0.45

 
Please don't create topics randomly in any section. It has been moved to the section: Expert Advisors and Automated Trading
 

Also read the following and apply it to your code, even if it is not for a Market product.

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.