test code with a function that works. some checks are missing so you shouldnt use this out of the box. hope it helps someone anyway. Its not elegant but does the job
import MetaTrader5 as mt5 from datetime import datetime # Initialize MetaTrader 5 def initialize_mt5(): if not mt5.initialize(): print(f"initialize() failed, error code = {mt5.last_error()}") return False print("MetaTrader 5 initialized.") return True # Function to calculate lot size using MetaTrader 5 values def calculate_lot_size(currency, signal_type, stop_loss, entry_price, risk_money): symbol_info = mt5.symbol_info(currency) if symbol_info is None: print(f"Symbol {currency} not found.") return 0 volume_step = symbol_info.volume_step max_volume = symbol_info.volume_max min_volume = symbol_info.volume_min lot_size = min_volume while lot_size <= max_volume: if signal_type == "LONG": potential_loss = mt5.order_calc_profit(mt5.ORDER_TYPE_BUY, currency, lot_size, entry_price, stop_loss) else: potential_loss = mt5.order_calc_profit(mt5.ORDER_TYPE_SELL, currency, lot_size, entry_price, stop_loss) print(f"Lot Size: {lot_size}, Potential Loss: {potential_loss}, Risk Money: {risk_money}") if abs(potential_loss) >= risk_money: print(f"Calculated lot size: {lot_size}") return lot_size lot_size = round(lot_size + volume_step, 2) print("No valid lot size found within the specified risk.") return 0 # Mock function to simulate logging def log_to_trade_log(message): print(message) # Test the calculate_lot_size function def test_calculate_lot_size(): currency = "EURUSD" risk_money = 100 # Risk amount in dollars # Test long trade signal_type = "LONG" entry_price = 1.2345 stop_loss = 1.2245 # Adjusted stop loss to increase potential loss lot_size = calculate_lot_size(currency, signal_type, stop_loss, entry_price, risk_money) print(f"Test Long Trade: Lot Size = {lot_size}") # Test short trade signal_type = "SHORT" entry_price = 1.2340 stop_loss = 1.2440 # Adjusted stop loss to increase potential loss lot_size = calculate_lot_size(currency, signal_type, stop_loss, entry_price, risk_money) print(f"Test Short Trade: Lot Size = {lot_size}") if __name__ == "__main__": # Initialize MetaTrader 5 if initialize_mt5(): test_calculate_lot_size() mt5.shutdown()
Hello and thanks for reading.
edit : it seems i have posted in the wrong section being tired. i am so sorry about this.
First I must apologize if this has been answered somewhere else. I've tried looking here and with google , but everything I found was either to complicated for me to understand , or incomplete, or in mql4 or 5. I am terribly sorry if this is a duplicate. Being frustrated and tired by this , it is obviously easier for me to ask. If there is a kind soul that could point me in the right direction with a workflow , point me to a nice resource or show me the correct way to do this , I would be extremely grateful.
The main problem I have is that my broker returns tick_size=1e-05 , and I have no idea how to round the tps and sls to the correct tick_size , nor calculate the volume for the trade correctly. Ive read about deltaPerLot but i didnt understand how to use it.I'm trying to fetch the symbol properties and calculate the lot size with a risk in money, however I cannot find the correct way to do it so it works on any instrument.
This is my code :
Again , apologies for being a lazy bastard and a half brain.
and thank you very much in advance for any help you could provide.
Wishing you a great day and a great life.