/// <summary> /// Gets the "opening" session's opening time, one for each day of the week. /// Session could potentially be on a previous day. /// </summary> /// <returns> /// A dictionary with keys corresponding to DayOfTheWeek, /// and values of the opening session for that day. /// </returns> public static Dictionary <int, InstrumentSession> SessionStartTimesByDay(this Instrument instrument) { var sessionStartTimes = new Dictionary <int, InstrumentSession>(); if (instrument.Sessions == null) { return(sessionStartTimes); } var dotwValues = MyUtils.GetEnumValues <DayOfTheWeek>(); var sessions = instrument.Sessions.OrderBy(x => x.OpeningTime).ToList(); foreach (DayOfTheWeek d in dotwValues) { if (sessions.Any(x => x.ClosingDay == d)) { //if there's a session starting on a different day, //that's the earliest one no matter the time InstrumentSession prevDaySession = sessions.FirstOrDefault(x => x.ClosingDay == d && x.OpeningDay != d); if (prevDaySession != null) { sessionStartTimes.Add((int)d, prevDaySession); } else { var session = sessions.First(x => x.ClosingDay == d); sessionStartTimes.Add((int)d, session); } } } return(sessionStartTimes); }
public void OverlapsReturnsTrueForIntradayOverlappingPeriods() { var m10To12 = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(10,0,0), ClosingDay = DayOfTheWeek.Monday, ClosingTime = new TimeSpan(12, 0, 0), }; var m8To14 = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(8, 0, 0), ClosingDay = DayOfTheWeek.Monday, ClosingTime = new TimeSpan(14, 0, 0), }; var m12To16 = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(12, 0, 0), ClosingDay = DayOfTheWeek.Monday, ClosingTime = new TimeSpan(16, 0, 0), }; var m12ToT12 = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(12, 0, 0), ClosingDay = DayOfTheWeek.Tuesday, ClosingTime = new TimeSpan(12, 0, 0), }; var s20ToM12 = new InstrumentSession { OpeningDay = DayOfTheWeek.Sunday, OpeningTime = new TimeSpan(20, 0, 0), ClosingDay = DayOfTheWeek.Monday, ClosingTime = new TimeSpan(12, 0, 0), }; //one contains the other Assert.IsTrue(m10To12.Overlaps(m8To14)); Assert.IsTrue(m8To14.Overlaps(m10To12)); //simple overlap Assert.IsTrue(m12To16.Overlaps(m8To14)); Assert.IsTrue(m8To14.Overlaps(m12To16)); //across days Assert.IsTrue(m12ToT12.Overlaps(m8To14)); Assert.IsTrue(m8To14.Overlaps(m12ToT12)); //across days and weeks Assert.IsTrue(s20ToM12.Overlaps(m8To14)); Assert.IsTrue(m8To14.Overlaps(s20ToM12)); }
public static InstrumentSession ToInstrumentSession(this ISession session) { var result = new InstrumentSession(); result.OpeningDay = session.OpeningDay; result.OpeningTime = TimeSpan.FromSeconds(session.OpeningTime.TotalSeconds); result.ClosingDay = session.ClosingDay; result.ClosingTime = TimeSpan.FromSeconds(session.ClosingTime.TotalSeconds); result.IsSessionEnd = session.IsSessionEnd; return result; }
public static InstrumentSession ToInstrumentSession(this ISession session) { var result = new InstrumentSession(); result.OpeningDay = session.OpeningDay; result.OpeningTime = TimeSpan.FromSeconds(session.OpeningTime.TotalSeconds); result.ClosingDay = session.ClosingDay; result.ClosingTime = TimeSpan.FromSeconds(session.ClosingTime.TotalSeconds); result.IsSessionEnd = session.IsSessionEnd; return(result); }
public void OverlapsReturnsFalseForNonOverlappingDailyPeriods() { var sundayToMonday = new InstrumentSession { OpeningDay = DayOfTheWeek.Sunday, ClosingDay = DayOfTheWeek.Monday }; var mondayToTuesday = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, ClosingDay = DayOfTheWeek.Tuesday }; var wednesdayToSaturday = new InstrumentSession { OpeningDay = DayOfTheWeek.Wednesday, ClosingDay = DayOfTheWeek.Saturday }; //intraweek Assert.IsFalse(wednesdayToSaturday.Overlaps(mondayToTuesday)); Assert.IsFalse(mondayToTuesday.Overlaps(wednesdayToSaturday)); //Across weeks Assert.IsFalse(sundayToMonday.Overlaps(wednesdayToSaturday)); Assert.IsFalse(wednesdayToSaturday.Overlaps(sundayToMonday)); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public object Clone() { var clone = new InstrumentSession { ID = ID, OpeningTime = TimeSpan.FromSeconds(OpeningTime.TotalSeconds), ClosingTime = TimeSpan.FromSeconds(ClosingTime.TotalSeconds), InstrumentID = InstrumentID, IsSessionEnd = IsSessionEnd, OpeningDay = OpeningDay, ClosingDay = ClosingDay }; return(clone); }
public void OverlapsReturnsTrueForDailyOverlaps() { var sundayToWednesday = new InstrumentSession { OpeningDay = DayOfTheWeek.Sunday, ClosingDay = DayOfTheWeek.Wednesday }; var mondayToTuesday = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, ClosingDay = DayOfTheWeek.Tuesday }; var tuesdayToThursday = new InstrumentSession { OpeningDay = DayOfTheWeek.Tuesday, ClosingDay = DayOfTheWeek.Thursday }; var mondayToFriday = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, ClosingDay = DayOfTheWeek.Friday }; var wednesdayToSaturday = new InstrumentSession { OpeningDay = DayOfTheWeek.Wednesday, ClosingDay = DayOfTheWeek.Saturday }; //completely covered, across weeks Assert.IsTrue(sundayToWednesday.Overlaps(mondayToTuesday)); Assert.IsTrue(mondayToTuesday.Overlaps(sundayToWednesday)); //completely covered, intraweek Assert.IsTrue(tuesdayToThursday.Overlaps(mondayToFriday)); Assert.IsTrue(mondayToFriday.Overlaps(tuesdayToThursday)); //partially covered, across weeks Assert.IsTrue(sundayToWednesday.Overlaps(tuesdayToThursday)); Assert.IsTrue(tuesdayToThursday.Overlaps(sundayToWednesday)); //partially covered, intraweek Assert.IsTrue(wednesdayToSaturday.Overlaps(tuesdayToThursday)); Assert.IsTrue(tuesdayToThursday.Overlaps(wednesdayToSaturday)); }
public void InSessionReturnsTrueWhenInsideMultiDaySession() { var session = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, ClosingDay = DayOfTheWeek.Tuesday, OpeningTime = new TimeSpan(8, 0, 0), ClosingTime = new TimeSpan(15, 0, 0) }; //Tuesday, April 1 var dt = new DateTime(2014, 4, 1, 7, 0, 0); Assert.IsTrue(dt.InSession(session)); }
public void InSessionReturnsFalseWhenAfterOneDaySessionOnNextDay() { var session = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, ClosingDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(8, 0, 0), ClosingTime = new TimeSpan(15, 0, 0) }; //Tuesday, April 1 var dt = new DateTime(2014, 4, 1, 12, 30, 5); Assert.IsFalse(dt.InSession(session)); }
public void InSessionReturnsFalseWhenAfterOneDaySessionOnSameDay() { var session = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, ClosingDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(8, 0, 0), ClosingTime = new TimeSpan(15, 0, 0) }; //Monday, March 31 var dt = new DateTime(2014, 3, 31, 16, 30, 5); Assert.IsFalse(dt.InSession(session)); }
private void AddSessionItemBtn_Click(object sender, RoutedEventArgs e) { var toAdd = new InstrumentSession { IsSessionEnd = true }; if (SelectedSessions.Count == 0) { toAdd.OpeningDay = DayOfTheWeek.Monday; toAdd.ClosingDay = DayOfTheWeek.Monday; } else { DayOfTheWeek maxDay = (DayOfTheWeek)Math.Min(6, SelectedSessions.Max(x => (int)x.OpeningDay) + 1); toAdd.OpeningDay = maxDay; toAdd.ClosingDay = maxDay; } SelectedSessions.Add(toAdd); }
/// <summary> /// Determine if a datetime falls inside a given session or not. /// </summary> public static bool InSession(this DateTime dt, InstrumentSession session) { int dotw = dt.DayOfWeek.ToInt(); if (session.OpeningDay > session.ClosingDay) { //the session spans multiple weeks if (dotw > (int)session.OpeningDay || dotw < (int)session.ClosingDay) { return true; } if (dotw == (int)session.OpeningDay && dt.TimeOfDay > session.OpeningTime) { return true; } if (dotw == (int)session.ClosingDay && dt.TimeOfDay <= session.ClosingTime) { return true; } } else { //session is intraweek if (dotw < (int)session.OpeningDay || dotw > (int)session.ClosingDay) { return false; } if (dotw == (int)session.OpeningDay && dt.TimeOfDay <= session.OpeningTime) { return false; } if (dotw == (int)session.ClosingDay && dt.TimeOfDay > session.ClosingTime) { return false; } return true; } return false; }
public void InSessionReturnsFalseWhenAfterWeekSpanningDaySession() { var session = new InstrumentSession { OpeningDay = DayOfTheWeek.Sunday, ClosingDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(15, 0, 0), ClosingTime = new TimeSpan(15, 0, 0) }; //Tuesday, April 2 var dt = new DateTime(2014, 4, 2, 13, 0, 0); Assert.IsFalse(dt.InSession(session)); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public object Clone() { var clone = new InstrumentSession { ID = ID, OpeningTime = TimeSpan.FromSeconds(OpeningTime.TotalSeconds), ClosingTime = TimeSpan.FromSeconds(ClosingTime.TotalSeconds), InstrumentID = InstrumentID, IsSessionEnd = IsSessionEnd, OpeningDay = OpeningDay, ClosingDay = ClosingDay }; return clone; }
public void OverlapsReturnsFalseForNonOverlappingIntradayPeriods() { var m10To12 = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(10, 0, 0), ClosingDay = DayOfTheWeek.Monday, ClosingTime = new TimeSpan(12, 0, 0), }; var m14To16 = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(14, 0, 0), ClosingDay = DayOfTheWeek.Monday, ClosingTime = new TimeSpan(16, 0, 0), }; var m14ToT11 = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(14, 0, 0), ClosingDay = DayOfTheWeek.Tuesday, ClosingTime = new TimeSpan(11, 0, 0), }; Assert.IsFalse(m10To12.Overlaps(m14To16)); Assert.IsFalse(m14To16.Overlaps(m10To12)); Assert.IsFalse(m10To12.Overlaps(m14ToT11)); Assert.IsFalse(m14ToT11.Overlaps(m10To12)); }
public void InSessionReturnsFalseWhenBeforeMultiDaySession() { var session = new InstrumentSession { OpeningDay = DayOfTheWeek.Monday, ClosingDay = DayOfTheWeek.Tuesday, OpeningTime = new TimeSpan(8, 0, 0), ClosingTime = new TimeSpan(15, 0, 0) }; //Monday, March 31 var dt = new DateTime(2014, 3, 31, 7, 0, 0); Assert.IsFalse(dt.InSession(session)); }
public void InSessionReturnsTrueWhenInsideWeekSpanningSessionInSecondWeek() { var session = new InstrumentSession { OpeningDay = DayOfTheWeek.Sunday, ClosingDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(15, 0, 0), ClosingTime = new TimeSpan(15, 0, 0) }; //Monday, March 31 var dt = new DateTime(2014, 3, 31, 13, 0, 0); Assert.IsTrue(dt.InSession(session)); }
/// <summary> /// Given a DateTime, calculates the DateTime of the next open and close of a given session. /// </summary> private static void SessionToDT(DateTime startingPoint, InstrumentSession session, out DateTime startingDT, out DateTime endingDT) { startingDT = SessionToDT(startingPoint, session, false); endingDT = SessionToDT(startingPoint, session, true); }
public void InSessionReturnsFalseWhenBeforeWeekSpanningDaySession() { var session = new InstrumentSession { OpeningDay = DayOfTheWeek.Sunday, ClosingDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(15, 0, 0), ClosingTime = new TimeSpan(15, 0, 0) }; //Sunday, March 30 var dt = new DateTime(2014, 3, 30, 13, 0, 0); Assert.IsFalse(dt.InSession(session)); }
private static DateTime SessionToDT(DateTime startingPoint, InstrumentSession session, bool closing) { DateTime currentDT = startingPoint; int targetDay = closing ? (int)session.ClosingDay : (int)session.OpeningDay; TimeSpan time = closing ? session.ClosingTime : session.OpeningTime; if (currentDT.DayOfWeek.ToInt() == targetDay && currentDT.TimeOfDay > session.ClosingTime) { currentDT = currentDT.AddDays(7); } while (currentDT.DayOfWeek.ToInt() != targetDay) { currentDT = currentDT.AddDays(1); } return currentDT.Date + time; }