MQL4 - automated forex trading   /  

Forum

MQL Guide

Back to topics list  | 1 2 3 To post a new topic, please log in or register

avatar
1272
RickD 2007.02.15 00:36 
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);
  ...

article

MQL4 Language for Newbies. Technical Indicators and Built-In Functions

This is the third article from the series "MQL4 Language for Newbies". Now we will learn to use built-in functions and functions for working with technical indicators. The latter ones will be essential in the future development of your own Expert Advisors and indicators. Besides we will see on a simple example, how we can trace trading signals for entering the market, for you to understand, how to use indicators correctly. And at the end of the article you will learn something new and interesting about the language itself.


avatar
1272
RickD 2007.02.17 03:10 
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);
  }
}



avatar
1890
waddahattar 2007.02.17 07:59 
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.

avatar
1890
waddahattar 2007.02.17 08:02 
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);
}


avatar
1272
RickD 2007.02.24 04:51 
waddahattar wrote:
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!

avatar
1272
RickD 2007.02.24 05:01 

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);
}



avatar
1272
RickD 2007.04.13 07:13 
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);
}

avatar
1272
RickD 2007.04.28 20:41 
nothere wrote:
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];
  }
}

avatar
1272
RickD 2007.04.28 21:25 
yocy1 wrote:
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++;
  }

avatar
1272
RickD 2007.05.18 19:17 
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);
}

avatar
1272
RickD 2007.05.25 20:57 
hopokuk wrote:
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, ...
Back to topics list   | 1 2 3  

To add comments, please log in or register