OrderExpiration() Function


for example, forex

Let’s discuss the trading function OrderExpiration().

   datetime OrderExpiration()

This function returns the expiration date of a pending order. If the order is not executed till some specified time it is automatically deleted from the system. The order should be chosen with the function OrderSelect() beforehand.

If the expiration date is not set (that is the order can exist within an unlimited period of time till it is executed or deleted) OrderExpiration() will return 0.

What is the date of expiration of a pending order used for? There are lots of reasons. E.g. it is quite reasonable to delete pending orders on Friday evening just before the week is closed.

As an example of how the function OrderExpiration() is used let’s write here a piece of the code which runs through the pending orders which were not deleted and if the expiration date is not fixed it is set at 23:59:59 of the nearest Friday.

   // let’s calculate 23:59:59 of the nearest Friday
   datetime friday;
   datetime c_time;
   c_time = CurTime();
   // on friday – date and time at 00:00:00 of the current day
   friday = c_time-TimeHour(c_time)*60*60-TimeMinute(c_time)*60-TimeSeconds(c_time);   
   // on friday - date and time at 00:00:00 of the nearest Friday
   friday = friday + (5-TimeDayOfWeek(c_time))*60*60*24;
   // on friday – date and time at 23:59:59 of the nearest Friday
   friday = friday + 23*60*60 + 59*60 + 59;
   // let’s look through the pending orders and change the expiration 
   int pos;   
   for(pos=0; posOP_SELL)
             {
               // is the expiration date absent?
               if (OrderExpiration()==0)
                 // change the expiration date
                 OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), 
                                      OrderTakeProfit(), friday);
             }
         }
       else
           Print(GetLastError(), " when choosing order number", pos);
     }

There are several unknown functions for us in this code:

  • int TimeDayOfWeek(datetime date) — returns the day of the week (0-Sunday,1,2,3,4,5,6) for the date specified;
  • datetime CurTime() — the time when the last quote is received in the form of the number of seconds after 00:00 of January 1, 1970;
  • int TimeHour(datetime time) — returns the order number of an hour for the time;
  • int TimeMinute(datetime time) — returns the order number of a minute for the time;
  • int TimeSeconds(datetime time) — returns the number of seconds from the beginning of a minute for the time.

E.g. for 02:16:05 of Wednesday functions TimeHour, TimeMinute and TimeSeconds will return 2, 16 and 5 correspondingly. Whereas function TimeDayOfWeek will return 3.

As far as we remember the value of datetime type is a number of seconds from 00:00 of January 1, 1970. Of course, there are 60x60 seconds in an hour and there are 60 seconds in a minute. On the basis of it we understand how we get in the variable friday the date and time which correspond to 23:59:59 of the nearest Friday:

   // let’s calculate 23:59:59 of the nearest Friday
   datetime friday;
   datetime c_time;
   c_time = CurTime();
   // for  friday – date and time at  00:00:00 of the current day
   friday = c_time-TimeHour(c_time)*60*60-TimeMinute(c_time)*60-TimeSeconds(c_time);   
   // for friday - date and time at 00:00:00 of the nearest Friday
   friday = friday + (5-TimeDayOfWeek(c_time))*60*60*24;
   // for  friday – date and time at 23:59:59 of the nearest Friday
   friday = friday + 23*60*60 + 59*60 + 59;


+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?