I have written a function to return the amount of days for which to query the historical server given a number of trading days. For instance, if today is Monday and the market has been open or is open, and you want the past two days of data, it will return 4, as it will skip over weekends. Otherwise, if the market is NOT open, it will return 5, as it will only be able to retrieve Friday and Thursday.
This has been integrated with a holiday schedule checker (MarketOpen() function) to determine whether or not the market was closed on that day (Christmas, Easter, etc).
The function is below. I ask DTN developers to please let me know if this logic is correct:
* If day is a weekend, increase the number of days to query by 1.
* If the time of the
CURRENT DAY is prior to the market opening (9:30 am), then increase the number of days to query by 1.
Is this correct? Please note the function below, and take a look at the TODO comments.
/*
* Get the actual amount of days to query the IQConnect server for 'days' trading days.
*/
int GetQueryDays(int days)
{
struct tm l_tm;
int cur_hour, cur_min, cur_day, cur_mon, cur_year, cur_wday;
time_t l_time = time(NULL);
int num_days = 0;
int i;
if (!days)
return 1;
localtime_r(&l_time, &l_tm);
cur_min = l_tm.tm_min;
cur_hour = l_tm.tm_hour;
cur_day = l_tm.tm_mday;
cur_mon = l_tm.tm_mon;
cur_year = l_tm.tm_year;
cur_wday = l_tm.tm_wday;
num_days = days;
i = 1;
/* First, check if we're in a trading day, and the request is only for one day. */
if (days == 1)
{
if (cur_wday != 0 && cur_wday != 6 && cur_hour <= 23 && cur_min <= 59)
{
if (cur_hour > 8)
{
/* TODO: > 30 may be buggy? It depends on when they roll-over for 1 minute bars. Check this later. */
if (cur_hour == 9 && cur_min > 30)
return 1; /* Just one day is needed. */
}
}
}
while (i <= days)
{
if (cur_wday == 0)
cur_wday = 6;
else
cur_wday -= 1;
if (cur_day == 1)
{
if (cur_mon == 0)
{
/* First day of January; we have to scroll-back a year. */
cur_year -= 1;
cur_mon = 11;
}
else
{
cur_mon -= 1;
}
cur_day = NumberOfDaysInMonth(cur_mon, cur_year);
}
else
cur_day -= 1;
if (Market_Open(cur_mon, cur_day, cur_wday, 11, 30))
i++;
else
num_days++;
}
/* If today is a weekend, or it's past midnight but before the market open, then add another day to the total. */
if (l_tm.tm_wday == 0 || l_tm.tm_wday == 6)
num_days++;
else if (l_tm.tm_hour >= 0 && l_tm.tm_hour <= 8)
num_days++;
else if (l_tm.tm_hour == 9 && l_tm.tm_min <= 30)
{
/* TODO: <= 30 may be buggy? It depends on when they roll-over for 1 minute bars. Check this later. */
num_days++;
}
return num_days;
}
Thanks in advance!
Brandon
Edited by brandon on Jan 8, 2006 at 02:35 PM