GetLastOpenTime function gets OpenTime of the last order with predefined type.
The function makes a search of open trades and the history.
-1 means there are no orders found.
Code:
datetime GetLastOpenTime(int type)
{
datetime tm = -1;
int cnt = OrdersTotal();
for (int i=0; i<cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
//Optional
//if (OrderSymbol() != Symbol()) continue;
//if (OrderMagicNumber() != Magic) continue;
if (OrderType() != type) continue;
tm = MathMax(tm, OrderOpenTime());
}
cnt = OrdersHistoryTotal();
for (i=0; i<cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
//Optional
//if (OrderSymbol() != Symbol()) continue;
//if (OrderMagicNumber() != Magic) continue;
if (OrderType() != type) continue;
tm = MathMax(tm, OrderOpenTime());
}
return (tm);
}