Error 4756, Invalid Stops

 

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);
 

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)

 
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:
 

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).
 
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
 

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?

 

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...
 

I have a problem with my code, it doesn't open operations, can anyone help me?

Improperly formatted code removed by moderator. @Manuel Pouso Please EDIT your post and use the CODE button when you insert code .

Code button in editor

 
  1. Here we speak English, this https://www.mql5.com/es/forum is the Spanish forum (with the same rules..)
  2. Run the Styler (Ctrl+,) in the editor!
  3. Edit the posted code so that is is shown as code with either Alt+S or from the headline.
  4. If the Code compiles without error but it doesn't act as planed use the debugger to find out where and why:
    Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
    Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
    Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 

To troubleshoot the issue, you can follow these steps:

  1. Check if the stop loss and take profit values are set correctly for the sell order. For a sell order, the stop loss should be higher than the current price, and the take profit should be lower than the current price. Verify if the calculations are correct in the code:

mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); 

  1. Verify if the values of STP (Stop Loss) and TKP (Take Profit) are set correctly and within the allowed range by your broker.

  2. For the issue with closing a sell order, consider the suggestions provided earlier:

a. Correct the loop:

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

b. Remove the unnecessary line:

ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); 
Post formatted by moderator
 
         mrequest.sl = NormalizeDouble(latest_price.bid - STP*_Point,_Digits);
         mrequest.tp = NormalizeDouble(latest_price.bid + TKP*_Point,_Digits);

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
    My GBPJPY shows average spread = 26 points, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

Reason: