How to close an mt5 order/position with python?

Marius-eduard Zeca  

I want to create a function to open an order and one to close the pending order or the position.

No problem with opening the position/order, but the issue is with the closing function.

import time
import MetaTrader5 as mt5

# establish connection to the MetaTrader 5 terminal
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()

# prepare the buy request structure
symbol = "EURUSD"
lot = 0.1
deviation = 200
point = mt5.symbol_info(symbol).point
magic = 123456
type_time = mt5.ORDER_TIME_GTC
type_filling =  mt5.ORDER_FILLING_IOC

buy_price = mt5.symbol_info_tick(symbol).ask
buy_sl = buy_price - 100 * point
buy_tp = buy_price + 100 * point

# create a buy request
def open_buy_position():   
    request = {
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": symbol,
        "volume": lot,
        "type": mt5.ORDER_TYPE_BUY,
        "price": buy_price,
        "sl": buy_sl,
        "tp": buy_tp,
        "deviation": deviation,
        "magic": magic,
        "comment": "python script open",
        "type_time": type_time,
        "type_filling":type_filling,
    }
   
    # send a trading request
    result = mt5.order_send(request)

# create a close request
def close_buy():
    position_id=result.order
    price=mt5.symbol_info_tick(symbol).bid
    request={
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": symbol,
        "volume": lot,
        "type": mt5.ORDER_TYPE_SELL,
        "position": position_id,
        "price": buy_price,
        "deviation": deviation,
        "magic": magic,
        "comment": "python script close",
        "type_time": mt5.ORDER_TIME_GTC,
        "type_filling": mt5.ORDER_FILLING_RETURN,
    }
    # send a trading request
    result=mt5.order_send(request) 

#open_buy_position()
close_buy()

I will get this error

> UnboundLocalError: local variable 'result' referenced before assignment.


Even if I set `result` as global, it will do nothing.


Any help would be welcomed!


Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Dominik Egert  
mieluconara:

I want to create a function to open an order and one to close the pending order or the position.

No problem with opening the position/order, but the issue is with the closing function.

I will get this error

> UnboundLocalError: local variable 'result' referenced before assignment.


Even if I set `result` as global, it will do nothing.


Any help would be welcomed!


Although I dont code python, you are using a variable without it being assigned a value before.

Your code says:

position_id=result.order

in the beginning of the function. But what is supposed to be in the variable result.order ?

Maybe I am wrong here, but it seems to me, this value is missing at that point of time?

It could be, your ticket number is placed in result.deal instead of result.order.

Marius-eduard Zeca  
Dominik Egert:

Although I dont code python, you are using a variable without it being assigned a value before.

Your code says:

in the beginning of the function. But what is supposed to be in the variable result.order ?

Maybe I am wrong here, but it seems to me, this value is missing at that point of time?

It could be, your ticket number is placed in result.deal instead of result.order.

Well...that's my problem and I don't understand how to fix it

Reason: