Please Help - How to select order by ticket by using CTrade library

 

I'm trying to get open price from ticket number and I'm using CTrade library to open position. I can open the position with no problem, but I think I cannot select the openned position by the ticket I have. I have tried OrderSelect() function, but it return false with error code 4754 which mean "Order not found". I don't know why.

Below is my code.

#include "cdynamicarray.mqh"
#include<Trade\Trade.mqh>

CDynamicArray dynamicArray;

CTrade trade;

int OnInit()
{     
    for(int i=0; i<PositionsTotal(); i++)
      {
         ulong ticket=PositionGetTicket(i);
         OrderSelect(ticket);
         printf(GetLastError());
         double openPrice = OrderGetDouble(ORDER_PRICE_OPEN);
         
         printf("ticket = " + ticket);
         printf("Open price = " + openPrice);
      }
      
      return(INIT_SUCCEEDED);
}

result in Experts console

From the result, I openned 2 positions manually. You can see that it can get ticket number, but the open price are 0.

Please Help. Appreciate :)

 
I know the issue already. It was just the misunderstanding of PositionGetTicket() function. When the PositionGetTicket() is execute, it means that the position is automatically selected. There is no need to execute OrderSelect() anymore. I deleted OrderSelect(ticket) from my code and it works. Thank you.
 
PositionGetDouble.
 
Tanitsak Jirawuttanakit #:
I know the issue already. It was just the misunderstanding of PositionGetTicket() function. When the PositionGetTicket() is execute, it means that the position is automatically selected. There is no need to execute OrderSelect() anymore. I deleted OrderSelect(ticket) from my code and it works. Thank you.

Hi, Jirawuttanakit!

I would like just to thank you. Your doubt, but especially your comment above, helped me a lot to understand what I was doing wrong.

Best regards.

Reason: