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
from How to compute lot size percentually having a stop loss on python using Metatrader
No comments:
Post a Comment