using System; using System.Collections; using System.Collections.Generic; using MarketData.Utils; // Filename: DateGenerator.cs // Author:Sean Kessler namespace MarketData.Utils { /// DateGenerator - Generate Historical Dates [Serializable] public class DateGenerator { private Hashtable holidays=null; public DateGenerator() { } public DateTime GetPrevMonthStart(DateTime dateTime) { DateTime startDate = new DateTime(dateTime.Year, dateTime.Month, 1); startDate = startDate.AddMonths(-1); startDate = GetNextBusinessDay(startDate); return startDate; } public DateTime GetCurrMonthStart(DateTime dateTime) { DateTime startDate = new DateTime(dateTime.Year, dateTime.Month, 1); startDate = GetNextBusinessDay(startDate); return startDate; } public DateGenerator(Hashtable holidays) { this.holidays=holidays; } public static int GetPrevMonth(int month) { if(1==month)return 12; return month-1; } /// FindPrevBusinessDay - Finds previous business day /// DateTime public DateTime FindPrevBusinessDay(DateTime asOf) { TimeSpan oneDay=new TimeSpan(1,0,0,0); asOf=asOf.Subtract(oneDay); while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Subtract(oneDay); return asOf; } /// FindNextBusinessDays - Finds following business day /// DateTime public DateTime FindForwardBusinessDay(DateTime asOf,int daysForward) { for (int index = 0; index < daysForward; index++) { asOf = FindNextBusinessDay(asOf); } return asOf; } /// FindNextBusinessDay - Finds following business day /// DateTime public DateTime FindNextBusinessDay(DateTime asOf) { TimeSpan oneDay=new TimeSpan(1,0,0,0); asOf=asOf.Add(oneDay); while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Add(oneDay); return asOf; } /// GetPrevBusinessDay - Gets previous business day /// If the given date is a business day then this method will return the given date /// None public DateTime GetPrevBusinessDay(DateTime asOf) { TimeSpan oneDay=new TimeSpan(1,0,0,0); while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Subtract(oneDay); return asOf; } /// GetNextBusinessDay - Gets next business day /// If the given date is a business day then this method will return the given date /// None public DateTime GetNextBusinessDay(DateTime asOf) { TimeSpan oneDay=new TimeSpan(1,0,0,0); while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Add(oneDay); return asOf; } /// GetNextDay - Gets next business day /// If the given date is a business day then this method will return the given date /// None public DateTime GetNextDay(DateTime asOf) { return asOf.Add(Utility.OneDay); } /// GetPrevFriday - Gets date of prior friday /// Get the date of the previous friday - if previous Friday is holiday will seek previous business day /// None public DateTime GetPrevFriday(DateTime asOf) { TimeSpan oneDay=new TimeSpan(1,0,0,0); List historicalDates=null; int daysToFetch=7; if(DayOfWeek.Friday==asOf.DayOfWeek) { asOf=asOf.Subtract(oneDay); daysToFetch--; } historicalDates=GenerateHistoricalDates(asOf,daysToFetch); for(int index=0;index endDate) { TimeSpan timeSpan = startDate.Date - endDate.Date; return (int)timeSpan.TotalDays; } else { TimeSpan timeSpan = endDate.Date - startDate.Date; return (int)timeSpan.TotalDays; } } public static List GenerateHistoricalYear(int startYear,int years) { List yearsList = new List(); for (int index = 0; index < years; index++) { yearsList.Add(startYear); startYear--; } return yearsList; } public List GenerateHistoricalDates(DateTime startDate, int dayCount) { List histDates=new List(); DateTime histDate; TimeSpan singleDay; startDate = startDate.Date; histDate=startDate; if(dayCount<0)singleDay=new TimeSpan(-1,0,0,0); else singleDay=new TimeSpan(1,0,0,0); dayCount=dayCount<0?-dayCount:dayCount; while(histDates.Count GenerateHistoricalDates(DateTime startDate,DateTime endDate) { if (Utility.Epoch.Equals(startDate)||Utility.Epoch.Equals(endDate)) return null; startDate = startDate.Date; endDate = endDate.Date; List histDates = new List(); DateTime histDate; TimeSpan singleDay; bool reverse = false; if (endDate > startDate) { reverse = true; DateTime swap = endDate; endDate = startDate; startDate = swap; } histDate=startDate; singleDay=new TimeSpan(1,0,0,0); while(histDate>=endDate) { if(IsMarketOpen(ref histDate))histDates.Add(histDate); histDate=histDate.Subtract(singleDay); } if (reverse) histDates.Reverse(); return histDates; } // The function will figure out which date is the most recent and which one is the historical date. // Generally, make startDate the most recent and endDate the historical date. public List GenerateHistoricalDatesActual(DateTime startDate, DateTime endDate) { if (Utility.Epoch.Equals(startDate) || Utility.Epoch.Equals(endDate)) return null; startDate = startDate.Date; endDate = endDate.Date; List histDates = new List(); DateTime histDate; TimeSpan singleDay; bool reverse = false; if (endDate > startDate) { reverse = true; DateTime swap = endDate; endDate = startDate; startDate = swap; } histDate = startDate; singleDay = new TimeSpan(1, 0, 0, 0); while (histDate >= endDate) { histDates.Add(histDate); histDate = histDate.Subtract(singleDay); } if (reverse) histDates.Reverse(); return histDates; } private bool IsMarketOpen(ref DateTime dateTime) { if(IsWeekend(dateTime)||IsHoliday(dateTime))return false; return true; } public bool IsWeekend(DateTime dateTime) { if(DayOfWeek.Sunday==dateTime.DayOfWeek || DayOfWeek.Saturday==dateTime.DayOfWeek)return true; return false; } public bool IsHoliday(DateTime dateTime) { if(null==holidays)return false; return holidays.Contains(dateTime.Date); } } }