Error 4756, Invalid Stops

To add comments, please log in or register
sheenanananana
13
sheenanananana  

Hi MT5 Community!!

I can't set up my stop loss and take profit for ORDER_TYPE_SELL. Below is the code I used, and when I run the EA it says I have invalid stops. Is there something I'm doing wrong, or is my code wrong?

         ZeroMemory(mrequest);
         ZeroMemory(mresult);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // latest Bid price
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = 0.5;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         mrequest.sl = NormalizeDouble(latest_price.bid - STP*_Point,_Digits);
         mrequest.tp = NormalizeDouble(latest_price.bid + TKP*_Point,_Digits);
         //--- send order
         OrderSend(mrequest,mresult);
Chris70
543
Chris70  

1) what are the values that you are setting for STP and TKP? (=what's the requested stop distance in points?)

2) what is the result for SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) with your broker on this symbol?

3) for a sell order the sl should be HIGHER than the current price, so it should be NormalizeDouble(latest_price.bid + STP*_Point,_Digits)

4) for a sell order the tp should be LOWER than the current price, so it's NormalizeDouble(latest_price.bid - TKP*_Point,_Digits)

sheenanananana
13
sheenanananana  
Chris70:

1) what are the values that you are setting for STP and TKP? (=what's the requested stop distance in points?)

2) what is the result for SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) with your broker on this symbol?

3) for a sell order the sl should be HIGHER than the current price, so it should be NormalizeDouble(latest_price.bid + STP*_Point,_Digits)

4) for a sell order the tp should be LOWER than the current price, so it's NormalizeDouble(latest_price.bid - TKP*_Point,_Digits)

1) My stop loss is 200ticks and take profit is 100000ticks

2) I'm sorry, I'm quite new to this whole thing. Are we supposed to find SymbolInfoInteger? If so, what for?

3 and 4) I did it but the problem still persists

Files:
Chris70
543
Chris70  

For the sell order the take profit needs to be lower then the current price, but of course it still needs to be a positive number. According to your screenshot the requested take profit is -9.36716, which is no valid price.

There is nothing wrong with the calculation, because if you go from a price of 0.63284 down by an extreme distance such as ten thousand pips, you get a negative result: -9.36716 is the correct result in your example, but in real life, the price of a currency pair can't be negative. Try a much smaller number for TKP.

About the question regarding the result of SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) : with many brokers there is a minimum distance between the current price and a requested trigger price of stop loss, take profit, stop buy order or stop sell order. Apparently, as you don't have low numbers for STP and TKP, this wasn't the problem in your case. But just keep in mind for the future that sometimes - depending on your broker and the symbol, a very low value for the distance in points (=your STP and TKP) can also sometimes cause errors. For example with my broker, I can't request a stop distance of less than 40 points for EURUSD or less than 52 points for GBPJPY... it may be different with your broker, just be aware that this problem sometimes exists --> that is what the SYMBOL_TRADE_STOPS_LEVEL is for: it tells you which minimum distance exactly won't cause trouble (and if it's zero: you can forget about it).
somebodyoncetoldme
1
somebodyoncetoldme  
Chris70:

For the sell order the take profit needs to be lower then the current price, but of course it still needs to be a positive number. According to your screenshot the requested take profit is -9.36716, which is no valid price.

There is nothing wrong with the calculation, because if you go from a price of 0.63284 down by an extreme distance such as ten thousand, you get a negative result: -9.36716 is the correct result in your example, but in real life, the price of a currency pair can't be negative. Try a much smaller number for TKP.

thanks so much!! you're a lifesaver!!

if its not too much trouble, is it possible for you to see why my closing sell order isnt working? it keeps coming back as Invalid Request, and I'm not sure where the problem is.

if(Sell_opened==true)
{
if(Sell_Close_1 || Sell_Close_2 || Sell_Close_3 || Sell_Close_4 || Sell_Close_5 || Sell_Close_6)
  {
   for( int i = PositionsTotal()-1; i>0;i--)
   {
   ulong position_ticket = PositionGetTicket(i);
   string position_symbol = PositionGetString(POSITION_SYMBOL);
   int digits = (int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS);
   ulong magic = PositionGetInteger(POSITION_MAGIC);
   double volume = PositionGetDouble(POSITION_VOLUME);
   ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
 
   if(magic == EA_Magic)
     {
      ZeroMemory(mrequest);
      ZeroMemory(mresult);
      mrequest.action = TRADE_ACTION_DEAL;
      mrequest.position = position_ticket;
      mrequest.symbol = position_symbol;
      mrequest.volume = volume;
      mrequest.deviation = 1000;
      mrequest.magic = EA_Magic;
      mrequest.price = SymbolInfoDouble(position_symbol,SYMBOL_ASK);
      mrequest.type = ORDER_TYPE_BUY;
      OrderSend(mrequest,mresult);
      if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
Chris70
543
Chris70  

I see 2 issues:

1) for( int i = PositionsTotal()-1; i>=0;i--)

2) the line 

ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

is useless here; you already established that you want to close a sell position, so you need to request an opposite order, which you are already doing correctly by saying mrequest.type=ORDER_TYPE_BUY;

but you don't use the variable "ENUM_POSITION_TYPE type" later at all - so why declare it in the first place?

Chris70
543
Chris70  

did you just change your username? Or is @sheenanananana and @somebodyoncetoldme the same person with two accounts? In any case: please don't ask the same question in two threads ("double-posting"), that's quite annoying

[--> https://www.mql5.com/en/forum/326734]

Unable to close sell trade, Invalid request
Unable to close sell trade, Invalid request
  • 2019.11.18
  • www.mql5.com
Hi MQL5 community, I am unable to send a buy order to close my sell trade due to invalid request, error 4756. Is there something wrong with my code...
To add comments, please log in or register