The OrderCloseBy function


for example, forex

Let’s rewrite our Expert Advisor taking into consideration this recommendation. The OrderCloseBy() function will help us:

   //+------------------------------------------------------------------+
   //|                      Close everything on Friday.mq4 |
   //|                 Copyright © 2006, Andrey Vedikhin |
   //|                                http://www.vedikhin.ru |
   //+------------------------------------------------------------------+
   #property copyright "Copyright © 2006, Andrey Vedikhin"
   #property link      "http://www.vedikhin.ru"
//---- input parameters extern int MyHour=22; extern int MyMinute=00;
// 5 - Friday #define MyDay 5
datetime LastTradeTime;
//+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- // we’ll set yesterday as the time of the last trading operation LastTradeTime = CurTime()-24*60*60; //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //----

//---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- // we’ll check whether we haven’t closed all positions today // if we have we exit if (TimeDayOfYear(CurTime())==TimeDayOfYear(LastTradeTime)) return(0);
// if it isn’t Friday we exit if (DayOfWeek()!=MyDay) return(0);
// we’ll check if it is time to close positions if (((TimeHour(CurTime())==MyHour)&&(TimeMinute(CurTime())>=MyMinute)) ||(TimeHour(CurTime())>MyHour)) { // we’ll close all positions and deleted orders while (OrdersTotal()>0) { // we’ll select the first position or order in the list if (!OrderSelect(0, SELECT_BY_POS)) // in case of a failure exit from the loop break;
// if it is a pending order delete it if (OrderType()>OP_SELL) { if (!OrderDelete(OrderTicket())) { Print("Error ", GetLastError()," at deleting a pending order ", OrderTicket()); break; } } // if it is an open position close it else { // we try to find a locked position in the same instrument int ticket0; ticket0 = OrderTicket();
string symbol0; symbol0 = OrderSymbol();
int ordertype0; ordertype0 = OrderType();
int i; for (i = 1; i // we’ll selece the zero position over again if (!OrderSelect(ticket0, SELECT_BY_TICKET)) // in case of a failure exit the loop break;
double price; if (OrderType()==OP_SELL) price = MarketInfo(OrderSymbol(), MODE_ASK); else price = MarketInfo(OrderSymbol(), MODE_BID);
if (!OrderClose(OrderTicket(), OrderLots(), price, 3)) { Print("Error ", GetLastError()," at closing a position ", OrderTicket()); break; } }
// 10-second pause Sleep(10000); } if (OrdersTotal()==0) LastTradeTime = CurTime(); }
//---- return(0); } //+------------------------------------------------------------------+

At first I will tell you about the function itself and then we’ll consider each new line of the expert advisor.

   bool OrderCloseBy(int ticket, int opposite, color сolor=CLR_NONE)

The OrderCloseBy() function closes a position by another position in the same instrument. In case of success it returns true, in case of an error it returns false. the code of the error you can find out later with the help of the GetLastError() function.

Parameters of the OrderCloseBy() function:

  • ticket — ticket of the first position;
  • opposite — ticket of the second position;
  • color — the color of the closing arrow on the chart. If this parameter is lacking or its value is equal to CLR_NONE, the closing arrow isn’t displayed on the chart.

Let’s consider each new line of our expert advisor:

   // we’ll try to find a locked position in the same instrument
   int ticket0;
   ticket0 = OrderTicket();
string symbol0; symbol0 = OrderSymbol();
int ordertype0; ordertype0 = OrderType();
int i; for (i = 1; i // we’ll select the zero position over again if (!OrderSelect(ticket0, SELECT_BY_TICKET)) // in case of a failure exit the loop break;

At first we will remember the ticket number, instrument and the operation type of the selected position (the first one in the list of open positions and pending orders) in variables ticket0, symbol0 and ordertype0 respectively:

   int ticket0;
   ticket0 = OrderTicket();
string symbol0; symbol0 = OrderSymbol();
int ordertype0; ordertype0 = OrderType();

After that we will sort out all the rest positions on the list of positions in order to find a position in the same instrument but of the opposite direction. (OrderType()==((ordertype0+1)%2)):

   int i;
   for (i = 1; i

If we have found such a position we will close it and our initially selected position with the help of the OrderCloseBy() function and in case of a success we will assign value -2 to the variable i.

Then we check whether locked positions have been closed. If they have been closed we make a pause and proceed to the next iteration of the while loop, i.e. we see the next position/order — candidates for closing/deleting:

   if (i==-2)
      {
         Sleep(10000);
         continue;
      }

If locked position haven’t been found we select the first position in the list of open positions and pending orders over again:

   if (!OrderSelect(ticket0, SELECT_BY_TICKET)) 
      // in case of a failure exit the loop
      break;

Then we try to close this position with the help of the OrderClose() function.


Next article: "Error handling"

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?