Пример #1
0
        /// <summary>
        /// 是否是开盘时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool IsOpenTime(DateTime dt)
        {
            //周六凌晨
            var hhmm = dt.GetHHMM();

            if (dt.DayOfWeek == DayOfWeek.Saturday)
            {
                if (hhmm > 230)
                {
                    return(false);
                }
                else
                {
                    var ret = HolidayDao.IsHolidayNight(dt.AddDays(-1).ToDate());
                    return(!ret);
                }
            }
            if (!IsTradeDay(dt.ToDate()))
            {
                return(false);
            }
            if (dt.DayOfWeek == DayOfWeek.Monday && hhmm < 900)
            {
                return(false);
            }
            if (hhmm >= 2100)
            {
                var ret = HolidayDao.IsHolidayNight(dt.ToDate());
                return(!ret);
            }
            var t = new TimeSpan(dt.Hour, dt.Minute, dt.Second);

            foreach (KeyValuePair <TimeSpan, TimeSpan> item in CloseTime)
            {
                if (t > item.Key && t < item.Value)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// 根据开收盘时间判断是否交易时间
        /// 特别说明:已经在行情里使用,谨慎修改
        /// 作者:jincheng
        /// 修改时间:2017年8月11日 14:19:15
        /// </summary>
        /// <param name="time"></param>
        /// <param name="symbol"></param>
        /// <param name="defaultValue">出错时的默认值</param>
        /// <returns></returns>
        private static bool IsTradingTime(DateTime time, List <TradeTimeRange> timeRangeList, bool defaultValue = true)
        {
            var nowHhmmss           = time.GetHHMMSS();
            var nightTradeTimeRange = timeRangeList.FirstOrDefault(p => p.BeginTime >= NightBeginTime);

            if (time.DayOfWeek == DayOfWeek.Saturday)
            {
                if (nightTradeTimeRange == null)
                {
                    return(false);
                }
                if (nowHhmmss > nightTradeTimeRange.EndTime)
                {
                    return(false);
                }
            }

            if (time.DayOfWeek == DayOfWeek.Sunday)
            {
                return(false);
            }
            if (HolidayDao.IsHoliday(time.ToDate()))
            {
                return(false);
            }
            if (nowHhmmss >= 210000)
            {
                var isHolidayNight = HolidayDao.IsHolidayNight(time.ToDate());
                if (isHolidayNight)
                {
                    return(false);
                }
            }

            if (nowHhmmss >= 0 && nowHhmmss <= 30000)             //节假日前一夜及第二天凌晨 无交易。节假日前夜,且第二天非法定节假日如:2017.09.30
            {
                var isHolidayNight = HolidayDao.IsHolidayNight(time.AddDays(-1).ToDate());
                if (isHolidayNight)
                {
                    return(false);
                }
            }
            if (timeRangeList == null || timeRangeList.Count <= 0)
            {
                return(defaultValue);
            }

            var hhmmss = time.GetHHMMSS();

            //星期6需要特殊处理
            if (time.DayOfWeek == DayOfWeek.Saturday)
            {
                if (hhmmss >= 210000)
                {
                    return(false);
                }

                return(timeRangeList.Exists(
                           e =>
                           e.BeginTime >= NightBeginTime &&
                           e.EndTime < e.BeginTime && (hhmmss <= e.EndTime || hhmmss >= e.BeginTime)));
            }
            if (time.DayOfWeek == DayOfWeek.Monday)
            {
                var startTime = timeRangeList.Where(e => e.BeginTime < NightBeginTime).OrderBy(p => p.BeginTime).FirstOrDefault();
                if (startTime != null)
                {
                    if (hhmmss < startTime.BeginTime)
                    {
                        return(false);
                    }
                }
            }

            var isDay = timeRangeList.Exists(e => e.BeginTime < NightBeginTime && (e.EndTime > e.BeginTime && hhmmss >= e.BeginTime && hhmmss <= e.EndTime));             //白天

            if (isDay)
            {
                return(true);
            }
            var isNight = timeRangeList.Exists(e => e.BeginTime >= NightBeginTime && ((e.EndTime < e.BeginTime && (hhmmss <= e.EndTime || hhmmss >= e.BeginTime)) ||
                                                                                      (e.EndTime > e.BeginTime && hhmmss >= e.BeginTime && hhmmss <= e.EndTime)));//夜盘

            if (isNight)
            {
                return(true);
            }
            return(false);
        }