Exemplo n.º 1
0
        static MarketHours()
        {
            LoadHoliday();

            // check if today is weekend
            isWeekend = DateTime.Today.DayOfWeek == DayOfWeek.Saturday || DateTime.Today.DayOfWeek == DayOfWeek.Sunday;

            // Get LastMarketDay
            var lastDate = DateTime.Today;

            while (lastDate.DayOfWeek == DayOfWeek.Saturday ||
                   lastDate.DayOfWeek == DayOfWeek.Sunday ||
                   _holidays.Any(o => o.Key == lastDate.Date && o.Value == false))
            {
                lastDate = lastDate.AddDays(-1);
            }
            LastMarketDay = lastDate;

            LastMarketDayCloseEarly = IsCloseEarly(LastMarketDay);

            // Get PreviousMarketDay
            // Get LastMarketDay
            lastDate = DateTime.Today.AddDays(-1);
            while (lastDate.DayOfWeek == DayOfWeek.Saturday ||
                   lastDate.DayOfWeek == DayOfWeek.Sunday ||
                   _holidays.Any(o => o.Key == lastDate.Date && o.Value == false))
            {
                lastDate = lastDate.AddDays(-1);
            }
            PreviousMarketDay           = lastDate;
            PreviousMarketDayCloseEarly = IsCloseEarly(PreviousMarketDay);

            StatusCode = GetStatus();
        }
Exemplo n.º 2
0
        public static MarketHoursCode GetStatus(DateTime currentTime)
        {
            var estTime = GetLocalTimeToEasternTime(currentTime);

            preMarket   = GetEasternTimeToLocalTime(LastMarketDay.AddHours(8));
            marketOpen  = GetEasternTimeToLocalTime(LastMarketDay.AddHours(9).AddMinutes(30));
            marketClose = GetEasternTimeToLocalTime(LastMarketDay.AddHours(16));
            afterHours  = GetEasternTimeToLocalTime(LastMarketDay.AddHours(20));

            //Debug.Print("Eastern Standard Time: {0:HH:mm:ss}", estTime);

            if (estTime.DayOfWeek == DayOfWeek.Saturday ||
                estTime.DayOfWeek == DayOfWeek.Sunday)
            {
                StatusCode = MarketHoursCode.Closed;
                return(MarketHoursCode.Closed);
            }

            if (_holidays != null && _holidays.ContainsKey(estTime.Date))
            {
                var closeEarly = _holidays[estTime.Date];
                if (closeEarly)
                {
                    preMarket   = GetEasternTimeToLocalTime(LastMarketDay.AddHours(8));
                    marketOpen  = GetEasternTimeToLocalTime(LastMarketDay.AddHours(9).AddMinutes(30));
                    marketClose = GetEasternTimeToLocalTime(LastMarketDay.AddHours(13));
                    afterHours  = GetEasternTimeToLocalTime(LastMarketDay.AddHours(17));
                }
                else
                {
                    StatusCode = MarketHoursCode.Closed;
                    return(MarketHoursCode.Closed);
                }
            }

            if (marketOpen == DateTime.MinValue || marketClose == DateTime.MinValue)
            {
                StatusCode = MarketHoursCode.Closed;
                return(MarketHoursCode.Closed);
            }

            if (estTime >= preMarket && marketOpen > estTime)
            {
                StatusCode = MarketHoursCode.PreMarket;
                return(MarketHoursCode.PreMarket);
            }

            if (estTime >= marketOpen && estTime < marketClose)
            {
                StatusCode = MarketHoursCode.Opened;
                return(MarketHoursCode.Opened);
            }

            if (estTime >= marketClose && estTime < afterHours)
            {
                StatusCode = MarketHoursCode.AfterHours;
                return(MarketHoursCode.AfterHours);
            }

            StatusCode = MarketHoursCode.Closed;
            return(MarketHoursCode.Closed);
        }