[Python]: Error when creating basic buying EA

 

Hi everyone, 

I am a beginner trying to learn to python script within MetaEditor (MetaTrader5) and I am hoping someone is willing to help me:

To start off I want to build a bot which instantly executes a market order when activated. I should be able to fill in the following parameters: Desired pair, lotsize, sl size (in pips) and tp size (in pips). Until now I came up with this:

import MetaTrader5 as mt5

# Connect to MetaTrader 5 terminal
if not mt5.initialize():
    print("Failed to initialize MT5")
    mt5.shutdown()
    quit()

# Set your desired parameters
symbol = "EURUSD"  # Desired currency pair
lot_size = 0.01  # Lot size
stop_loss = 50  # Stop loss size in pips
take_profit = 100  # Take profit size in pips

# Check if the symbol is valid
if not mt5.symbol_select(symbol):
    print(f"Symbol '{symbol}' is not available")
    mt5.shutdown()
    quit()

# Get the current ask price
current_price = mt5.symbol_info_tick(symbol).ask

# Calculate stop loss and take profit levels based on current price
stop_loss_price = current_price - stop_loss * mt5.symbol_info(symbol).point
take_profit_price = current_price + take_profit * mt5.symbol_info(symbol).point

# Prepare the trade request
request = mt5.TradeRequest()
request.action = mt5.TRADE_ACTION_DEAL
request.symbol = symbol
request.volume = lot_size
request.type = mt5.ORDER_TYPE_BUY
request.price = current_price
request.sl = stop_loss_price
request.tp = take_profit_price

# Send the trade request
result = mt5.order_send(request)

# Check if the trade request was successful
if result.retcode == mt5.TRADE_RETCODE_DONE:
    print("Trade request successful")
else:
    print("Trade request failed with error code: {result.retcode}")

# Close MetaTrader 5 terminal connection
mt5.shutdown()
Unfortunately this gives this error: TypeError: structseq() missing required argument 'sequence' (pos 1). I searched the whole day for fixing this but I am really stuck. Hopefully you guys can help me with this and the rest of the code (I am sure there are several more errors in the code). 

Many thanks in advance!!!

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
request = mt5.TradeRequest()

Where did you see you can use such syntax ?

Replace it with :

# Prepare the trade request
request = {
    "action": mt5.TRADE_ACTION_DEAL,
    "symbol": symbol,
    "volume": lot_size,
    "type": mt5.ORDER_TYPE_BUY,
    "price": current_price,
    "sl": stop_loss_price,
    "tp": take_profit_price
}

I didn't check further...

 
Alain Verleyen #:

Where did you see you can use such syntax ?

Replace it with :

I didn't check further...

Thanks Alain! This fixed the error and I can succesfully compile the code now. However, when using the bot it gives the following error: 

Failed market buy 0.01 EURUSD sl: 1.10441 tp: 1.10591 [Unsupported filling mode].

The code now is: 

import MetaTrader5 as mt5

# Connect to MetaTrader 5 terminal
if not mt5.initialize():
    print("Failed to initialize MT5")
    mt5.shutdown()
    quit()

# Set your desired parameters
symbol = "EURUSD"  # Desired currency pair
lot_size = 0.01  # Lot size
stop_loss = 50  # Stop loss size in pips
take_profit = 100  # Take profit size in pips

# Check if the symbol is valid
if not mt5.symbol_select(symbol):
    print(f"Symbol '{symbol}' is not available")
    mt5.shutdown()
    quit()

# Get the current ask price
current_price = mt5.symbol_info_tick(symbol).ask

# Calculate stop loss and take profit levels based on current price
stop_loss_price = current_price - stop_loss * mt5.symbol_info(symbol).point
take_profit_price = current_price + take_profit * mt5.symbol_info(symbol).point

# Prepare the trade request
request = {
    "action": mt5.TRADE_ACTION_DEAL,
    "symbol": symbol,
    "volume": lot_size,
    "type": mt5.ORDER_TYPE_BUY,
    "price": current_price,
    "sl": stop_loss_price,
    "tp": take_profit_price
}

# Send the trade request
result = mt5.order_send(request)

# Check if the trade request was successful
if result.retcode == mt5.TRADE_RETCODE_DONE:
    print("Trade request successful")
else:
    print("Trade request failed with error code: {result.retcode}")

# Close MetaTrader 5 terminal connection
mt5.shutdown()
Why does it give this error and does someone have a solution for this?