Error Message "Order Closed" When Modifying Orders Using Python

 
Hello everyone. Currently, I'm attempting to manipulate MT5 using Python. I first place an order without setting any stop-loss and take-profit conditions. After a period of time, I then set the stop-loss and take-profit prices. However, I'm encountering the following error message: "order_send failed, retcode=10036," indicating that the order I'm trying to modify has already been closed. However, upon inspection, I confirm that the order I intend to modify still exists. Can anyone shed light on what might be causing this issue and suggest potential solutions? Thank you for your assistance. Below is my code.


                price = mt5.symbol_info_tick(symbol).ask

                request = {
                "action": mt5.TRADE_ACTION_DEAL,
                "symbol": symbol,
                "volume": ld_8,
                "type": mt5.ORDER_TYPE_BUY,
                "price": price,
                "deviation": slippage,
                "magic": MagicNumber,
                "comment":  EA_Name + ls_32,
                "type_filling": find_filling_mode(symbol),
                "type_time": mt5.ORDER_TIME_GTC}

                li_4 = mt5.order_send(request)

                if li_4.retcode != mt5.TRADE_RETCODE_DONE:
                    print("order_send failed, retcode={}".format(li_4.retcode))
                    return
                else:
                    time.sleep(10) 
                    if li_4.order >= 0:
                        orders = mt5.positions_get(symbol=symbol)
                        empty = ()
                        if orders == empty:
                            print("No orders EURUSD, error code={}".format(mt5.last_error()))
                        else:
                            orders_df = pd.DataFrame(list(orders),columns=orders[0]._asdict().keys())
                            orders_df['time'] = pd.to_datetime(orders_df['time'], unit='s')
                            orders_df['time_msc'] = pd.to_datetime(orders_df['time_msc'], unit='ms')
                            orders_df['time_update'] = pd.to_datetime(orders_df['time_update'], unit='s')
                            orders_df['time_update_msc'] = pd.to_datetime(orders_df['time_update_msc'], unit='ms')
                            point = mt5.symbol_info(symbol).point
                            price = mt5.symbol_info_tick(symbol).ask

                            tp = float(price + 100 * point)

                            sl = float(price - 100 * point)


                            position = int(orders_df.loc[len(orders_df) - 1, 'ticket'])
                            volume = float(orders_df.loc[len(orders_df) - 1, 'volume'])

                            print('position: ', position)
                            print('sl: ', sl)
                            print('tp: ', tp)
                            print('volume: ', volume)

                            request = {
                            "action": mt5.TRADE_ACTION_SLTP,
                            "symbol": symbol,
                            "ticket": position,
                            "volume": volume,
                            "sl": sl,
                            "tp":  tp,
                            "magic": MagicNumber,
                            "comment": EA_Name + 'Modify',
                            "type_time": mt5.ORDER_TIME_GTC,
                            "type_filling": find_filling_mode(symbol)}

                            li_4 = mt5.order_send(request)

                            if li_4 is None:
                                print('order send failed!!!')
                            elif li_4.retcode != mt5.TRADE_RETCODE_DONE:
                                print("order_send failed, retcode={}".format(li_4.retcode))
                            else:
                                print('Modify OK!!!')   


Print:

order_send failed, retcode=10036

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
Reason: