Beispiel #1
0
        public void ScheduleTrigger(SqTrigger p_trigger, bool p_isMarketHoursValid, bool p_isMarketTradingDay, DateTime p_marketOpenTimeUtc, DateTime p_marketCloseTimeUtc)
        {
            DateTime?proposedTime = CalcNextTriggerTime(p_trigger, p_isMarketHoursValid, p_isMarketTradingDay, p_marketOpenTimeUtc, p_marketCloseTimeUtc);

            if (proposedTime != null)
            {
                bool doSetTimer = true;
                if (p_trigger.NextScheduleTimeUtc != null)
                {
                    TimeSpan timeSpan = ((DateTime)p_trigger.NextScheduleTimeUtc > (DateTime)proposedTime) ? (DateTime)p_trigger.NextScheduleTimeUtc - (DateTime)proposedTime : (DateTime)proposedTime - (DateTime)p_trigger.NextScheduleTimeUtc;
                    if (timeSpan.TotalMilliseconds < 1000.0)    // if the proposedTime is not significantly different that the scheduledTime
                    {
                        doSetTimer = false;
                    }
                }
                if (doSetTimer)
                {
                    p_trigger.NextScheduleTimeUtc = proposedTime;

                    StrongAssert.True((DateTime)p_trigger.NextScheduleTimeUtc > DateTime.UtcNow, Severity.ThrowException, "nextScheduleTimeUtc > DateTime.UtcNow");
                    p_trigger.Timer.Change((DateTime)p_trigger.NextScheduleTimeUtc - DateTime.UtcNow, TimeSpan.FromMilliseconds(-1.0));
                }
            }
            // Warn() temporarily to show it on Console
            //Console.WriteLine($"{DateTime.UtcNow.ToString("dd'T'HH':'mm':'ss")}: Task '" + p_trigger.TriggeredTask.Name + "' next time: " + ((p_trigger.NextScheduleTimeUtc != null) ? ((DateTime)p_trigger.NextScheduleTimeUtc).ToString("dd'T'HH':'mm':'ss") : "null"));
            Utils.Logger.Info("Trigger '" + String.Concat(p_trigger.SqTask !.Name, ".", p_trigger.Name) + "' next time: " + ((p_trigger.NextScheduleTimeUtc != null) ? ((DateTime)p_trigger.NextScheduleTimeUtc).ToString("dd'T'HH':'mm':'ss") : "null"));
        }
Beispiel #2
0
        private DateTime?CalcNextTriggerTime(SqTrigger p_trigger, bool p_isMarketHoursValid, bool p_isMarketTradingDay, DateTime p_marketOpenTimeUtc, DateTime p_marketCloseTimeUtc)
        {
            // if it is scheduled 5 seconds from now, just forget it (1 seconds was not enough)
            // once the timer was set to ellapse at 20:30:00, but it ellapsed at 20:29:58sec.5, so the trade was scheduled again, because it was later than 1 sec
            DateTime tresholdNowTime = DateTime.UtcNow.AddSeconds(5);
            DateTime proposedTime    = DateTime.MinValue;

            if (p_trigger.TriggerType == TriggerType.Daily)
            {
                if (p_trigger.Start.Base == RelativeTimeBase.BaseOnAbsoluteTimeMidnightUtc)
                {
                    proposedTime = DateTime.UtcNow.Date + p_trigger.Start.TimeOffset;
                }
            }
            else if (p_trigger.TriggerType == TriggerType.DailyOnUsaMarketDay)
            {
                if (p_isMarketHoursValid && p_isMarketTradingDay)  // in this case market open and close times are not given
                {
                    if (p_trigger.Start.Base == RelativeTimeBase.BaseOnUsaMarketOpen)
                    {
                        proposedTime = p_marketOpenTimeUtc + p_trigger.Start.TimeOffset;
                    }

                    if (p_trigger.Start.Base == RelativeTimeBase.BaseOnUsaMarketClose)
                    {
                        proposedTime = p_marketCloseTimeUtc + p_trigger.Start.TimeOffset;
                    }
                }
            }
            else if (p_trigger.TriggerType == TriggerType.AtApplicationStartup)
            {
                proposedTime = m_schedulerStartupTime + p_trigger.Start.TimeOffset;
            }
            else if (p_trigger.TriggerType == TriggerType.Periodic)
            {
                if (p_trigger.Start.Base == RelativeTimeBase.BaseOnAbsoluteTimeAtEveryHourUtc)
                {
                    DateTime utcNow        = DateTime.UtcNow;
                    DateTime nextWholeHour = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, utcNow.Hour, 0, 0).AddHours(1);
                    proposedTime = nextWholeHour + p_trigger.Start.TimeOffset;
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            if (proposedTime > tresholdNowTime)
            {
                return(proposedTime);
            }
            return(null);
        }