Strange random error [InvalidRequest] when opening a trade

 

Hi guys

I'm moving from mql4 to mql5

so far so good

I've read few tutorials on how properly to make an EA and check for errors but I'm still struggling to make this one working 

bool OpenTrade(int iType, double dLots, string sSymbol)
{
 Print("Open ticket for " + sSymbol);
 
 double dPrice;
 int iSign = 1;
 MqlTradeRequest request;
 MqlTradeResult result; 
 MqlTradeCheckResult checkresult;
 
 dPrice = SymbolInfoDouble(sSymbol,SYMBOL_ASK);
 if (iType == ORDER_TYPE_SELL) {dPrice = SymbolInfoDouble(sSymbol,SYMBOL_BID); iSign = -1;}
 
 request.action = TRADE_ACTION_DEAL;
 request.magic = MagicNumber;
 request.symbol      =sSymbol;
 request.volume      =dLots;
 request.price = NormalizeDouble(dPrice,_Digits);
 request.sl          =NormalizeDouble(dPrice-iSign*StopLoss*MyPoint,_Digits);
 request.tp          =NormalizeDouble(dPrice+iSign*TakeProfit*MyPoint,_Digits);
 request.deviation   =iSlipp;
 request.type_filling=ORDER_FILLING_AON;
 request.type = iType; // tried here to place manually SELL
 
 if (!OrderCheck(request,checkresult)) Print("Error opening Order " + GetLastError());
 OrderSend(request,result);
 
 Ticket = result.order;
 if (Ticket <= 0) {Print("Error opening Order " + GetLastError());return(false);}
 
 return (true);
}

I tried debugging it and got all the necessary error codes but they don;t help at all

please help me - this thing kills me already for few days - sometimes it works and opens orders sometimes it doesn;t 

here are the errors:

  Error opening Order 4756

  failed instant sell 0.30 EURUSD at 1.42185 sl: 1.43185 tp: 1.41685 (deviation: 30) [Invalid request]
  the return code from return.retcode = 10013 which is the same as Invalid Request

10013

TRADE_RETCODE_INVALID

Invalid request


from my side it looks to be a bug - because sometimes it opens the trades very nice - sometimes it does not open and only provides this errors like a lot of them which really pisses me off

does anyone of you have any experience with this?

thank you in advance guys

OOP in MQL5 by Example: Processing Warning and Error Codes
  • 2010.05.26
  • KlimMalgin
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
 

The error code 4756 means "Trade request sending failed".

Is any messages in client terminal log?

Please write to servicedesk. Do not forget to specify build number, server you are connecting to and attach logs of expert and client terminal.

Get in touch with developers using Service Desk!
  • www.mql5.com
We therefore attach great importance to all user reports about issues in our programs and try to answer each one of them.
 

Do you use the same method to calculate SL and TP for both operations - Buy and Sell?

 request.sl          =NormalizeDouble(dPrice-iSign*StopLoss*MyPoint,_Digits);
 request.tp          =NormalizeDouble(dPrice+iSign*TakeProfit*MyPoint,_Digits);
If your answer is Yes it is not surprise.
 
Rosh:

Do you use the same method to calculate SL and TP for both operations - Buy and Sell?

If your answer is Yes it is not surprise.


One way or another is started to work - but I had connections problems - the same code no changes and now it works


Rosh - about you question

It is not the same method for SL and TP - if you will be more attentive you will see that there is no error in Stop Loss and Take Profit calculations - they all are calculated in the right way

beside this if I had any problems in SL or TP the error message would be [Invalid Stops]

anyway - if you will take a closer look in the code you will see that this is not the case

I was thinking maybe I have some errors in writing the "request" parameters - but they all look ok - so I am more of confidence that this seems to be connectivity issues or a bug


By the way - another question for you guys - in order to close a sell order we need to open a buy one?

It looks I did not found a corresponding of OrderClose function of mql4 for mql5

thx for your time and consideration

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM - Documentation on MQL5
 
eronyx:

Rosh - about you question

It is not the same method for SL and TP - if you will be more attentive you will see that there is no error in Stop Loss and Take Profit calculations - they all are calculated in the right way

Yes, you are right. I forgot that parameter iSign can change its own sign
 
eronyx:

anyway - if you will take a closer look in the code you will see that this is not the case

I was thinking maybe I have some errors in writing the "request" parameters - but they all look ok - so I am more of confidence that this seems to be connectivity issues or a bug

Maybe
 
eronyx:

By the way - another question for you guys - in order to close a sell order we need to open a buy one?

It looks I did not found a corresponding of OrderClose function of mql4 for mql5

Yes, you have to do opposite operation to close open position.
 
Rosh:
Yes, you have to do opposite operation to close open position.

Thank you Rosh for your replies


 
I am using version 5.0 build 485 and the same problem appeared when I tried to run an EA that I developed last year. I will appreciate any help.

 

I found a solution before but forgot to post in here

I think it is also somewhere here in forum

all you need to do is this:

before changing any of the request parameter add this line of code : ZeroMemory(request);

 MqlTradeRequest request;
 MqlTradeResult result; 
 MqlTradeCheckResult checkresult;
 
 ZeroMemory(request);
 request.action = TRADE_ACTION_DEAL;
 request.magic = MagicNumber;

 at least this worked for me

 
eronyx:

I found a solution before but forgot to post in here

I think it is also somewhere here in forum

all you need to do is this:

before changing any of the request parameter add this line of code : ZeroMemory(request);

 at least this worked for me

Thanks eronyx,

Appreciate you sharing, this magically worked for me!

Reason: