Python / MT5, how to close half a position?

 

Hello,

I am opening an Buy order with:

   price = mt5.symbol_info_tick(symbol_for_strategy).ask
   stop_loss = price - price/2
   take_profit = price + price/2
   request = {
        "action": MetaTrader5.TRADE_ACTION_DEAL,
        "symbol": "BTCUSD",
        "volume": 0.01,
        "type": mt5.ORDER_TYPE_BUY,
        "price": round(price, 3),
        "sl": round(stop_loss, 3),
        "tp": round(take_profit, 3),
        "type_filling": MetaTrader5.ORDER_FILLING_IOC,
        "type_time": MetaTrader5.ORDER_TIME_GTC,
        "comment": "JustBought"
    }

I receive this response:

OrderSendResult(retcode=10009, deal=32053541, order=36382142, volume=0.01, price=27387.84, bid=0.0, ask=0.0, comment='Request executed', request_id=185019985, retcode_external=0, request=TradeRequest(action=1, magic=0, order=0, symbol='BTCUSD', volume=0.01, price=27387.84, stoplimit=0.0, sl=13693.92, tp=41081.76, deviation=0, type=0, type_filling=1, type_time=0, expiration=0, comment='JustBought', position=0, position_by=0))

Then, I am sending a close order with:

request = {
    "action": MetaTrader5.TRADE_ACTION_CLOSE_BY,
    "position": 36382142,
    "symbol": "BTCUSD",
    "volume": 0.005,
}


Where "position" is the number of the buy order...

And I receive an error:

OrderSendResult(retcode=10013, deal=0, order=0, volume=0.0, price=0.0, bid=0.0, ask=0.0, comment='Invalid request', request_id=0, retcode_external=0, request=TradeRequest(action=10, magic=0, order=0, symbol='BTCUSD', volume=0.005, price=0.0, stoplimit=0.0, sl=0.0, tp=0.0, deviation=0, type=0, type_filling=0, type_time=0, expiration=0, comment='', position=36382142, position_by=0))


Any idea why I am getting this "Invalid request" error ?


Thanks

 

Just look at ..\Include\Trade\Trade.mqh how it closes partially a position, one for hedge, oner for netting accounts:

   bool              PositionClosePartial(const string symbol,const double volume,const ulong deviation=ULONG_MAX);
   bool              PositionClosePartial(const ulong ticket,const double volume,const ulong deviation=ULONG_MAX);
 
Thanks, but could you tell please where is this file ? I am using metatrader5 on windows 10
 
AYMERIC75 #:
Thanks, but could you tell please where is this file ? I am using metatrader5 on windows 10

..\Include\Trade\Trade.mqh

What file ?

 
I mean, where is this file in my system ? If I go in C:\Program Files\MetaTrader 5 I don't see such path
 
AYMERIC75 #:
I mean, where is this file in my system ? If I go in C:\Program Files\MetaTrader 5 I don't see such path

It's a sub-folder of MQL5 data folder.

Forum on trading, automated trading systems and testing trading strategies

Invalid License for all EA (even free ones)

Miguel Angel Vico Alba, 2023.04.26 20:50

Do a Crtl+Shift+D on the terminal or go to the 'File\Open Data Folder' ...

open


 
Thanks a lot!
 

I have tried to replicate what is on this Trade.mqh file:

    request = {

        "type" : MetaTrader5.ORDER_TYPE_SELL,
        "price" : MetaTrader5.symbol_info_tick(symbol).bid,
        "action" : MetaTrader5.TRADE_ACTION_DEAL,
        "symbol" : symbol,
        "volume" : 0.005,
        "position" : position_ticket,


    }

But I get "Unsupported filling mode"


Then I tried, to add

"type_filling": MetaTrader5.ORDER_FILLING_RETURN, also with ORDER_FILLING_IOC and ORDER_FILLING_FOK but still the same
 
AYMERIC75 #:

I have tried to replicate what is on this Trade.mqh file:

But I get "Unsupported filling mode"


Then I tried, to add

"type_filling": MetaTrader5.ORDER_FILLING_RETURN, also with ORDER_FILLING_IOC and ORDER_FILLING_FOK but still the same

?? Can you read? In my version of e.g.:

//+------------------------------------------------------------------+
//| Partial close specified opened position (for hedging mode only)  |
//+------------------------------------------------------------------+
bool CTrade::PositionClosePartial(const string symbol,const double volume,const ulong deviation)

I read:

...
//--- check volume
   double position_volume=PositionGetDouble(POSITION_VOLUME);
   if(position_volume>volume)
      position_volume=volume;
//--- setting request
   m_request.action   =TRADE_ACTION_DEAL;
   m_request.symbol   =symbol;
   m_request.volume   =position_volume;
   m_request.magic    =m_magic;
   m_request.deviation=(deviation==ULONG_MAX) ? m_deviation : deviation;
   m_request.position =PositionGetInteger(POSITION_TICKET);
...
 
the only difference with mine is request.magic and request.deviation that re added, I have tried to add deviation but still the same, magic is just a ref to the strategy, anyway I will try to add it too
 
same, not working