Пример #1
0
 /// <summary>
 /// Helper to extract market hours from the <see cref="_openHoursByDay"/> dictionary, filling
 /// in Closed instantes when not present
 /// </summary>
 private void SetMarketHoursForDay(DayOfWeek dayOfWeek, out LocalMarketHours localMarketHoursForDay)
 {
     if (!_openHoursByDay.TryGetValue(dayOfWeek, out localMarketHoursForDay))
     {
         // assign to our dictionary that we're closed this day, as well as our local field
         _openHoursByDay[dayOfWeek] = localMarketHoursForDay = LocalMarketHours.ClosedAllDay(dayOfWeek);
     }
 }
Пример #2
0
        /// <summary>
        /// Check whether the market is always open or not
        /// </summary>
        /// <returns>True if the market is always open, false otherwise</returns>
        private bool CheckIsMarketAlwaysOpen()
        {
            LocalMarketHours marketHours = null;

            for (var i = 0; i < daysOfWeek.Count; i++)
            {
                var day = daysOfWeek[i];
                switch (day)
                {
                case DayOfWeek.Sunday:
                    marketHours = _sunday;
                    break;

                case DayOfWeek.Monday:
                    marketHours = _monday;
                    break;

                case DayOfWeek.Tuesday:
                    marketHours = _tuesday;
                    break;

                case DayOfWeek.Wednesday:
                    marketHours = _wednesday;
                    break;

                case DayOfWeek.Thursday:
                    marketHours = _thursday;
                    break;

                case DayOfWeek.Friday:
                    marketHours = _friday;
                    break;

                case DayOfWeek.Saturday:
                    marketHours = _saturday;
                    break;
                }

                if (!marketHours.IsOpenAllDay)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Sets the regular market hours for the specified days If no days are specified then
        /// all days will be updated.
        /// </summary>
        /// <param name="marketHoursSegments">Specifies each segment of the market hours, such as premarket/market/postmark</param>
        /// <param name="days">The days of the week to set these times for</param>
        public void SetMarketHours(IEnumerable <MarketHoursSegment> marketHoursSegments, params DayOfWeek[] days)
        {
            if (days.IsNullOrEmpty())
            {
                days = Enum.GetValues(typeof(DayOfWeek)).OfType <DayOfWeek>().ToArray();
            }

            var marketHours = Hours.MarketHours.ToDictionary();

            marketHoursSegments = marketHoursSegments as IList <MarketHoursSegment> ?? marketHoursSegments.ToList();
            foreach (var day in days)
            {
                marketHours[day] = new LocalMarketHours(day, marketHoursSegments);
            }

            // create a new exchange hours instance for the new hours
            Hours = new SecurityExchangeHours(Hours.TimeZone, Hours.Holidays, marketHours, Hours.EarlyCloses, Hours.LateOpens);
        }
Пример #4
0
        /// <summary>
        /// Sets the regular market hours for the specified days If no days are specified then
        /// all days will be updated.
        /// </summary>
        /// <param name="extendedMarketOpen">The time of day the pre market opens</param>
        /// <param name="marketOpen">The time of day the market opens</param>
        /// <param name="marketClose">The time of day the market closes</param>
        /// <param name="extendedMarketClose">The time of day the post market opens</param>
        /// <param name="days">The days of the week to set these times for</param>
        public void SetMarketHours(TimeSpan extendedMarketOpen, TimeSpan marketOpen, TimeSpan marketClose, TimeSpan extendedMarketClose, params DayOfWeek[] days)
        {
            if (days.IsNullOrEmpty())
            {
                days = Enum.GetValues(typeof(DayOfWeek)).OfType <DayOfWeek>().ToArray();
            }

            // if we specify close as 1 tick before the day rolls over, the exchange is still
            // considered to be open all day,so set it to one day and this impl will make it OpenAllDay
            if (extendedMarketOpen == marketOpen && marketOpen == TimeSpan.Zero && extendedMarketClose == marketClose && marketClose.Ticks == Time.OneDay.Ticks - 1)
            {
                marketClose = Time.OneDay;
            }

            // make sure extended hours are outside of regular hours
            extendedMarketOpen  = TimeSpan.FromTicks(Math.Min(extendedMarketOpen.Ticks, marketOpen.Ticks));
            extendedMarketClose = TimeSpan.FromTicks(Math.Max(extendedMarketClose.Ticks, marketClose.Ticks));

            var marketHours = _exchangeHours.MarketHours.ToDictionary();

            foreach (var day in days)
            {
                if (marketOpen == TimeSpan.Zero && marketClose == TimeSpan.Zero)
                {
                    marketHours[day] = LocalMarketHours.ClosedAllDay(day);
                }
                else if (marketOpen == TimeSpan.Zero && marketClose == Time.OneDay)
                {
                    marketHours[day] = LocalMarketHours.OpenAllDay(day);
                }
                else
                {
                    marketHours[day] = new LocalMarketHours(day, extendedMarketOpen, marketOpen, marketClose, extendedMarketClose);
                }
            }

            // create a new exchange hours instance for the new hours
            _exchangeHours = new SecurityExchangeHours(_exchangeHours.TimeZone, _exchangeHours.Holidays, marketHours);
        }
Пример #5
0
        public static SecurityExchangeHours CreateUsEquitySecurityExchangeHours()
        {
            var sunday = LocalMarketHours.ClosedAllDay(DayOfWeek.Sunday);
            var monday = new LocalMarketHours(DayOfWeek.Monday, new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0));
            var tuesday = new LocalMarketHours(DayOfWeek.Tuesday, new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0));
            var wednesday = new LocalMarketHours(DayOfWeek.Wednesday, new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0));
            var thursday = new LocalMarketHours(DayOfWeek.Thursday, new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0));
            var friday = new LocalMarketHours(DayOfWeek.Friday, new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0));
            var saturday = LocalMarketHours.ClosedAllDay(DayOfWeek.Saturday);

            var exchangeHours = new SecurityExchangeHours(TimeZones.NewYork, USHoliday.Dates.Select(x => x.Date), new[]
            {
                sunday, monday, tuesday, wednesday, thursday, friday, saturday
            }.ToDictionary(x => x.DayOfWeek));
            return exchangeHours;
        }
Пример #6
0
        public static SecurityExchangeHours CreateForexSecurityExchangeHours()
        {
            var sunday = new LocalMarketHours(DayOfWeek.Sunday, new TimeSpan(17, 0, 0), TimeSpan.FromTicks(Time.OneDay.Ticks - 1));
            var monday = LocalMarketHours.OpenAllDay(DayOfWeek.Monday);
            var tuesday = LocalMarketHours.OpenAllDay(DayOfWeek.Tuesday);
            var wednesday = LocalMarketHours.OpenAllDay(DayOfWeek.Wednesday);
            var thursday = LocalMarketHours.OpenAllDay(DayOfWeek.Thursday);
            var friday = new LocalMarketHours(DayOfWeek.Friday, TimeSpan.Zero, new TimeSpan(17, 0, 0));
            var saturday = LocalMarketHours.ClosedAllDay(DayOfWeek.Saturday);

            var exchangeHours = new SecurityExchangeHours(TimeZones.NewYork, USHoliday.Dates.Select(x => x.Date), new[]
            {
                sunday, monday, tuesday, wednesday, thursday, friday//, saturday
            }.ToDictionary(x => x.DayOfWeek));
            return exchangeHours;
        }
Пример #7
0
        /// <summary>
        /// Helper to access the market hours field based on the day of week
        /// </summary>
        /// <param name="localDateTime">The local date time to retrieve market hours for</param>
        public LocalMarketHours GetMarketHours(DateTime localDateTime)
        {
            LocalMarketHours marketHours;

            switch (localDateTime.DayOfWeek)
            {
            case DayOfWeek.Sunday:
                marketHours = _sunday;
                break;

            case DayOfWeek.Monday:
                marketHours = _monday;
                break;

            case DayOfWeek.Tuesday:
                marketHours = _tuesday;
                break;

            case DayOfWeek.Wednesday:
                marketHours = _wednesday;
                break;

            case DayOfWeek.Thursday:
                marketHours = _thursday;
                break;

            case DayOfWeek.Friday:
                marketHours = _friday;
                break;

            case DayOfWeek.Saturday:
                marketHours = _saturday;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(localDateTime), localDateTime, null);
            }

            // If the earlyCloseTime is between a segment, change the close time with it
            // and add it after the segments prior to the earlyCloseTime
            // Otherwise, just take the segments prior to the earlyCloseTime
            if (_earlyCloses.TryGetValue(localDateTime.Date, out var earlyCloseTime))
            {
                var index = marketHours.Segments.Count;
                MarketHoursSegment newSegment = null;
                for (var i = 0; i < marketHours.Segments.Count; i++)
                {
                    var segment = marketHours.Segments[i];
                    if (segment.Start <= earlyCloseTime && earlyCloseTime <= segment.End)
                    {
                        newSegment = new MarketHoursSegment(segment.State, segment.Start, earlyCloseTime);
                        index      = i;
                        break;
                    }
                    else if (earlyCloseTime < segment.Start)
                    {
                        // we will drop any remaining segment starting by this one
                        index = i - 1;
                        break;
                    }
                }

                var newSegments = new List <MarketHoursSegment>(marketHours.Segments.Take(index));
                if (newSegment != null)
                {
                    newSegments.Add(newSegment);
                }
                marketHours = new LocalMarketHours(localDateTime.DayOfWeek, newSegments);
            }

            // If the lateOpenTime is between a segment, change the start time with it
            // and add it before the segments previous to the lateOpenTime
            // Otherwise, just take the segments previous to the lateOpenTime
            if (_lateOpens.TryGetValue(localDateTime.Date, out var lateOpenTime))
            {
                var index       = 0;
                var newSegments = new List <MarketHoursSegment>();
                for (var i = 0; i < marketHours.Segments.Count; i++)
                {
                    var segment = marketHours.Segments[i];
                    if (segment.Start <= lateOpenTime && lateOpenTime <= segment.End)
                    {
                        newSegments.Add(new (segment.State, lateOpenTime, segment.End));
                        index = i + 1;
                        break;
                    }
                    else if (lateOpenTime < segment.Start)
                    {
                        index = i;
                        break;
                    }
                }

                newSegments.AddRange(marketHours.Segments.TakeLast(marketHours.Segments.Count - index));
                marketHours = new LocalMarketHours(localDateTime.DayOfWeek, newSegments);
            }

            return(marketHours);
        }
Пример #8
0
        /// <summary>
        /// Sets the regular market hours for the specified days If no days are specified then
        /// all days will be updated.
        /// </summary>
        /// <param name="extendedMarketOpen">The time of day the pre market opens</param>
        /// <param name="marketOpen">The time of day the market opens</param>
        /// <param name="marketClose">The time of day the market closes</param>
        /// <param name="extendedMarketClose">The time of day the post market opens</param>
        /// <param name="days">The days of the week to set these times for</param>
        public void SetMarketHours(TimeSpan extendedMarketOpen, TimeSpan marketOpen, TimeSpan marketClose, TimeSpan extendedMarketClose, params DayOfWeek[] days)
        {
            if (days.IsNullOrEmpty()) days = Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>().ToArray();

            // if we specify close as 1 tick before the day rolls over, the exchange is still
            // considered to be open all day,so set it to one day and this impl will make it OpenAllDay
            if (extendedMarketOpen == marketOpen && marketOpen == TimeSpan.Zero && extendedMarketClose == marketClose && marketClose.Ticks == Time.OneDay.Ticks - 1)
            {
                marketClose = Time.OneDay;
            }
            
            // make sure extended hours are outside of regular hours
            extendedMarketOpen = TimeSpan.FromTicks(Math.Min(extendedMarketOpen.Ticks, marketOpen.Ticks));
            extendedMarketClose = TimeSpan.FromTicks(Math.Max(extendedMarketClose.Ticks, marketClose.Ticks));

            var marketHours = _exchangeHours.MarketHours.ToDictionary();
            foreach (var day in days)
            {
                if (marketOpen == TimeSpan.Zero && marketClose == TimeSpan.Zero)
                {
                    marketHours[day] = LocalMarketHours.ClosedAllDay(day);
                }
                else if (marketOpen == TimeSpan.Zero && marketClose == Time.OneDay)
                {
                    marketHours[day] = LocalMarketHours.OpenAllDay(day);
                }
                else
                {
                    marketHours[day] = new LocalMarketHours(day, extendedMarketOpen, marketOpen, marketClose, extendedMarketClose);
                }
            }

            // create a new exchange hours instance for the new hours
            _exchangeHours = new SecurityExchangeHours(_exchangeHours.TimeZone, _exchangeHours.Holidays, marketHours);
        }
Пример #9
0
 /// <summary>
 /// Helper to extract market hours from the <see cref="_openHoursByDay"/> dictionary, filling
 /// in Closed instantes when not present
 /// </summary>
 private void SetMarketHoursForDay(DayOfWeek dayOfWeek, out LocalMarketHours localMarketHoursForDay)
 {
     if (!_openHoursByDay.TryGetValue(dayOfWeek, out localMarketHoursForDay))
     {
         // assign to our dictionary that we're closed this day, as well as our local field
         _openHoursByDay[dayOfWeek] = localMarketHoursForDay = LocalMarketHours.ClosedAllDay(dayOfWeek);
     }
 }
Пример #10
0
        /// <summary>
        /// Sets the regular market hours for the specified days If no days are specified then
        /// all days will be updated.
        /// </summary>
        /// <param name="extendedMarketOpen">The time of day the pre market opens</param>
        /// <param name="marketOpen">The time of day the market opens</param>
        /// <param name="marketClose">The time of day the market closes</param>
        /// <param name="extendedMarketClose">The time of day the post market opens</param>
        /// <param name="days">The days of the week to set these times for</param>
        public void SetMarketHours(TimeSpan extendedMarketOpen, TimeSpan marketOpen, TimeSpan marketClose, TimeSpan extendedMarketClose, params DayOfWeek[] days)
        {
            if (days.IsNullOrEmpty()) days = Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>().ToArray();

            // make sure extended hours are outside of regular hours
            extendedMarketOpen = TimeSpan.FromTicks(Math.Min(extendedMarketOpen.Ticks, marketOpen.Ticks));
            extendedMarketClose = TimeSpan.FromTicks(Math.Max(extendedMarketClose.Ticks, marketClose.Ticks));

            var marketHours = _exchangeHours.MarketHours.ToDictionary();
            foreach (var day in days)
            {
                if (marketOpen == TimeSpan.Zero && marketClose == TimeSpan.Zero)
                {
                    marketHours[day] = LocalMarketHours.ClosedAllDay(day);
                }
                else if (marketOpen == TimeSpan.Zero && marketClose == Time.OneDay)
                {
                    marketHours[day] = LocalMarketHours.OpenAllDay(day);
                }
                else
                {
                    marketHours[day] = new LocalMarketHours(day, extendedMarketOpen, marketOpen, marketClose, extendedMarketClose);
                }
            }

            // create a new exchange hours instance for the new hours
            _exchangeHours = new SecurityExchangeHours(_exchangeHours.TimeZone, _exchangeHours.Holidays, marketHours);
        }
Пример #11
0
        /// <summary>
        /// Sets the regular market hours for the specified days If no days are specified then
        /// all days will be updated.
        /// </summary>
        /// <param name="marketHoursSegments">Specifies each segment of the market hours, such as premarket/market/postmark</param>
        /// <param name="days">The days of the week to set these times for</param>
        public void SetMarketHours(IEnumerable<MarketHoursSegment> marketHoursSegments, params DayOfWeek[] days)
        {
            if (days.IsNullOrEmpty()) days = Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>().ToArray();
            
            var marketHours = _exchangeHours.MarketHours.ToDictionary();
            marketHoursSegments = marketHoursSegments as IList<MarketHoursSegment> ?? marketHoursSegments.ToList();
            foreach (var day in days)
            {
                marketHours[day] = new LocalMarketHours(day, marketHoursSegments);
            }

            // create a new exchange hours instance for the new hours
            _exchangeHours = new SecurityExchangeHours(_exchangeHours.TimeZone, _exchangeHours.Holidays, marketHours);
        }