Getting retcode TRADE_RETCODE_INVALID_STOPS when sending request

 

Hello,

I am trying to send a buy request using the metatrader python libary:

import time
import MetaTrader5 as mt
LOGIN = ...
PASSWORD = ...
SERVER = "ICMarketsSC-Demo"
symbol = 'BTCUSD'
lot_size = 0.1

if not mt.initialize(login=LOGIN, password=PASSWORD, server=SERVER):
    print("initialize() failed, error code =", mt5.last_error())
    quit()

symbol_info = mt.symbol_info(symbol)
if symbol_info is None:
    print(symbol, "not found, can not call order_check()")
    mt.shutdown()
    quit()

point = mt.symbol_info(symbol).point
price = mt.symbol_info(symbol).ask
deviation = 20
request = {
    "action": mt.TRADE_ACTION_DEAL,
    "symbol": symbol,
    "volume": lot_size,
    "type": mt.ORDER_TYPE_BUY,
    "price": price,
    "sl": price - 5000 * point,
    "tp": price + 5000 * point,
    "deviation": deviation,
    "magic": 234000,
    "comment": "python script open",
    "type_time": mt.ORDER_TIME_GTC,
    "type_filling": mt.ORDER_FILLING_IOC
}
result = mt.order_send(request)

print("1. order_send(): by {} {} lots at {} with deviation={} points".format(symbol,lot_size,price,deviation));
if result.retcode != mt.TRADE_RETCODE_DONE:
    print("2. order_send failed, retcode={}".format(result.retcode))
    # request the result as a dictionary and display it element by element
    result_dict=result._asdict()
    for field in result_dict.keys():
        print("   {}={}".format(field,result_dict[field]))
        # if this is a trading request structure, display it element by element as well
        if field=="request":
            traderequest_dict=result_dict[field]._asdict()
            for tradereq_filed in traderequest_dict:
                print("       traderequest: {}={}".format(tradereq_filed,traderequest_dict[tradereq_filed]))
    print("shutdown() and quit")
    mt.shutdown()
    quit()

Output:

1. order_send(): by BTCUSD 0.1 lots at 56202.67 with deviation=20 points
2. order_send failed, retcode=10016
   retcode=10016
   deal=0
   order=0
   volume=0.0
   price=0.0
   bid=0.0
   ask=0.0
   comment=Invalid stops
   request_id=1
   retcode_external=0
   request=TradeRequest(action=1, magic=234000, order=0, symbol='BTCUSD', volume=0.1, price=56202.67, stoplimit=0.0, sl=56152.67, tp=56252.67, deviation=20, type=0, type_filling=1, type_time=0, expiration=0, comment='python script open', position=0, position_by=0)
       traderequest: action=1
       traderequest: magic=234000
       traderequest: order=0
       traderequest: symbol=BTCUSD
       traderequest: volume=0.1
       traderequest: price=56202.67
       traderequest: stoplimit=0.0
       traderequest: sl=56152.67
       traderequest: tp=56252.67
       traderequest: deviation=20
       traderequest: type=0
       traderequest: type_filling=1
       traderequest: type_time=0
       traderequest: expiration=0
       traderequest: comment=python script open
       traderequest: position=0
       traderequest: position_by=0
shutdown() and quit

Now as you can see, I get the error 'invalid stops' but if I take a look at the outputs I get

(sl = 56152.67) < (open = 56202.67) < (tp = 56252.67)

which shouldn't be a problem, anyways it does not work

Reason: