public static int CountBusinessDaysBetween(DateTime start, DateTime end) { var startDate = new Date(start); var endDate = new Date(end); int count = 0; startDate += 1; while (startDate < endDate) { if (Calendar.isBusinessDay(startDate)) { count++; } startDate += 1; } return(count); }
private IEnumerable <TradingDay> PopulateTradingDays(DateTime start, DateTime end) { var symbols = _securityManager.Keys; var holidays = new HashSet <DateTime>(); foreach (var symbol in symbols) { var entry = _marketHoursDatabase.GetEntry(symbol.ID.Market, symbol, symbol.ID.SecurityType); foreach (var holiday in entry.ExchangeHours.Holidays) { holidays.Add(holiday.Date); } } var qlCalendar = new UnitedStates(); var options = symbols.Where(x => x.ID.SecurityType.IsOption()).ToList(); var futures = symbols.Where(x => x.ID.SecurityType == SecurityType.Future).ToList(); var totalDays = (int)(end.Date.AddDays(1.0) - start.Date).TotalDays; if (totalDays < 0) { throw new ArgumentException( $"TradingCalendar.PopulateTradingDays(): Total days is negative ({totalDays}), indicating reverse start and end times." + $" Check your usage of TradingCalendar to ensure proper arrangement of variables"); } foreach (var dayIdx in Enumerable.Range(0, totalDays)) { var currentDate = start.Date.AddDays(dayIdx); var publicHoliday = holidays.Contains(currentDate) || !qlCalendar.isBusinessDay(currentDate); var weekend = currentDate.DayOfWeek == DayOfWeek.Sunday || currentDate.DayOfWeek == DayOfWeek.Saturday; var businessDay = !publicHoliday && !weekend; yield return (new TradingDay { Date = currentDate, PublicHoliday = publicHoliday, Weekend = weekend, BusinessDay = businessDay, OptionExpirations = options.Where(x => x.ID.Date.Date == currentDate), FutureExpirations = futures.Where(x => x.ID.Date.Date == currentDate) }); } }
private IEnumerable <TradingDay> PopulateTradingDays(DateTime start, DateTime end) { var symbols = _securityManager.Keys; var holidays = new HashSet <DateTime>(); foreach (var symbol in symbols) { var entry = _marketHoursDatabase.GetEntry(symbol.ID.Market, symbol, symbol.ID.SecurityType); foreach (var holiday in entry.ExchangeHours.Holidays) { holidays.Add(holiday.Date); } } var qlCalendar = new UnitedStates(); var options = symbols.Where(x => x.ID.SecurityType == SecurityType.Option || x.ID.SecurityType == SecurityType.FutureOption).ToList(); var futures = symbols.Where(x => x.ID.SecurityType == SecurityType.Future).ToList(); foreach (var dayIdx in Enumerable.Range(0, (int)(end.Date.AddDays(1.0) - start.Date).TotalDays)) { var currentDate = start.Date.AddDays(dayIdx); var publicHoliday = holidays.Contains(currentDate) || !qlCalendar.isBusinessDay(currentDate); var weekend = currentDate.DayOfWeek == DayOfWeek.Sunday || currentDate.DayOfWeek == DayOfWeek.Saturday; var businessDay = !publicHoliday && !weekend; yield return (new TradingDay { Date = currentDate, PublicHoliday = publicHoliday, Weekend = weekend, BusinessDay = businessDay, OptionExpirations = options.Where(x => x.ID.Date.Date == currentDate), FutureExpirations = futures.Where(x => x.ID.Date.Date == currentDate) }); } }