Error 10016 (invalid stops) when placing order in python mt5

 

Hello,

That is my first topic on this forum. I need a solution to my problem.

I am using python to place order on a list of mt5 accounts. But on some account I have the error 10016 that mean Invalid stops. What i don't understant is that the same order run without problem on others accounts.

I noticed that it happen rondomly, not always on the same accounts.

That is my function to run a position:

# Function to place a trade on MT5
def place_order(order, symbol, volume, sl, tp, comment, price=0, deviation=10):
    try:
        request = {
            "symbol": symbol,
            "volume": volume,
            "magic": magic_number,
            "comment": "Position run by python bot",
            "deviation" : deviation,
            "type_time": MetaTrader5.ORDER_TIME_GTC, 
            "type_filling": MetaTrader5.ORDER_FILLING_FOK, # ORDER_FILLING_IOC
            "sl": sl,
            "tp": tp
        }
        if order == "BUY":
            request["type"] = MetaTrader5.ORDER_TYPE_BUY
            request["price"] = MetaTrader5.symbol_info_tick(symbol).ask
            request["action"] = MetaTrader5.TRADE_ACTION_DEAL
        if order == "SELL":
            request["type"] = MetaTrader5.ORDER_TYPE_SELL
            request["price"] = MetaTrader5.symbol_info_tick(symbol).bid
            request["action"] = MetaTrader5.TRADE_ACTION_DEAL
        if order == "BUY_STOP":
            request["type"] = MetaTrader5.ORDER_TYPE_BUY_STOP
            request["price"] = price
            request["action"] = MetaTrader5.TRADE_ACTION_PENDING
        if order == "BUY_LIMIT":
            request["type"] = MetaTrader5.ORDER_TYPE_BUY_LIMIT
            request["price"] = price
            request["action"] = MetaTrader5.TRADE_ACTION_PENDING
        if order == "SELL_STOP":
            request["type"] = MetaTrader5.ORDER_TYPE_SELL_STOP
            request["price"] = price
            request["action"] = MetaTrader5.TRADE_ACTION_PENDING
        if order == "SELL_LIMIT":
            request["type"] = MetaTrader5.ORDER_TYPE_SELL_LIMIT
            request["price"] = price
            request["action"] = MetaTrader5.TRADE_ACTION_PENDING
    except Exception as e:
        print(e)
        return False, 0

    # Send the order to MT5
    order_result = MetaTrader5.order_send(request)
    
    if not order_result:
        print("order failed on this account, error code = ", MetaTrader5.last_error())
        return False, order_result

    # Notify based on return outcomes
    if order_result[0] == 10009:
        print(order_result)
        #print("Order for EURUSD successful") # Enable if error checking order_result
        #return order_result[2]
        return True, order_result
    else:
        # Print result
        print(f"Error placing order. ErrorCode {order_result[0]}, Error Details: {order_result}")
        #raise exceptions.MetaTraderOrderPlacingError
        return False, order_result


The error happen for the orders BUY and SELL.


And that is a result for a position:

3
Connexion au compte mt5 de Ariel Mawete en cours...
Trading Bot Logged in and Ready to Go!
Connecté au compte mt5 de Ariel Mawete avec succès
OrderSendResult(retcode=10009, deal=2887363911, order=2863687055, volume=0.005, price=358615.74, bid=358530.74, ask=358615.74, comment='Request executed', request_id=3356531804, retcode_external=0, request=TradeRequest(action=1, magic=100077, order=0, symbol='Volatility 75 Index', volume=0.005, price=358615.74, stoplimit=0.0, sl=340000.0, tp=360000.0, deviation=10, type=0, type_filling=0, type_time=0, expiration=0, comment='Position run by python bot', position=0, position_by=0))
Position lancée avec succès sur le compte de Ariel Mawete
OrderSendResult(retcode=10009, deal=2887363911, order=2863687055, volume=0.005, price=358615.74, bid=358530.74, ask=358615.74, comment='Request executed', request_id=3356531804, retcode_external=0, request=TradeRequest(action=1, magic=100077, order=0, symbol='Volatility 75 Index', volume=0.005, price=358615.74, stoplimit=0.0, sl=340000.0, tp=360000.0, deviation=10, type=0, type_filling=0, type_time=0, expiration=0, comment='Position run by python bot', position=0, position_by=0))

4
Connexion au compte mt5 de Lupence N en cours...
Trading Bot Logged in and Ready to Go!
Connecté au compte mt5 de Lupence N avec succès
Error placing order. ErrorCode 10016, Error Details: OrderSendResult(retcode=10016, deal=0, order=0, volume=0.0, price=0.0, bid=0.0, ask=0.0, comment='Invalid stops', request_id=0, retcode_external=0, request=TradeRequest(action=1, magic=100077, order=0, symbol='Volatility 75 Index', volume=0.005, price=374429.17, stoplimit=0.0, sl=340000.0, tp=360000.0, deviation=10, type=0, type_filling=0, type_time=0, expiration=0, comment='Position run by python bot', position=0, position_by=0))
Impossible d'exécuter cet position sur le compte de Lupence N pour la raison suivante: Invalid stops

5
Connexion au compte mt5 de Amegan Derrick en cours...
Trading Bot Logged in and Ready to Go!
Connecté au compte mt5 de Amegan Derrick avec succès
OrderSendResult(retcode=10009, deal=2887363981, order=2863687126, volume=0.005, price=358688.81, bid=358603.81, ask=358688.81, comment='Request executed', request_id=3356531805, retcode_external=0, request=TradeRequest(action=1, magic=100077, order=0, symbol='Volatility 75 Index', volume=0.005, price=358688.81, stoplimit=0.0, sl=340000.0, tp=360000.0, deviation=10, type=0, type_filling=0, type_time=0, expiration=0, comment='Position run by python bot', position=0, position_by=0))
Position lancée avec succès sur le compte de Amegan Derrick
OrderSendResult(retcode=10009, deal=2887363981, order=2863687126, volume=0.005, price=358688.81, bid=358603.81, ask=358688.81, comment='Request executed', request_id=3356531805, retcode_external=0, request=TradeRequest(action=1, magic=100077, order=0, symbol='Volatility 75 Index', volume=0.005, price=358688.81, stoplimit=0.0, sl=340000.0, tp=360000.0, deviation=10, type=0, type_filling=0, type_time=0, expiration=0, comment='Position run by python bot', position=0, position_by=0))


It is the same position on that tree accounts, but the account number 3 return Invalid stops

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Account Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Hello,

Somebody can help me ??

Reason: