Select position by ticket function

 

Hi,

I want to ask about selecting position by ticket function. My EA is multi-currency EA and I store each buy ticket and sell ticket to BuyTicket[] and SellTicket[] Array.

Usually when I want to select a position I will use loop through all open position and select each position by the ticket then filter it using position.Symbol(), position.Magic() and position.PositionType().

 for(int i=PositionsTotal()-1; i>=0; i--) {
      ulong ticket = PositionGetTicket(i);
      if(position.Symbol() == CurrentSymbol && position.Magic() == MagicNumber) {
         if(position.PositionType() == POSITION_TYPE_BUY){
         //......
      }
  }

I think this method is something unnecessary step since I already save buy ticket and sell ticket. Instead looping all through all open position I want to select it only using the ticket. My question is since I already have the ticket that I want to work with, should I loop through all open position or can I just select the position and working on it?

e.g.
note: no selection of symbol, magic number or position type

void TrailingStop(int SymbolLoo.p, ulong positionTicket){
   if(positionTicket <= 0) return;
   if(OrderSelect(positionTicket)) return;

   //--- SELECT POSITION AND GET TRAILING STATUS
   if(!position.SelectByTicket(PositionTicket)) {
      Print("ERROR - failed to SELECT POSITION with ticket #", PositionTicket)
      return;
   } else {
         if(tick.bid + Spread[SymbolLoop] > position.PriceOpen() + InpTrailStart * Point()) {
            double sl = NormalizeDouble(bid - InpTrailDistance * Point(), Digits());
            if(sl > position.StopLoss() || position.StopLoss() == 0) {
               result = TRAILING_LONG;
               //   trade.PositionModify(posTicket,sl,position.TakeProfit());
            }
         }
    }
}
 
Luandre Ezra:

Hi,

I want to ask about selecting position by ticket function. My EA is multi-currency EA and I store each buy ticket and sell ticket to BuyTicket[] and SellTicket[] Array.

Usually when I want to select a position I will use loop through all open position and select each position by the ticket then filter it using position.Symbol(), position.Magic() and position.PositionType().

I think this method is something unnecessary step since I already save buy ticket and sell ticket. Instead looping all through all open position I want to select it only using the ticket. My question is since I already have the ticket that I want to work with, should I loop through all open position or can I just select the position and working on it?

e.g.
note: no selection of symbol, magic number or position type

You can select the position by ticket and work with it… but you have to know that this position might be closed, might be Pending or a Market order… 
 
Daniel Cioca #:
You can select the position by ticket and work with it… but you have to know that this position might be closed, might be Pending or a Market order… 

Ok thanks for the answer