Issue with Obtaining Ticket Number When Executing a Buy Order mql5

 

Hello everyone!

I'm facing an issue with an MQL5 script that executes a buy order but returns the ticket number as 0. I have verified that the order is executed correctly, but when printing the value of ticket , I always get 0.

Here is my code:

#include <Trade\Trade.mqh>
CTrade         m_trade;
#define Bid (SymbolInfoDouble("XAUUSD", SYMBOL_BID))
#define Ask (SymbolInfoDouble("XAUUSD", SYMBOL_ASK))
int OnInit()
  {
//---
   vender();
  //---
   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
//---
     }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//-----------------------------------------------------------------------------------------
void vender()
  {
          ulong  ticket=OrderSendR("XAUUSD",ORDER_TYPE_SELL,0.01,Bid,3,0,0,"666",0,0,clrBlack);
Alert(ticket);  //RETURN 0
     }
//---------------------------------------------------------------------------------*
//+------------------------------------------------------------------+
ulong OrderSendR(string Symb, const ENUM_ORDER_TYPE order_type, double volume, double price, ulong Slippage, double SL, double TP, string comment = NULL, int Magic = 0, datetime expiration = 0, color arrow_color = clrNONE) {
    MqlTradeRequest Request;
    MqlTradeResult Result;
    ZeroMemory(Request);
    const int curDigits = (int)SymbolInfoInteger(Request.symbol, SYMBOL_DIGITS);
    if (SL > 0) { SL = NormalizeDouble(SL, curDigits); }
    if (TP > 0) { TP = NormalizeDouble(TP, curDigits); }
    if (price > 0) { price = NormalizeDouble(price, curDigits); }
    else {
        Print("---- ERROR PRICE ZERO IN ORDER SEND -----");
        return (-1);
    }
    if (order_type == ORDER_TYPE_BUY || order_type == ORDER_TYPE_SELL) {
        Request.action = TRADE_ACTION_DEAL;
        Request.magic = Magic;
        Request.symbol = ((Symb == NULL) ? ::Symbol() : Symb);
        Request.volume = volume;
        Request.price = price;
        Request.tp = TP;
        Request.sl = SL;
        Request.deviation = Slippage;
        Request.type = order_type;
        Request.type_filling = ORDER_FILLING_IOC;
        if (expiration > 0) {
            Request.type_time = ORDER_TIME_SPECIFIED;
            Request.expiration = expiration;
        }
        Request.comment = comment;
        if (OrderSendAsync(Request, Result)) {
            // Se ejecutó con éxito, verificar el resultado
            if (Result.retcode == 10009 || Result.retcode == 10008) {
                Alert("Order executed successfully. Ticket: ", Result.deal);  //RETURN 0
                return (Result.deal); // Devolver el número de ticket
            }
            else {
                Print("---- ERROR IN ORDER SEND ----- Retcode: ", Result.retcode);
                return (-1);
            }
        }
        else {
            Print("---- ERROR IN ORDER SEND ----- OrderSendAsync failed");
            return (-1);
        }
    }
    return (-1);
}

I have verified that the order is executed correctly in mt5 and returns a valid ticket for the order. However, when printing the value of ticket , I always get 0.

Does anyone have any idea what could be causing this issue? Is there something I am overlooking in my code or something else I can check?

Any help or suggestions would be greatly appreciated! Thank you very much in advance.

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties
  • www.mql5.com
Running MQL5 Program Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

It is strange that this is not working. However there is another solution here: 

PositionGetTicket(PositionsTotal()-1)
 
Yashar Seyyedin #:

It is strange that this is not working. However there is another solution here: 

Thank you for your response! Let me explain the code I'm using and the situation I'm facing.

I'm trying it with a demo account.

ulong ticket = OrderSendR("XAUUSD", ORDER_TYPE_SELL, 0.01, Bid, 3, 0, 0, "666", 0, 0, clrBlack);
Alert(ticket);   // Returns 0
ulong lastPositionTicket = PositionGetTicket(PositionsTotal() - 1);  
Alert("Last position ticket: ", lastPositionTicket);

In this code snippet, I'm attempting to send a sell order using the OrderSendR function, which is a modified version of the OrderSend function to support asynchronous orders. After sending the order, I'm trying to retrieve the ticket of the last opened position in my account using the PositionGetTicket function.

However, I'm encountering an issue where ticket always returns 0 , even when the order executes successfully. I've tried using PositionGetTicket(PositionsTotal() - 0) instead of PositionGetTicket(PositionsTotal() - 1) ,

Returns the ticket opened in MT5 before.

image:

https://bitcoincrack.com/users/Captura.png

.I'm sorry for my English

I appreciate any help or suggestions to resolve this issue!

 

I tried using OrderSend instead of OrderSendAsync and it is working. It has something to do with the Asynchronous functionality.

 
Yashar Seyyedin #:

Intenté usar OrderSend en lugar de OrderSendAsync y está funcionando. Tiene algo que ver con la funcionalidad asincrónica.

Thank you for sharing your insights; they have been very helpful to me. It seems that the issue was related to the asynchronous nature of trading operations. After further investigation, I have discovered that the OrderSendAsync function sometimes returns a ticket of 0 immediately after sending an order. To address this issue, I plan to modify my approach and consider using OrderSend, as you have suggested.

Best regards,

Reason: