Trade Failed Due to retcode: 10009, with comment: Request executed

ArthurEzenwanne  

Hello, 

I am writing an EA using the MT5 Python library (https://www.mql5.com/en/docs/integration/python_metatrader5)

I have a working EA, however some of my trades do not close. It usually fails with the comment "Trade Failed Due to retcode: 10009, with comment: Request executed". This usually happens (maybe 1 in 4 tries) when I try close out half of the position.

Has anyone experienced such issues and can I know how it was resolved?

Thanks in advance!

Snippet:

...     
# if trade gets to 50% profitability
# i. close half of the position
price=mt5.symbol_info_tick(instrument).bid          #because i am selling
request={
    "action": mt5.TRADE_ACTION_DEAL,
    "symbol": instrument,
    "volume": (trade.volume/2),                                    #close half of the position
    "type": mt5.ORDER_TYPE_SELL,
    "position": (trade.order),
    "price": price,
    "deviation": slippage,
    "magic": magicnumber,
    "comment": "strategy_close_half",
    "type_time": mt5.ORDER_TIME_GTC,
    "type_filling": mt5.ORDER_FILLING_IOC,
    }
# send a trading request to close out 50% of the current trade
trade_close_half = mt5.order_send(request)
if trade_close_half.retcode == mt5.TRADE_RETCODE_DONE:
    print('closed half of the buy trade due to strategy. letting the other half run to SL or TP')
else:
    # https://www.mql5.com/en/docs/constants/errorswarnings/enum_trade_return_codes
    print('closing half of the buy trade failed due to retcode: {}, with comment: {}'.format(trade.retcode, trade.comment))
    # exit application
    print('Exiting application')
    sys.exit('Execution stopped')
...
ArthurEzenwanne  
Vladimir Karputov #:
Do you update current prices before submitting a trade order?
Yes. In the request I updated the price to market bid price.
Vladimir Karputov  
ArthurEzenwanne # :
Yes. In the request I updated the price to market bid price.

And your 'slippage' parameter is not very small?

ArthurEzenwanne  
Vladimir Karputov #:

And your 'slippage' parameter is not very small?

Well I used 20. Don't know if it's small. Is it?
ArthurEzenwanne  
Vladimir Karputov #:

And your 'slippage' parameter is not very small?

Something I should also add is that I observed it today on EU around 7am/8am UTC. I noticed that there was something like a spike around that time.

So could that be a reason?
Vladimir Karputov  
ArthurEzenwanne # :
Well I used 20. Don't know if it's small. Is it?

Just zoom in 10 times and check the result.

ArthurEzenwanne  
Vladimir Karputov #:

Just zoom in 10 times and check the result.

Found the culprit:

 "volume": (trade.volume/2),                                    #close half of the position

So some of the trade volume returns float values with like 3 or more decimal places. But the volume for my broker is to a max of 2 decimal places.

I've modified it and the problem went away.

Thanks for your help @Valdimir

Vladimir Karputov  
ArthurEzenwanne # :

Found the culprit:

So some of the trade volume returns float values with like 3 or more decimal places. But the volume for my broker is to a max of 2 decimal places.

I've modified it and the problem went away.

Thanks for your help @Valdimir

In MQL5 I use trading classes. In particular, I normalize Stop Loss and Take Profit with CSymbolInfo::NormalizePrice, and for volume normalization I use the following function:

//+------------------------------------------------------------------+
//| Lot Check                                                        |
//+------------------------------------------------------------------+
double LotCheck(double lots,CSymbolInfo &symbol)
  {
//--- calculate maximum volume
   double volume=NormalizeDouble(lots,2);
   double stepvol=symbol.LotsStep();
   if(stepvol>0.0)
      volume=stepvol*MathFloor(volume/stepvol);
//---
   double minvol=symbol.LotsMin();
   if(volume<minvol)
      volume=0.0;
//---
   double maxvol=symbol.LotsMax();
   if(volume>maxvol)
      volume=maxvol;
//---
   return(volume);
  }
Documentation on MQL5: Standard Library / Trade Classes / CSymbolInfo / NormalizePrice
Documentation on MQL5: Standard Library / Trade Classes / CSymbolInfo / NormalizePrice
  • www.mql5.com
NormalizePrice(double) - CSymbolInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: