Skip to content

Commit

Permalink
Check time range on daily bases for weekly session configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed May 8, 2023
1 parent c4e8171 commit 47bcb50
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
36 changes: 30 additions & 6 deletions QuickFIXn/SessionSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SessionSchedule
public System.TimeSpan EndTime { get; private set; }

public bool WeeklySession { get; private set; }
public bool UseDailyTimeCheck { get; private set; }
public System.DayOfWeek StartDay { get; private set; }
public System.DayOfWeek EndDay { get; private set; }

Expand Down Expand Up @@ -73,7 +74,9 @@ public bool IsSessionTime(System.DateTime utc)

System.DateTime adjusted = AdjustUtcDateTime(utc);

if (WeeklySession)
if (UseDailyTimeCheck && WeeklySession)
return CheckDay(adjusted) && CheckTime(adjusted.TimeOfDay);
else if (WeeklySession)
return CheckDay(adjusted);
else
return CheckTime(adjusted.TimeOfDay);
Expand All @@ -95,19 +98,26 @@ public DateTime NextEndTime(DateTime utc)
throw new ArgumentException("Only UTC time is supported", "time");

DateTime d = AdjustUtcDateTime(utc);
DateTime end = DateTime.MinValue;
DateTime end = new DateTime(d.Year, d.Month, d.Day, EndTime.Hours, EndTime.Minutes, EndTime.Seconds, d.Kind);

if (WeeklySession)
if (UseDailyTimeCheck && WeeklySession)
{
if(DateTime.Compare(d, end) > 0) // d is later than end
end = end.AddDays(1);
while (!CheckDay(end))
{
end = end.AddDays(1);
}
}
else if (WeeklySession)
{
end = new DateTime(d.Year, d.Month, d.Day, EndTime.Hours, EndTime.Minutes, EndTime.Seconds, d.Kind);
while (end.DayOfWeek != EndDay)
end = end.AddDays(1);
if (DateTime.Compare(d, end) > 0) // d is later than end
end = end.AddDays(7);
}
else
{
end = new DateTime(d.Year, d.Month, d.Day, EndTime.Hours, EndTime.Minutes, EndTime.Seconds, d.Kind);
if (DateTime.Compare(d, end) > 0) // d is later than end
end = end.AddDays(1);
}
Expand All @@ -132,7 +142,16 @@ public DateTime LastEndTime(DateTime utc)
throw new ArgumentException("Only UTC time is supported", "time");

DateTime n = NextEndTime(utc);
if (WeeklySession)

if (UseDailyTimeCheck && WeeklySession)
{
n = n.AddDays(-1);
while(!CheckDay(n))
{
n = n.AddDays(-1);
}
}
else if (WeeklySession)
n = n.AddDays(-7);
else
n = n.AddDays(-1);
Expand Down Expand Up @@ -251,6 +270,11 @@ public SessionSchedule(QuickFix.Dictionary settings)
WeeklySession = true;
}

if (WeeklySession && settings.Has(SessionSettings.USE_DAILY_TIME_CHECK) && settings.GetBool(SessionSettings.USE_DAILY_TIME_CHECK))
{
UseDailyTimeCheck = true;
}

if (settings.Has(SessionSettings.USE_LOCAL_TIME))
{
UseLocalTime = settings.GetBool(SessionSettings.USE_LOCAL_TIME);
Expand Down
1 change: 1 addition & 0 deletions QuickFIXn/SessionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SessionSettings
public const string USE_DATA_DICTIONARY = "UseDataDictionary";
public const string NON_STOP_SESSION = "NonStopSession";
public const string USE_LOCAL_TIME = "UseLocalTime";
public const string USE_DAILY_TIME_CHECK = "UseDailyTimeCheck";
public const string TIME_ZONE = "TimeZone";
public const string START_DAY = "StartDay";
public const string END_DAY = "EndDay";
Expand Down
54 changes: 54 additions & 0 deletions UnitTests/SessionScheduleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,5 +714,59 @@ public void testAdjustDateTime()
Util.UtcDateTimeSerializerTests.AssertHackyDateTimeEquality(d5expected, d5actual);
Assert.AreEqual(DateTimeKind.Local, d5actual.Kind);
}



[Test]
public void IsSessionTime_should_return_true_for_weekly_session_only_when_USE_DAILY_TIME_CHECK_is_true_and_date_and_time_in_range()
{
QuickFix.Dictionary settings = new QuickFix.Dictionary();
settings.SetString(QuickFix.SessionSettings.START_TIME, "06:00:00");
settings.SetString(QuickFix.SessionSettings.END_TIME, "17:30:00");
settings.SetString(QuickFix.SessionSettings.USE_DAILY_TIME_CHECK, "Y");

settings.SetDay(QuickFix.SessionSettings.START_DAY, System.DayOfWeek.Monday);
settings.SetDay(QuickFix.SessionSettings.END_DAY, System.DayOfWeek.Friday);
QuickFix.SessionSchedule sched = new QuickFix.SessionSchedule(settings);

//a Sunday
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 23, 06, 00, 00, DateTimeKind.Utc)));
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 23, 17, 30, 00, DateTimeKind.Utc)));


// Monday
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 24, 05, 59, 59, 999, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 24, 06, 00, 00, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 24, 17, 30, 00, DateTimeKind.Utc)));
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 24, 17, 30, 00, 001, DateTimeKind.Utc)));

// Tuesday
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 25, 05, 59, 59, 999, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 25, 06, 00, 00, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 25, 17, 30, 00, DateTimeKind.Utc)));
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 25, 17, 30, 00, 001, DateTimeKind.Utc)));

// Wednesday
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 26, 05, 59, 59, 999, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 26, 06, 00, 00, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 26, 17, 30, 00, DateTimeKind.Utc)));
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 26, 17, 30, 00, 001, DateTimeKind.Utc)));

// Thursday
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 27, 05, 59, 59, 999, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 27, 06, 00, 00, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 27, 17, 30, 00, DateTimeKind.Utc)));
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 27, 17, 30, 00, 001, DateTimeKind.Utc)));

// Friday
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 28, 05, 59, 59, 999, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 28, 06, 00, 00, DateTimeKind.Utc)));
Assert.IsTrue(sched.IsSessionTime(new DateTime(2019, 06, 28, 17, 30, 00, DateTimeKind.Utc)));
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 28, 17, 30, 00, 001, DateTimeKind.Utc)));

// Saturday
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 23, 06, 00, 00, DateTimeKind.Utc)));
Assert.IsFalse(sched.IsSessionTime(new DateTime(2019, 06, 23, 17, 30, 00, DateTimeKind.Utc)));
}
}
}

0 comments on commit 47bcb50

Please sign in to comment.