Trade.RequestPosition() alway return 0 ?

 

Hi everyone,

Do you know why the RequestPosition method of the CTrade class always returns 0 ?


In other words, what is the simplest solution for retrieving the ticket or identifier of the position that has just been opened using my CTrade class?

I thought I saw in the forum a solution to compare the positions with their opening time to recover the last one and therefore the ticket ?

Best Reguards,
ZeroCafeine.

 

n MQL5, the RequestPosition method of the CTrade class returns 0 when the requested position does not exist or when there is no active position that matches the given symbol or parameters. There are several reasons why this might happen:

  1. No Active Positions: If there are no positions currently open in the symbol or market you are trying to query, RequestPosition will return 0 .

  2. Incorrect Symbol or Ticket: If you're passing an invalid symbol or a ticket number that does not correspond to an active position, the function will return 0 .

  3. Position Filtering: If you're filtering positions by parameters like magic number, type, or ticket, and there are no positions matching those filters, the method will return 0 .

  4. Permissions/Account Restrictions: Depending on your broker, there might be account settings or permissions that prevent position data from being queried.

 
Thank you for your reply

I do some backtesting and I can see that my position has been created and therefore exists.
Wilna Barnard #:
  • No Active Positions: If there are no positions currently open in the symbol or market you are trying to query, RequestPosition will return 0 .

  • Incorrect Symbol or Ticket: If you're passing an invalid symbol or a ticket number that does not correspond to an active position, the function will return 0 .

  • Position Filtering: If you're filtering positions by parameters like magic number, type, or ticket, and there are no positions matching those filters, the method will return 0 .

  • Permissions/Account Restrictions: Depending on your broker, there might be account settings or permissions that prevent position data from being queried.No Active Positions: I have position for sure, even I have many position, all the time get 0

  • No Active Positions: I have position for sure, even I have many position, all the time get 0
  • Incorrect Symbol or TicketI search for the ticket of my position on a single symbol
  • Position Filtering: I'm not filtering anything, I just want to retrieve the ticket from the Trade object using the RequestPosition method.
  • Permissions/Account Restrictions: How to know that ?
  •  

    Would you mind sharing your code? Sometimes, having a fresh set of eyes can help spot something that might be easy to overlook.

    Maybe there's a small detail or issue that stands out to someone else, and they could offer some insights or suggestions.

     
    Wilna Barnard #:

    Would you mind sharing your code? Sometimes, having a fresh set of eyes can help spot something that might be easy to overlook.

    Maybe there's a small detail or issue that stands out to someone else, and they could offer some insights or suggestions.

    yes for sure 

    if(!Trade.PositionOpen(_Symbol, ORDER_TYPE_BUY, .....) { Do Something }
    
    Last_Position_Ticket = Trade.RequestPosition();
     
    ZeroCafeine #:
    Trade.RequestPosition()

    tks you Wilna Barnard , Problem solved with RequestPosition method 😊

    Trade.RequestPosition()