Quote:
|
Originally Posted by hopokuk
I would like to program this
IF NUMBER OF OPEN TRADES = 8 THEN CLOSE THE OLDEST OPEN TRADE
I want to close the oldest market order if there are >=8 market orders, but to ignore all the pending orders
thanks
|
Code:
extern int Magic = ...
//-----
int BuyCnt = 0;
int SellCnt = 0;
int cnt = OrdersTotal();
for (int i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == OP_BUY) BuyCnt++;
if (type == OP_SELL) SellCnt++;
}
if (BuyCnt+SellCnt < 8) return;
//-----
datetime tm = 0;
int ticket = -1;
cnt = OrdersTotal();
for (i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
type = OrderType();
if (type == OP_BUY || type == OP_SELL)
{
if (OrderOpenTime() > tm)
{
tm = OrderOpenTime();
ticket = OrderTicket();
}
}
}
//-----
if (ticket != -1) OrderClose(ticket, ...