Cannot close orders in ANY way.

 

Hi,

Before trying to help me, I need to say I researched and read a LOT (dozens!) of posts about "Closing Orders" in forum- and none of them was enough to help me.

The problem is related to close all BUY/SELL orders if the Trend changes... and whenever I try this, I get a "requote" message and the position stay open.

What am I doing?


1) Opening a BUY/SELL with predefined TP/SL rates:

 ResetLastError();
      
      if(CheckMoneyForTrade(_Symbol, Lot, ORDER_TYPE_SELL))
      {

	  m_trade.SetTypeFilling(ORDER_FILLING_IOC);        //SYMBOL_FILLING_IOC

          m_trade.Sell( NormalizeDouble(Lot,2),
                        _Symbol, 
                        NormalizeDouble(Preco,(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS)), 
                        NormalizeDouble(SL,(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS)), 
                        NormalizeDouble(TP,(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS)),
                        Comment
                      );

    
          //--  Vamos verificar se a ordem foi recebida.
    
          if(m_trade.ResultRetcode() == 10008 || m_trade.ResultRetcode() == 10009)
            {
            
             // Print("Order successfully done");
                if(PrintLog==true)
                {
                PrintResultTrade(m_trade, m_symbol);
                }
                        
            return true;
            }
          else
            {
             Print(">>>>> Error ", GetLastError());
             PrintResultTrade(m_trade, m_symbol);
             return false;
            }
      
      }


2) At some point, the Trend can change and I try to close all "trend-opposite" orders using:

  for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
   {   
      string simbol = PositionGetSymbol(i);

      if (_Symbol==simbol)
        {
         long magicNumber = PositionGetInteger(POSITION_MAGIC);

         if(magicNumber==m_magic)
           {
                
                ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
           
                long order_type = PositionGetInteger(POSITION_TYPE);
                
               
                if(order_type== <parameter send to BUY/SELL type>) 
                {
                  RefreshRates();
                  m_trade.PositionClose(PositionTicket,15);
                }

           }    

        }
    }
    
    return Sucess;
}


m_Trade is a derived class of CTrade.

Everytime I call this function it returns 4756 and I see the problem is "requote".

I had debugged the entire code and everything runs OK, except the close this way.


An important info:

In another location of my EA, I close any ONE order using "ClosePosition", passing the exclusive order's Position Number to the function - and everything runs perfectly.

So, I tried also to call this routine within the above one, passing the researched number of all order, one by one. No success at all - I have the same behavior (error 4756 and requote).

I appreciate if you could help me to understand what is going on here and how to solve this.

 
AliceRioBR:

Hi,

Before trying to help me, I need to say I researched and read a LOT (dozens!) of posts about "Closing Orders" in forum- and none of them was enough to help me.

The problem is related to close all BUY/SELL orders if the Trend changes... and whenever I try this, I get a "requote" message and the position stay open.

What am I doing?


1) Opening a BUY/SELL with predefined TP/SL rates:


2) At some point, the Trend can change and I try to close all "trend-opposite" orders using:


m_Trade is a derived class of CTrade.

Everytime I call this function it returns 4756 and I see the problem is "requote".

I had debugged the entire code and everything runs OK, except the close this way.


An important info:

In another location of my EA, I close any ONE order using "ClosePosition", passing the exclusive order's Position Number to the function - and everything runs perfectly.

So, I tried also to call this routine within the above one, passing the researched number of all order, one by one. No success at all - I have the same behavior (error 4756 and requote).

I appreciate if you could help me to understand what is going on here and how to solve this.

HI AliceRioBR, here is my 5 cents statement: problem could be the "PositionClose" function within the "m_trade" class in the bottom MT4/MT5 function to actually close the position.  If you have the source code of that function you may find it passes a few values as parameters, and some brokers put some constraints on the parameters.  For instance, if closing a buying position on the EURUSD FX at price 1.18007 may fail because the broker demand it matches a minimum 0.00005 tick size.  This is just an example, don't trust me.  In that case 1.18007 is invalid.  The function would need to round it to 1.18005 or 1.18010.  Above is the most common cause I have seen but eventually there is a more basic issue with the code and the closure price used is far from the BID price at the broker.  You may need to check a slippage or deviation of your closure price compare to the BID price at the broker.  Sometimes there is a delay either because of internet, the EA, or the broker, and your desired closure price aged too much, and during a quick trend the BID price goes away.

I would suggest "Print" parameters right before the "PositionClose" function inside the "m_trade" class, including printing the Symbol "minimum tick size" and the Symbol BID price (if a BUY .... or ASK price if a SELL) so that you could find what is wrong for the parameter.  I hope you have the source code of the "m_trade" class, but if that is the case, that is what I would try based on my experience on the error you shared.

 
Rene Fernando Tovar Macchi:

HI AliceRioBR, here is my 5 cents statement: problem could be the "PositionClose" function within the "m_trade" class in the bottom MT4/MT5 function to actually close the position.  If you have the source code of that function you may find it passes a few values as parameters, and some brokers put some constraints on the parameters.  For instance, if closing a buying position on the EURUSD FX at price 1.18007 may fail because the broker demand it matches a minimum 0.00005 tick size.  This is just an example, don't trust me.  In that case 1.18007 is invalid.  The function would need to round it to 1.18005 or 1.18010.  Above is the most common cause I have seen but eventually there is a more basic issue with the code and the closure price used is far from the BID price at the broker.  You may need to check a slippage or deviation of your closure price compare to the BID price at the broker.  Sometimes there is a delay either because of internet, the EA, or the broker, and your desired closure price aged too much, and during a quick trend the BID price goes away.

I would suggest "Print" parameters right before the "PositionClose" function inside the "m_trade" class, including printing the Symbol "minimum tick size" and the Symbol BID price (if a BUY .... or ASK price if a SELL) so that you could find what is wrong for the parameter.  I hope you have the source code of the "m_trade" class, but if that is the case, that is what I would try based on my experience on the error you shared.

Hi Rene!

Thanks a lot for your information. Let's discuss it:

1) Yes, I see the problem in PRINT and everytime it is related to "Invalid SL/TP values" (which are in fact set since the position ordering).

2) I set my SLIP from 15 to 50 (to comply with m_Trader) and it has no effect at all.

But you gave me another new info: a kind of "rounding" may be expected from brokers. I'll see how can I deal with it.

Thank you again and I'm sorry for the delay in answer you.

 
AliceRioBR:

Hi Rene!

Thanks a lot for your information. Let's discuss it:

1) Yes, I see the problem in PRINT and everytime it is related to "Invalid SL/TP values" (which are in fact set since the position ordering).

2) I set my SLIP from 15 to 50 (to comply with m_Trader) and it has no effect at all.

But you gave me another new info: a kind of "rounding" may be expected from brokers. I'll see how can I deal with it.

Thank you again and I'm sorry for the delay in answer you.

Hi AliceRioBR

If I guess what you may need, here is a routine to format the stoploss (or any price field) before using it in the OrderSend / OrderClose.

You may invoke it just as ... double price = RformatPrice("COFFEE_C", 103.1234);

If "COFFEE_C" owns 2 decimal places and tick size is 0.25 then you should get something like this in the log:

In:103.1234 tick=0.25 digits=2 out=103.0


double RformatPrice(string mySymbol, double price)

  {

    // Converts any price to the closes tick required by the broker

    // And adjust to the maximum number of digits for the price

    double priceRounded = 0.0;

    int Gdigits = (int)MarketInfo(mySymbol,MODE_DIGITS);

    double GtickSize = MarketInfo(mySymbol,MODE_TICKSIZE);

    if (GtickSize < 1)

    {

      priceRounded = NormalizeDouble(MathRound(price / GtickSize) * GtickSize, Gdigits);

    }

    else

    {

      priceRounded = NormalizeDouble(MathRound(price * GtickSize) / GtickSize, Gdigits);

    }

    Print("In:", price, " tick=", GtickSize, " digits=", Gdigits, " out=", priceRounded);

    return priceRounded;

  }

Reason: