MQL Guide

 
OrdersCount function allows to get the orders count of predefined type.

int OrdersCount(int type)
{
  int orders = 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;
 
    if (OrderType() == type) orders++;
  }
 
  return (orders);
}
Example:

int start() 
{
  int BuyCnt = OrdersCount(OP_BUY);
  if (BuyCnt > 0) return (0);
  ...
 
How to close all the orders:

int Slippage = 3;
 
void CloseOrders() 
{
  int cnt = OrdersTotal();
  for (int i=cnt-1; i>=0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
 
    if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage);
    if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage);
  }
}
How to close all the orders of predefined type:

void CloseOrders(int type) 
{
  int cnt = OrdersTotal();
  for (int i=cnt-1; i>=0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() != type) continue;
    
    if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage);
    if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage);
  }
}
 
Hi

I suggest you to add Magic number to your code .. when you Open , Count , Modify or Close your expert Orders .

Thanks for your How-To library.
 
This is an example

int MyRealOrdersTotal(int Magic)
{
  int c=0;
  int total  = OrdersTotal();
 
  for (int cnt = 0 ; cnt < total ; cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber() == Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUY || OrderType()==OP_SELL))
    {
      c++;
    }
  }
  return(c);
}
 
waddahattar:
Hi

I suggest you to add Magic number to your code .. when you Open , Count , Modify or Close your expert Orders .

Thanks for your How-To library.

Ordinarily I describe Magic as expert parameter.
Thank you!
 

Get the OpenTime of last order with predefined type.
The function makes a search of open trades and the history.
-1 means there are no orders found.

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);
}
 
The NextDay function gets the date of the next day.

void NextDay(int& day, int& month, int& year) 
{
  datetime Time0 = CurTime();
  datetime Tomorrow = Time0 + 24*60*60;
  
  day = TimeDayOfYear(Tomorrow);
  month = TimeMonth(Tomorrow);
  year = TimeYear(Tomorrow);
}
 
nothere:
Hello:

Could you please take a look at the following EA, and let me know what modifications I need to make to have it only enter a trade once per bar? (I run this on the 1H or 4H Time frame)

Regards
Not Here.


datetime Time0 = 0;
 
void start()
{
  if (Time0 != Time[0])
  {
    OrderSend(...
    Time[0] = Time[0];
  }
}
 
yocy1:
i would like to write a code for a hedge strategy in one currency and stop open when, for example, buy OP are more then sell OP, so i need to write something like, "if totalorder of OP_BUY > totalorders of OP_SELL" any idea?

  int BuyCnt = 0;
  int SellCnt = 0;
  int BuyStopCnt = 0;
  int SellStopCnt = 0;
  int BuyLimitCnt = 0;
  int SellLimitCnt = 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 (type == OP_BUYSTOP) BuyStopCnt++;
    if (type == OP_SELLSTOP) SellStopCnt++;
    if (type == OP_BUYLIMIT) BuyLimitCnt++;
    if (type == OP_SELLLIMIT) SellLimitCnt++;
  }
 
A code for dynamic calculation of working lot.

extern bool uplot = true;
extern int lastprofit = 1;
extern double lotmin = 0.1;
extern double lotmax = 0.5;
extern double lotstep = 0.1;
 
double GetLots() 
{  
  double lot = lotmin;
  if (!uplot) return (lot);
  
  int ticket = GetLastOrderHist();
  if (ticket == -1) return (lot);
  
  if (!OrderSelect(ticket, SELECT_BY_TICKET, MODE_HISTORY)) return (lot);
  if (OrderProfit()*lastprofit < 0) return (lot);
  
  lot = MathMin(OrderLots() + lotstep, lotmax);
  return (lot);
}
 
int GetLastOrderHist(int type = -1)
{
  int ticket = -1;
  datetime dt = 0;
  int cnt = OrdersHistoryTotal();
    
  for (int i=0; i < cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
    
    if (type != -1 && OrderType() != type) continue;
    
    if (OrderCloseTime() > dt) {
      dt = OrderCloseTime();
      ticket = OrderTicket();
    }
  }
  
  return (ticket);
}
 
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

  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, ...
Reason: