Thread: MQL4 Guide
View Single Post
  #12 (permalink)  
Old 06-17-2007, 05:56 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
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, ...
Reply With Quote