orderclose problem

 

Hello, I have a problem when I must close an order, it does not work; there is an "invalid price" error (129) althought the programm seems good

if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == true) {
         if (OrderMagicNumber () == magicnumber)   {
            int prix ;
            int ticket = OrderTicket ();
            if (ordre == 1)   {                   // si ordre d'achat
               prix = Bid;                        // clôturer au Bid
               retour = OrderClose (ticket, lot, prix, 3, Orange);
               if (retour < 1) {
                  error = GetLastError();
                  Print ("erreur de fermeture d\'ordre d\'achat: (",error,"): ", ErrorDescription(error));
                  return (0);
               }
               achat = true;                    // ordre d'achat désormais autorisé de nouveau
               up = false;                      // remise à zéro du dépassement de "trailing" pips
               mess = "fermeture d\'ordre d\'achat";
               info (mess);
               if (Bid <= OrderOpenPrice ()) {
                  PlaySound ("boo.wav");
               }  else  {
                  PlaySound ("caisse.wav");
               }
 
emmett_brown:

Hello, I have a problem when I must close an order, it does not work; there is an "invalid price" error (129) althought the programm seems good

Is it a Buy or a Sell ? You close a Buy at Bid and a Sell at Ask . . . or just use OrderClosePrice()
 

In this example, it is a buy order at Ask and I have to close it at Bid but I have seen the error wich was as BIG as the german chancellor bottom!

==>

I use int prix = Bid instead of double prix = Bid ;)

Thanks

 
emmett_brown:

In this example, it is a buy order at Ask and I have to close it at Bid but I have seen the error wich was as BIG as the german chancellor bottom!

==>

I use int prix = Bid instead of double prix = Bid ;)

Thanks

wrong, use double bid, or instead to make a mess always use OrderClosePrice() as raptor said
 

Correct I would say "I used ...", not "I use"

But OrderClosePrice is for knowing the Close price of a closed order, isn't it?

Or for the price you can close it?

Here the order is not closed yet, am I wrong?

 
c 4 yourself
double OrderClosePrice( )
Returns close price for the currently selected order.

Note: The order must be previously selected by the OrderSelect() function.

https://docs.mql4.com/trading/ordercloseprice

 
Ok, closed or not;
 

closed !!!

 
Yeah, so I do not need this function for CLOSING the order.
 
Use OrderClosePrice() . . it works, try it for yourself if you do not want to believe us . .
 
  1. retour = OrderClose (ticket, lot, prix, 3, Orange);
    if (retour < 1) {
    OrderClose returns a bool not a int so retour < 1 makes no sense.
    if (!OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), 3, Orange)
       Alert("OrderClose failed: ", GetLastError());

  2. int ticket = OrderTicket ();
    if (ordre == 1)   {                   // si ordre d'achat
    What is ordre and what does 1 mean. It's not the order type since the variable isn't ever set in the posted code.
  3. prix, 3, Orange
    Code fails on a 5 digit broker. EA's must adjust TP, SL, and SLIPPAGE for 4 vs 5 digit brokers.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
         if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    //---- These are adjusted for 5 digit brokers.
    

Reason: