Download MetaTrader 5

Close all position _ error 4756

To add comments, please log in or register
Wjack07
69
Wjack07  

Hi:

I wrote a very easy close-all script just for convenience in some cases.
And because it is market execution, and doesn't care about the slippage (simply want to close all and leave), so it doesn't use placing price or check results.

But it doesnt work. I keep getting error 4756 "Trade request sending failed".

Anyone has a clue?

btw, I refer to this post https://www.mql5.com/en/forum/5290  and use ZeroMemory, but it doesnt work on me.

void OnStart(){

   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   ZeroMemory(result);
   
   for(int i=0;i<PositionsTotal();i++)
   {
      PositionSelect(PositionGetSymbol(i));
      //Print(PositionGetSymbol(i));
      
      request.action = TRADE_ACTION_DEAL;
      request.magic  = 100; 
      request.symbol =  PositionGetSymbol(i);
      request.volume = PositionGetDouble(POSITION_VOLUME);
      //Print(PositionGetDouble(POSITION_VOLUME));
      request.price = 0;
      request.sl = 0;      // Stop Loss specified
      request.tp = 0;    // Take Profit specified
      request.deviation = 4;          
      request.expiration=0;
      request.type_time=ORDER_TIME_GTC;
      request.type_filling = ORDER_FILLING_RETURN;
            
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      request.type   = ORDER_TYPE_SELL; 
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      request.type   = ORDER_TYPE_SELL;               
      
      if(OrderSendAsync(request,result)) {}
      else {
      Print("Getdata failed. Error ", GetLastError());}
             
   }
}

Many thanks
Wjack

Expert advisor works with build 527 but not with 540
Expert advisor works with build 527 but not with 540
  • www.mql5.com
GetLastError() returns 4756 (Trade request sending failed) and MqlTradeResult of OrderSent returns 10013 (Invalid request).
Alain Verleyen
Moderator
31478
Alain Verleyen  
Wjack07:

Hi:

I wrote a very easy close-all script just for convenience in some cases.
And because it is market execution, and doesn't care about the slippage (simply want to close all and leave), so it doesn't use placing price or check results.

But it doesnt work. I keep getting error 4756 "Trade request sending failed".

Anyone has a clue?

btw, I refer to this post https://www.mql5.com/en/forum/5290  and use ZeroMemory, but it doesnt work on me.

Many thanks
Wjack

  • You have to count down when deleting/closing.
  • You always have to check return value of function and check for error (what'is the result of PositionSelect() ?)
  • There is a typo :
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      request.type   = ORDER_TYPE_SELL; 
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      request.type   = ORDER_TYPE_SELL;  
      request.price = 0;
      request.sl = 0;      // Stop Loss specified
      request.tp = 0;    // Take Profit specified
      request.deviation = 4;          
      request.expiration=0;
      request.type_time=ORDER_TIME_GTC;
Wjack07
69
Wjack07  
angevoyageur:
  • You have to count down when deleting/closing.
  • You always have to check return value of function and check for error (what'is the result of PositionSelect() ?)
  • There is a typo :
Hi:

Thank you for pointing me out many mistakes. I modified it as following, but it still doesn't work.
I check every step, and every step looks fine except OrderSendAsync.

I am willing to hear any advice. (I might just be silly but I cannot find my blind spot.)

void OnStart(){

   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   ZeroMemory(result);
   
   for (int i = PositionsTotal()-1; i>0;i--)													             // I changed it to count down
   {
      if(!PositionSelect(PositionGetSymbol(i))){												             // this one is OK
      Print("failed at PositionSelect  ", GetLastError());
      }
      
      request.action = TRADE_ACTION_DEAL;      
      request.symbol = PositionGetSymbol(i);
      request.volume = PositionGetDouble(POSITION_VOLUME);    
      request.type_filling = ORDER_FILLING_RETURN;
      
            
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      request.type   = ORDER_TYPE_SELL; 
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      request.type   = ORDER_TYPE_BUY;               
      
      
      Print("Check  1.", request.action,"  2.",request.symbol,"  3.",request.volume,"  4.",request.type_filling,"  5.",request.type);  		             // this one looks find, every element prints out correct value
     
      if(!OrderSendAsync(request,result)){ 														     // this is where the error is
      Print("failed at OrderSend  ", GetLastError());
      }
             
   }
}

Alain Verleyen
Moderator
31478
Alain Verleyen  
Wjack07:
Hi:

Thank you for pointing me out many mistakes. I modified it as following, but it still doesn't work.
I check every step, and every step looks fine except OrderSendAsync.

I am willing to hear any advice. (I might just be silly but I cannot find my blind spot.)

What is the return (trading) code ?

How do you know it's market execution ?

Wjack07
69
Wjack07  
Hi:

What do you mean "return trading code"? the message I got from the OrderSendAsync is its error, which is 4756 = "Trade request sending failed".

As for how do I know it will be market execution...
I thought once I only provide the five parameters "Market execution" requires, it will do the market execution. It is like its definition on the manual, right?

Quote from the MT5 reference:

Market Execution

This is a trade order to open a position in the Market Execution mode. It requires to specify the following 5 fields:

·action

·symbol

·volume

·type

·type_filling

Also it is possible to specify the "magic" and "comment" field values.

 

To give it a try, I used Trade.mqh instead, following is the code, and it is working.

#include <Trade\Trade.mqh>

void OnStart(){
   CTrade            myTrade; 
   
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   ZeroMemory(result);
   
   for (int i = PositionsTotal()-1; i>-1;i--)
   {
      if(!PositionSelect(PositionGetSymbol(i))){
      Print("failed at PositionSelect  ", GetLastError());
      }
      
      request.action = TRADE_ACTION_DEAL;
      request.symbol = PositionGetSymbol(i);
      request.volume = PositionGetDouble(POSITION_VOLUME); 
      request.type_filling = ORDER_FILLING_RETURN;      
            
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
      request.type   = ORDER_TYPE_SELL; 
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
      request.type   = ORDER_TYPE_BUY;  
     
      if(!myTrade.PositionOpenAsync(request.symbol,request.type ,request.volume, 0, 0, 0)){
      Print("failed at OrderSend  ", GetLastError());
      }
             
   }
}

BUT, I dont understand why the trade.mqh can work, and using OrderSendAsync cannot.

They are essentially the same.

Maybe I miss something, and my Mqlrequest is not defined correctly, but I cannot find my blind spot.

Many thanks
Wjack

Alain Verleyen
Moderator
31478
Alain Verleyen  
Wjack07:
Hi:

What do you mean "return trading code"? the message I got from the OrderSendAsync is its error, which is 4756 = "Trade request sending failed".

As for how do I know it will be market execution...
I thought once I only provide the five parameters "Market execution" requires, it will do the market execution. It is like its definition on the manual, right?


If you are hoping some help, try to answer my questions instead of asking other questions.
Rogerio Figurelli
Moderator
40853
Rogerio Figurelli  
Wjack07:
Hi:

Thank you for pointing me out many mistakes. I modified it as following, but it still doesn't work.
I check every step, and every step looks fine except OrderSendAsync.

I am willing to hear any advice. (I might just be silly but I cannot find my blind spot.)

Hi, what about a 'continue;' if you get PositionSelect() false?
Wjack07
69
Wjack07  
angevoyageur:
If you are hoping some help, try to answer my questions instead of asking other questions.
Hi:

you are right, so lets step back a bit.

To you r question, I dont know the answer (I mean "return (trading) code").

For each function, there is a return (a boolean, int, double, etc.), do you mean that?

The return for the OrderSendAsync in my code above is false. Hope that answer your question, and if not, could you please explain a bit.

Many thanks
Wjack
Wjack07
69
Wjack07  
figurelli:
Hi, what about a 'continue;' if you get PositionSelect() false?
Thank you for the suggestion, you are right, I should do so to avoid unwanted results.

But that doesnt seem to be where the problem was. (I changed it, the error 4756 still jumps out.)

Many thanks
Wjack
Rogerio Figurelli
Moderator
40853
Rogerio Figurelli  
Wjack07:
Thank you for the suggestion, you are right, I should do so to avoid unwanted results.

But that doesnt seem to be where the problem was. (I changed it, the error 4756 still jumps out.)

Many thanks
Wjack
You guys are on the right way to solve, just my two cents! :-)
My two cents - Wikipedia, the free encyclopedia
My two cents - Wikipedia, the free encyclopedia
  • en.wikipedia.org
My two cents" (2¢) and its longer version "put my two cents in" is a United States (US) idiomatic expression,1 taken from the original English idiom expression: to put in "my two pennies worth" or "my two-penn'orth." It is used to preface the tentative stating of one’s opinion. By deprecating the opinion to follow – suggesting its value is...
Alain Verleyen
Moderator
31478
Alain Verleyen  
Wjack07:
Hi:

you are right, so lets step back a bit.

To you r question, I dont know the answer (I mean "return (trading) code").

For each function, there is a return (a boolean, int, double, etc.), do you mean that?

The return for the OrderSendAsync in my code above is false. Hope that answer your question, and if not, could you please explain a bit.

Many thanks
Wjack

As I already said in your other topic, PLEASE READ THE DOCUMENTATION CAREFULLY, OrderSendSync():

Return Value

Returns true if the request is sent to a trade server. In case the request is not sent, it returns false. In case the request is sent, in the result variable the response code contains TRADE_RETCODE_PLACED value (code 10008) – "order placed". Successful execution means only the fact of sending, but does not give any guarantee that the request has reached the trade server and has been accepted for processing. When processing the received request, a trade server sends a reply to a client terminal notifying of change in the current state of positions, orders and deals, which leads to the generation of the Trade event.

The result of executing the trade request on a server sent by OrderSendAsync() function can be tracked by OnTradeTransaction handler. It should be noted that OnTradeTransaction handler will be called several times when executing one trade request.

Error 4756 is meaningless, you have to check what is the real cause of this error.

About Market execution, do you know what that means ?

My guess is you don't have market execution for this symbol.


12
To add comments, please log in or register