int PositionLastOpenedBarIndex(string f_symbol="",
int f_cmd=-1,
int f_magic=-1,
ENUM_TIMEFRAMES f_tf=PERIOD_CURRENT)
{
if(f_cmd==OP_BUYLIMIT || f_cmd==OP_SELLLIMIT ||
f_cmd==OP_BUYSTOP || f_cmd==OP_SELLSTOP)
return -1;
datetime f_timeOpen=0;
for(int i=OrdersTotal()-1; i>=0; i--)
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if((f_symbol==OrderSymbol() || f_symbol=="") &&
(f_cmd ==OrderType() || f_cmd==-1) &&
(f_magic ==OrderMagicNumber() || f_magic==-1))
if(OrderOpenTime()>f_timeOpen)
f_timeOpen=OrderOpenTime();
if(f_timeOpen==0)
return -1;
else
return iBarShift(f_symbol,f_tf,f_timeOpen,true);
}