예제 #1
0
        private static DateTime GetPreviousTradingDay(List <CompiledScheduleTimeInterval>
                                                      compiledSchedule, CompiledScheduleTimeInterval currentInterval, DateTime currentDateTime)
        {
            if (currentInterval.Enabled())
            {
                return(currentDateTime.Date);
            }

            var timestampBeforeCurrentIntervalStart = currentInterval.Start.AddTicks(-1);

            // search for the interval just before the current interval started
            var previousInterval = compiledSchedule
                                   .Where(x => IsBetween(timestampBeforeCurrentIntervalStart, x.Start, x.End))
                                   .OrderByDescending(x => x.Schedule.Rank)
                                   .FirstOrDefault();

            // if trading was enabled, then at that moment was the last trading day
            if (previousInterval.Enabled())
            {
                return(timestampBeforeCurrentIntervalStart.Date);
            }

            // if no, there was one more disabled interval and we should go next
            return(GetPreviousTradingDay(compiledSchedule, previousInterval, previousInterval.Start));
        }
예제 #2
0
        private bool GetTradingEnabled(IEnumerable <CompiledScheduleTimeInterval> timeIntervals,
                                       out CompiledScheduleTimeInterval selectedInterval)
        {
            var currentDateTime = _dateService.Now();

            var intersecting = timeIntervals.Where(x => x.Start <= currentDateTime && currentDateTime < x.End);

            selectedInterval = intersecting
                               .OrderByDescending(x => x.Schedule.Rank)
                               .FirstOrDefault();

            return(selectedInterval?.Schedule.IsTradeEnabled ?? true);
        }
예제 #3
0
 public static CompiledScheduleTimeIntervalContract ToRabbitMqContract(this CompiledScheduleTimeInterval schedule)
 {
     return(new CompiledScheduleTimeIntervalContract
     {
         Schedule = new ScheduleSettingsContract
         {
             Id = schedule.Schedule.Id,
             Rank = schedule.Schedule.Rank,
             IsTradeEnabled = schedule.Schedule.IsTradeEnabled,
             PendingOrdersCutOff = schedule.Schedule.PendingOrdersCutOff,
         },
         Start = schedule.Start,
         End = schedule.End,
     });
 }
예제 #4
0
        private static DateTime GetNextTradingDay(List <CompiledScheduleTimeInterval>
                                                  compiledSchedule, CompiledScheduleTimeInterval currentInterval, DateTime currentDateTime, DateTime lastTradingDay)
        {
            // search for the interval right after the current interval finished
            var ordered = compiledSchedule
                          .Where(x => x.End > (currentInterval?.End ?? currentDateTime) ||
                                 currentInterval != null && x.Schedule.Rank > currentInterval.Schedule.Rank &&
                                 x.End > currentInterval.End)
                          .OrderBy(x => x.Start)
                          .ThenByDescending(x => x.Schedule.Rank)
                          .ToList();

            var nextInterval = ordered.FirstOrDefault();

            if (nextInterval == null)
            {
                if (!currentInterval.Enabled() && currentInterval.End.Date > lastTradingDay.Date)
                {
                    return(currentInterval.End);
                }
                else // means no any intervals (current or any in the future)
                {
                    return(currentDateTime.Date.AddDays(1));
                }
            }

            var stateIsChangedToEnabled = nextInterval.Schedule.IsTradeEnabled != currentInterval.Enabled() && nextInterval.Enabled();
            var intervalIsMissing       = currentInterval != null && nextInterval.Start > currentInterval.End;

            if (stateIsChangedToEnabled || intervalIsMissing && currentInterval.End.Date > lastTradingDay.Date)
            {
                // ReSharper disable once PossibleNullReferenceException
                // if status was changed and next is enabled, that means current interval is disable == it not null
                return(currentInterval.End);
            }

            // if we have long enabled interval with overnight, next day will start at 00:00:00
            if (currentInterval.Enabled() && currentDateTime.Date.AddDays(1) < nextInterval.Start)
            {
                return(currentDateTime.Date.AddDays(1));
            }

            return(GetNextTradingDay(compiledSchedule, nextInterval, nextInterval.End.AddTicks(1), lastTradingDay));
        }
예제 #5
0
        public bool TryGetPlatformCurrentDisabledInterval(out CompiledScheduleTimeInterval disabledInterval)
        {
            var platformSchedule = GetPlatformTradingSchedule();

            return(!GetTradingEnabled(platformSchedule, out disabledInterval));
        }
예제 #6
0
 public static bool Enabled(this CompiledScheduleTimeInterval compiledScheduleTimeInterval)
 {
     return(compiledScheduleTimeInterval?.Schedule.IsTradeEnabled ?? true);
 }