How to close a position from a webhook

 

Hi,

I can't seem to figure out how to add the ability to close a trade to my bot. I am using python. Here it shows SL and TP https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py

Which I am using, but I cannot see anything related to closing a trade.

Right now my bot receives a JSON message containing the information it needs to send to MT5. It looks something like this: "signal": "LONG", "symbol": "GBPUSD", "volume": 0.1, "stop_loss": 10, "take_profit": 10

I should be able to create another alert which closes a trade based on a condition but how do I set that up?

In the bot the code is like this

point = mt5.symbol_info(symbol).point
price = mt5.symbol_info_tick(symbol).ask
deviation = 20
request = {
    "action"mt5.TRADE_ACTION_DEAL,
    "symbol"symbol,
    "volume"lot,
    "type"mt5.ORDER_TYPE_BUY,
    "price"price,
    "sl"price - 100 * point,
    "tp"price + 100 * point,
    "deviation"deviation,
    "magic"234000,
    "comment""python script open",
    "type_time"mt5.ORDER_TIME_GTC,
    "type_filling"mt5.ORDER_FILLING_RETURN,


This is how you set up sl and tp so that it works with the above JSON message.
So I will need to add the closing code so that it can accept a JSON message with a close request.

Any ideas?
Documentation on MQL5: Integration / MetaTrader for Python / order_send
Documentation on MQL5: Integration / MetaTrader for Python / order_send
  • www.mql5.com
order_send - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Dean-Trades: I can't seem to figure out how to add the ability to close a trade to my bot. I am using python.

You close a position by reversing it. I don't use Python, so here is the equivalent logic in MQL5.

Forum on trading, automated trading systems and testing trading strategies

How do I close a position without CTrade?

Fernando Carreiro, 2022.07.19 03:57

In MT5, closing a position is done by a trade deal in the opposite direction in reference to an open position. Here is a simple example (NB! position must be previously selected) ...

// Close selected position
   bool PositionClose( void )
   {
      MqlTradeRequest stCloseRequest = {};
      
      switch( (ENUM_POSITION_TYPE) PositionGetInteger( POSITION_TYPE ) )
      {
         case POSITION_TYPE_BUY:
            stCloseRequest.type  = ORDER_TYPE_SELL;
            stCloseRequest.price = SymbolInfoDouble( _Symbol, SYMBOL_BID );
            break;
         case POSITION_TYPE_SELL:
            stCloseRequest.type  = ORDER_TYPE_BUY;
            stCloseRequest.price = SymbolInfoDouble( _Symbol, SYMBOL_ASK );
            break;
         default:
            return false;
      };

      stCloseRequest.action       = TRADE_ACTION_DEAL;
      stCloseRequest.position     = PositionGetInteger( POSITION_TICKET );
      stCloseRequest.volume       = PositionGetDouble(  POSITION_VOLUME );
      stCloseRequest.symbol       = _Symbol;
      stCloseRequest.magic        = i_nMagicNumber;
      stCloseRequest.deviation    = i_nMaxDeviation;
      stCloseRequest.type_filling = g_eOrderFillType;

      MqlTradeResult stCloseResult = {};
      if( !OrderSend( stCloseRequest, stCloseResult ) )
      {
         PrintFormat( "Error %d closing selected position with return code %d (%s)",
            _LastError, stCloseResult.retcode, stCloseResult.comment );
         return false;
      };

      return true;
   };

Forum on trading, automated trading systems and testing trading strategies

How do I close a position without CTrade?

Fernando Carreiro, 2022.07.20 12:57

Select the position by ticket first, then you can call the the function in my example.

PositionSelectByTicket( nTicket );
PositionClose();
Reason: