Пример #1
0
        public void FiresEventAfterTimePassesViaScanWithMultipleResolutions()
        {
            TradeBar consolidated = null;
            var      period       = TimeSpan.FromMinutes(2);
            var      consolidator = new TradeBarConsolidator(period);

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13, 10, 20, 0);
            var time      = reference;

            for (int i = 0; i < 10; i++)
            {
                consolidator.Update(new TradeBar {
                    Time = time, Period = Time.OneSecond
                });
                time = time.AddSeconds(1);
                consolidator.Scan(time);
                Assert.IsNull(consolidated);
            }

            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneMinute
            });
            time = reference.Add(period);
            consolidator.Scan(time);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);
            Assert.AreEqual(period, consolidated.Period);

            consolidated = null;

            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneSecond
            });
            time = time.AddSeconds(1);
            consolidator.Scan(time);
            Assert.IsNull(consolidated);

            time = time.AddSeconds(-1);
            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneMinute
            });
            time = time.AddMinutes(1);
            consolidator.Scan(time);
            Assert.IsNull(consolidated);

            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneMinute
            });
            time = time.AddMinutes(1);
            consolidator.Scan(time);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference.AddMinutes(2), consolidated.Time);
            Assert.AreEqual(period, consolidated.Period);
        }
Пример #2
0
        public void ConsolidatorEmitsOffsetBarsCorrectly()
        {
            // This test is to cover an issue seen with the live data stack
            // The consolidator would fail to emit every other bar because of a
            // ms delay in data from a live stream
            var period = TimeSpan.FromHours(2);

            using var consolidator = new TradeBarConsolidator(period);
            var consolidatedBarsCount = 0;

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidatedBarsCount++;
            };

            var random = new Random();
            var time   = new DateTime(2015, 04, 13);

            // The bars time is accurate, covering the hour perfectly
            // But the emit time is slightly offset (the timeslice that contains the bar)
            // So add a random ms offset to the scan time
            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneHour
            });
            time = time.Add(period);
            consolidator.Scan(time.AddMilliseconds(random.Next(800)));

            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneHour
            });
            time = time.Add(period);
            consolidator.Scan(time.AddMilliseconds(random.Next(800)));

            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneHour
            });
            time = time.Add(period);
            consolidator.Scan(time.AddMilliseconds(random.Next(800)));

            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneHour
            });
            time = time.Add(period);
            consolidator.Scan(time.AddMilliseconds(random.Next(800)));

            // We should expect to see 4 bars emitted from the consolidator
            Assert.AreEqual(4, consolidatedBarsCount);
        }
Пример #3
0
        [TestCase(16)] // 4PM
        public void BarsEmitOnTime(int hour)
        {
            // This test just pushes one full hourly bar into a consolidator
            // and scans to see if it will emit immediately as expected

            using var consolidator = new TradeBarConsolidator(Time.OneHour);
            var      consolidatedBarsCount = 0;
            TradeBar latestBar             = null;
            var      time = new DateTime(2015, 04, 13, hour, 0, 0);

            consolidator.DataConsolidated += (sender, bar) =>
            {
                latestBar = bar;
                consolidatedBarsCount++;
            };

            // Update with one tradebar that ends at this time
            // This is to simulate getting a data bar for the last period
            consolidator.Update(new TradeBar {
                Time = time.Subtract(Time.OneMinute), Period = Time.OneMinute
            });

            // Assert that the bar hasn't emitted
            Assert.IsNull(latestBar);
            Assert.AreEqual(0, consolidatedBarsCount);

            // Scan afterwards (Like algorithmManager does)
            consolidator.Scan(time);

            // Assert that the bar emitted
            Assert.IsNotNull(latestBar);
            Assert.IsTrue(latestBar.EndTime == time);
            Assert.AreEqual(1, consolidatedBarsCount);
        }
Пример #4
0
        public void FiresEventAfterTimePassesViaScan()
        {
            TradeBar consolidated = null;
            var      period       = TimeSpan.FromDays(1);
            var      consolidator = new TradeBarConsolidator(period);

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13);

            consolidator.Update(new TradeBar {
                Time = reference.AddSeconds(45), Period = period
            });
            Assert.IsNull(consolidated);

            consolidator.Scan(reference + period);
            Assert.IsNotNull(consolidated);
        }
Пример #5
0
        public void ConsolidatorEmitsRegularly()
        {
            // This test just pushes through 1000 bars
            // and ensures that the emit time and count are correct
            var period                = TimeSpan.FromHours(1);
            var consolidator          = new TradeBarConsolidator(period);
            var consolidatedBarsCount = 0;
            var time = new DateTime(2015, 04, 13);

            consolidator.DataConsolidated += (sender, bar) =>
            {
                Assert.IsTrue(bar.EndTime == time);
                consolidatedBarsCount++;
            };

            PushBarsThrough(1000, Time.OneHour, consolidator, ref time);

            // Scan one last time so we can emit the 1000th bar
            consolidator.Scan(time);
            Assert.AreEqual(1000, consolidatedBarsCount);
        }
Пример #6
0
        public void FiresEventAfterTimePassesViaScan()
        {
            TradeBar consolidated = null;
            var      period       = TimeSpan.FromDays(2);

            using var consolidator         = new TradeBarConsolidator(period);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13);

            consolidator.Update(new TradeBar {
                Time = reference, Period = Time.OneDay
            });
            Assert.IsNull(consolidated);

            consolidator.Scan(reference + period);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);
        }
Пример #7
0
        public void ConsolidatedPeriodEqualsTimeBasedConsolidatorPeriod()
        {
            TradeBar consolidated = null;
            var      period       = TimeSpan.FromMinutes(2);
            var      consolidator = new TradeBarConsolidator(period);

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13, 10, 20, 0);
            var time      = reference;

            consolidator.Update(new TradeBar {
                Time = time, Period = Time.OneMinute
            });
            time = reference.Add(period);
            consolidator.Scan(time);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);
            Assert.AreEqual(period, consolidated.Period);
        }
Пример #8
0
        public void ConsolidatorEmitsOldBarsUsingScan()
        {
            // This test is to ensure that no bars get swallowed by the consolidator
            // even if it doesn't get the data on regular intervals.
            // We will use Consolidators Scan method to emit bars
            var period = TimeSpan.FromHours(1);

            using var consolidator = new TradeBarConsolidator(period);
            TradeBar latestConsolidated    = null;
            var      consolidatedBarsCount = 0;

            consolidator.DataConsolidated += (sender, bar) =>
            {
                latestConsolidated = bar;
                consolidatedBarsCount++;
            };

            var time = new DateTime(2015, 04, 13);

            // Push through one bar at 12:00AM to create the consolidators working bar
            PushBarsThrough(1, Time.OneMinute, consolidator, ref time);

            // There should be no emit, lets assert the current time and count
            Assert.IsTrue(time == new DateTime(2015, 04, 13, 0, 1, 0));
            Assert.AreEqual(0, consolidatedBarsCount);

            // Now advance time way past (3 Hours) the bar end time of 1AM
            time += TimeSpan.FromHours(3); // Time = 3:59AM now

            // Call scan with current time, it should emit the 12AM - 1AM Bar without any update
            consolidator.Scan(time);
            Assert.AreEqual(1, consolidatedBarsCount);
            Assert.IsTrue(latestConsolidated != null && latestConsolidated.Time == new DateTime(2015, 04, 13, 0, 0, 0));

            // WorkingBar should be null, ready for whatever data comes through next
            Assert.IsTrue(consolidator.WorkingBar == null);
        }
        public void FiresEventAfterTimePassesViaScanWithMultipleResolutions()
        {
            TradeBar consolidated = null;
            var period = TimeSpan.FromMinutes(2);
            var consolidator = new TradeBarConsolidator(period);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13, 10, 20, 0);
            var time = reference; 

            for (int i = 0; i < 10; i++)
            {
                consolidator.Update(new TradeBar {Time = time, Period = Time.OneSecond});
                time = time.AddSeconds(1);
                consolidator.Scan(time);
                Assert.IsNull(consolidated);
            }

            consolidator.Update(new TradeBar { Time = time, Period = Time.OneMinute });
            time = reference.Add(period);
            consolidator.Scan(time);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);
            Assert.AreEqual(period, consolidated.Period);

            consolidated = null;

            consolidator.Update(new TradeBar { Time = time, Period = Time.OneSecond });
            time = time.AddSeconds(1);
            consolidator.Scan(time);
            Assert.IsNull(consolidated);

            time = time.AddSeconds(-1);
            consolidator.Update(new TradeBar { Time = time, Period = Time.OneMinute });
            time = time.AddMinutes(1);
            consolidator.Scan(time);
            Assert.IsNull(consolidated);

            consolidator.Update(new TradeBar { Time = time, Period = Time.OneMinute });
            time = time.AddMinutes(1);
            consolidator.Scan(time);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference.AddMinutes(2), consolidated.Time);
            Assert.AreEqual(period, consolidated.Period);
        }
Пример #10
0
        public void ConsolidatedPeriodEqualsTimeBasedConsolidatorPeriod()
        {
            TradeBar consolidated = null;
            var period = TimeSpan.FromMinutes(2);
            var consolidator = new TradeBarConsolidator(period);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13, 10, 20, 0);
            var time = reference;

            consolidator.Update(new TradeBar { Time = time, Period = Time.OneMinute });
            time = reference.Add(period);
            consolidator.Scan(time);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);
            Assert.AreEqual(period, consolidated.Period);
        }
Пример #11
0
        public void FiresEventAfterTimePassesViaScan()
        {
            TradeBar consolidated = null;
            var period = TimeSpan.FromDays(1);
            var consolidator = new TradeBarConsolidator(period);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13);
            consolidator.Update(new TradeBar { Time = reference.AddSeconds(45), Period = period });
            Assert.IsNull(consolidated);

            consolidator.Scan(reference + period);
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);
        }