'Changing stoploss in metatrader5 with python using Metatrader5 library and nothing happens

I have this code for changing stoploss on opened order/orders in metatrader 5. When I run this code nothing happens even when my compiler print that, nothing is wrong. I have algotrading on so I'm not sure where is the problem.

Here is source code:

def sl_change(ticket, SL, TP, pair, p_open, volume, o_type):

    order_request = {
        'action': mt5.TRADE_ACTION_SLTP,
        'ticket': ticket,
        'type': o_type,
        'price_open': p_open,
        'volume': volume,
        'sl': SL,
        'tp': TP,
        'symbol': pair,
        'deviation': 20,
        "magic": ea_magic_number,
        "comment": "sent by python",
        "type_time": mt5.ORDER_TIME_GTC,  # good till cancelled
        'type_filling': mt5.ORDER_FILLING_FOK,
        "type_filling": mt5.ORDER_FILLING_RETURN,
    }
    result = mt5.order_check(order_request)
    return result, order_request

pair = 'AUDUSD'
SL = 0.7101
positions = mt5.positions_get(symbol=pair)

ordernum = len(positions)

for i in range(0, ordernum):
    position = positions[i]
    ticket = position.ticket
    TP = position.tp
    volume = position.volume
    o_type = position.type
    p_open = position.price_open
    print(positions)
    time.sleep(5)
    sl_change(ticket, SL, TP, pair, p_open, volume, o_type)

When I replace order_check with order_send still nothing happens.



Solution 1:[1]

This is what works for me now it's sample code if you don't understand input answer me I can give you more info

def changeslpl(ticket,pair,pos_type,SL,tp,ea_magic_number,volume,p_open):
request = {
    "action": mt5.TRADE_ACTION_SLTP,
    "symbol": pair,
    "volume": volume,
    "type": pos_type,
    "position": ticket,
    "price_open": p_open,
    "sl": SL,
    "tp": tp,
    "deviation": 20,
    "magic": ea_magic_number,
    "comment": "python script open",
    "type_time": mt5.ORDER_TIME_GTC,
    "type_filling": mt5.ORDER_FILLING_FOK,
    "ENUM_ORDER_STATE": mt5.ORDER_FILLING_RETURN,
}
#// perform the check and display the result 'as is'
result = mt5.order_send(request)

if result.retcode != mt5.TRADE_RETCODE_DONE:
    print("4. order_send failed, retcode={}".format(result.retcode))

    print(" result",result)

Solution 2:[2]

You should invert your order type. If it is a mt5.ORDER_TYPE_BUY, the SL/TP modification request should be a mt5.ORDER_TYPE_SELL

# This is what is important to you!
if(order_type == mt5.ORDER_TYPE_BUY):
    order_type = mt5.ORDER_TYPE_SELL
    price = mt5.symbol_info_tick(symbol).bid
else:
    order_type = mt5.ORDER_TYPE_BUY
    price = mt5.symbol_info_tick(symbol).ask
#importance ends here.

sltp_request = {
    "action": mt5.TRADE_ACTION_SLTP,
    "symbol": symbol,
    "volume": float(volume),
    "type": order_type,
    "position": deal_id,
    "sl": sl,
    "price": price,
    "magic": 234000,
    "comment": "Change stop loss",
    "type_time": mt5.ORDER_TIME_GTC,
    "type_filling": mt5.ORDER_FILLING_IOC,
}

result = mt5.order_send(sltp_request)

Solution 3:[3]

I found out why we were facing this problem.

When you execute a trade for the first time note that STOP_LOSS is always equal to for example 500 points difference from price but now you want to modify a stop loss.

So 500 points +/- current_price = e.g. 138.500 == OUR STOP_LOSS

The hack here is that you put price for a STOP_LOSS, NOT POINTS.

So the new request will be:

request = { 'action': mt5.TRADE_ACTION_SLTP, 'position': position.ticket, 'sl': 139.000, }

Now you will be finally on to something.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kurina11
Solution 2 user8549687
Solution 3 richardec