With the help of the OrderOpenTime() function we can get the date and time of opening of an order/position selected previously by the OrderSelect() function.
datetime OrderOpenTime()
- In case of a pending order the OrderOpenTime() function returns the date and time when the order was placed.
- In case of an open or closed position it returns the date and time when the position was open.
In order to determine that a position was open today we need to know the value of the type datetime for today’s date at 00:00:00 o’clock.
Four new functions will help us in this:
- datetime CurTime() — the time when the last quotation came in the form of the number of seconds passed after 00:00 January 1, 1970;
- int TimeHour(datetime time) — returns the order number of the hour for the time time;
- int TimeMinute(datetime time) — returns the order number of the minute for the time time;
- int TimeSeconds(datetime time) — returns the number of seconds from the beginning of the minute for the time time.
For example for 02:16:05 functions TimeHour, TimeMinute and TimeSeconds will return consequently 2, 16 and 5.
We remember that a value of the type datetime represents the number pf seconds passed since 00:00 1 January 1, 1970. Also we remember that there are 60×60 seconds in an hour, and 60 seconds in a minute. That’s why knowing the current time (we can get it with the help of CutTime), we can get the time of the current day for 00:00:00 o’clock:
int c_time = CurTime();
datetime day_start;
day_start=c_time-TimeHour(c_time)*60*60-TimeMinute(c_time)*60-TimeSeconds(c_time);
And the whole code which will count the profit on all closed positions that were opened today, will be as follows:
//---- we will count the beginning of the day - variable day_start
int c_time = CurTime();
datetime day_start;
day_start=c_time-TimeHour(c_time)*60*60-TimeMinute(c_time)*60-TimeSeconds(c_time);
//---- we’ll count the profit
int profit = 0;
int pos;
for ( pos = 0; pos=day_start) profit += OrderProfit();
}
}
else
Print("Error ", GetLastError(), " at selecting order ", OrderTicket());
}
Print("Total profit on all closed positions = ", profit);
Next article: "
OrderMagicNumber function"