Exemple #1
0
        private void AddSession(InstrumentSession session = null)
        {
            if (session != null)
            {
                Sessions.Add(new SessionViewModel(session));
                Model.Sessions.Add(session);
                return;
            }

            var toAdd = new InstrumentSession {
                IsSessionEnd = true
            };

            if (Sessions.Count == 0)
            {
                toAdd.OpeningDay = DayOfTheWeek.Monday;
                toAdd.ClosingDay = DayOfTheWeek.Monday;
            }
            else
            {
                DayOfTheWeek maxDay = (DayOfTheWeek)Math.Min(6, Sessions.Max(x => (int)x.OpeningDay) + 1);
                toAdd.OpeningDay = maxDay;
                toAdd.ClosingDay = maxDay;
            }
            Sessions.Add(new SessionViewModel(toAdd));
            Model.Sessions.Add(toAdd);
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        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));
        }
Exemple #4
0
        /// <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);
        }
Exemple #5
0
        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));
        }
Exemple #6
0
        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));
        }
Exemple #7
0
        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));
        }
Exemple #8
0
        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));
        }
Exemple #9
0
        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));
        }
Exemple #10
0
        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));
        }
Exemple #11
0
        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));
        }
Exemple #12
0
        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));
        }
Exemple #13
0
        /// <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);
        }
Exemple #14
0
        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);
        }
Exemple #15
0
        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);
        }
Exemple #16
0
        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));
        }
Exemple #17
0
        private bool CheckIfLocalStorageCanSatisfyThisRequest(HistoricalDataRequest request, StoredDataInfo localDataInfo)
        {
            if (localDataInfo != null &&
                localDataInfo.LatestDate >= request.EndingDate &&
                localDataInfo.EarliestDate <= request.StartingDate)
            {
                dataStorage.RequestHistoricalData(request);
                return(true);
            }

            if (localDataInfo != null)
            {
                int dotw = request.Instrument.Expiration.Date.DayOfWeek.ToInt();
                InstrumentSession session = request.Instrument.Sessions?.First(x => (int)x.ClosingDay == dotw && x.IsSessionEnd);

                if (session != null &&
                    localDataInfo.LatestDate >= request.Instrument.Expiration.Date + session.ClosingTime)
                {
                    dataStorage.RequestHistoricalData(request);
                    return(true);
                }
            }
            return(false);
        }
Exemple #18
0
        /// <summary>
        /// Removes bars outside of regular trading hours, as defined by the provided sessions.
        /// </summary>
        /// <param name="data">The data to be filtered, ordered with the earliest bar first.</param>
        /// <param name="sessions"></param>
        /// <returns></returns>
        public static void Filter(List <OHLCBar> data, List <InstrumentSession> sessions)
        {
            if (data == null)
            {
                throw new NullReferenceException("data");
            }
            if (sessions == null)
            {
                throw new NullReferenceException("sessions");
            }
            if (sessions.Count == 0)
            {
                return;
            }
            if (data.Count == 0)
            {
                return;
            }

            sessions = sessions.OrderBy(x => x.OpeningDay).ThenBy(x => x.OpeningTime).ToList();

            //start by grabbing the first session
            var firstBar = data[0];
            InstrumentSession currentSession = FirstSessionAfter(firstBar.DT, sessions);
            int sessionIndex = sessions.IndexOf(currentSession);


            bool inSession = firstBar.DT.InSession(currentSession);

            DateTime nextOpeningDT, nextClosingDT;

            SessionToDT(firstBar.DT, currentSession, out nextOpeningDT, out nextClosingDT);

            int i = 0;

            while (i < data.Count)
            {
                if (inSession)
                {
                    //Currently in a session. Check if we have moved outside it
                    if (data[i].DT > nextClosingDT)
                    {
                        //find next session
                        sessionIndex   = sessionIndex < sessions.Count - 1 ? sessionIndex + 1 : 0;
                        currentSession = sessions[sessionIndex];
                        // Use the previous bar here, because if we jump over a session and we used the current bar,
                        // it gives us next week's times instead of this week's
                        SessionToDT(data[i - 1].DT, currentSession, out nextOpeningDT, out nextClosingDT);

                        //Is this bar already inside the next session?
                        if (data[i].DT > nextOpeningDT)
                        {
                            //check if we have "overshot", if not then we're in the session
                            if (data[i].DT < nextClosingDT)
                            {
                                i++;
                                continue;
                            }

                            //we have overshot the next session, start from scratch
                            currentSession = FirstSessionAfter(data[i].DT, sessions);
                            sessionIndex   = sessions.IndexOf(currentSession);
                            SessionToDT(data[i].DT, currentSession, out nextOpeningDT, out nextClosingDT);

                            if (data[i].DT.InSession(currentSession))
                            {
                                i++;
                                continue;
                            }
                        }

                        inSession = false;
                        data.RemoveAt(i);
                        continue;
                    }

                    i++;
                }
                else
                {
                    //Currently not in a session, check if this bar is inside the next session
                    if (data[i].DT > nextOpeningDT)
                    {
                        if (data[i].DT > nextClosingDT)
                        {
                            //the bar overshoots the next session, start from scratch
                            currentSession = FirstSessionAfter(data[i].DT, sessions);
                            sessionIndex   = sessions.IndexOf(currentSession);
                            SessionToDT(data[i].DT, currentSession, out nextOpeningDT, out nextClosingDT);

                            if (data[i].DT.InSession(currentSession))
                            {
                                inSession = true;
                                i++;
                                continue;
                            }
                        }
                        else
                        {
                            inSession = true;
                            i++;
                            continue;
                        }
                    }

                    data.RemoveAt(i);
                }
            }
        }
Exemple #19
0
 /// <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);
 }