Exemple #1
0
        public void TriggersScheduledEventsSameTimeInOrder()
        {
            var realTimeHandler = new BacktestingRealTimeHandler();

            realTimeHandler.Setup(new AlgorithmStub(new NullDataFeed()),
                                  new AlgorithmNodePacket(PacketType.AlgorithmNode),
                                  new BacktestingResultHandler(),
                                  null,
                                  null);
            var eventTime = DateTime.UtcNow;

            var count = 0;

            for (var i = 0; i < 100; i++)
            {
                var id = i;
                realTimeHandler.Add(new ScheduledEvent($"{id}", eventTime,
                                                       (s, time) =>
                {
                    Assert.AreEqual(id, count);
                    Assert.AreEqual(s, $"{id}");
                    count++;
                }));
            }

            realTimeHandler.SetTime(DateTime.UtcNow);
            realTimeHandler.Exit();
            Assert.AreEqual(100, count);
        }
Exemple #2
0
        public void DuplicateScheduledEventsAreBothFired()
        {
            var algorithm = new QCAlgorithm();

            var handler          = new BacktestingRealTimeHandler();
            var timeLimitManager = new AlgorithmTimeLimitManager(TokenBucket.Null, TimeSpan.MaxValue);

            handler.Setup(algorithm, new AlgorithmNodePacket(PacketType.BacktestNode), null, null, timeLimitManager);

            algorithm.Schedule.SetEventSchedule(handler);

            var time = new DateTime(2018, 1, 1);

            algorithm.SetDateTime(time);

            var count1 = 0;
            var count2 = 0;

            algorithm.Schedule.On(algorithm.Schedule.DateRules.EveryDay(), algorithm.Schedule.TimeRules.Every(TimeSpan.FromHours(1)), () => { count1++; });
            algorithm.Schedule.On(algorithm.Schedule.DateRules.EveryDay(), algorithm.Schedule.TimeRules.Every(TimeSpan.FromHours(1)), () => { count2++; });

            const int timeSteps = 12;

            for (var i = 0; i < timeSteps; i++)
            {
                handler.SetTime(time);
                time = time.AddHours(1);
            }

            Assert.AreEqual(timeSteps, count1);
            Assert.AreEqual(timeSteps, count2);
        }
Exemple #3
0
        public void SortsEventsAfterSetup()
        {
            var realTimeHandler = new BacktestingRealTimeHandler();
            var algo            = new TestAlgorithm();

            algo.SubscriptionManager.SetDataManager(new DataManagerStub(algo));
            algo.AddEquity("SPY");
            var startDate = new DateTime(2019, 1, 1);

            algo.SetStartDate(startDate);
            algo.SetDateTime(startDate);
            algo.SetEndDate(2020, 1, 1);

            var firstCalled  = false;
            var secondCalled = false;
            var events       = new List <ScheduledEvent>
            {
                new ScheduledEvent("1", new List <DateTime> {
                    startDate.AddMinutes(-10), startDate.AddMinutes(5)
                },
                                   (s, time) => { firstCalled = true; }),
                new ScheduledEvent("2", new List <DateTime> {
                    startDate.AddMinutes(1)
                },
                                   (s, time) => { secondCalled = true; }),
                new ScheduledEvent("3", new List <DateTime> {
                    startDate.AddMinutes(10)
                }, (s, time) => { })
            };

            foreach (var scheduledEvent in events)
            {
                realTimeHandler.Add(scheduledEvent);
            }

            realTimeHandler.Setup(algo,
                                  new AlgorithmNodePacket(PacketType.AlgorithmNode),
                                  new BacktestingResultHandler(),
                                  null,
                                  null);

            realTimeHandler.SetTime(startDate.AddMinutes(1));
            realTimeHandler.Exit();

            Assert.IsTrue(secondCalled);
            // 'first' should of been called and should be moved behind 'second' after setup
            Assert.IsFalse(firstCalled);
        }
        public void SingleScheduledEventFires_SetTime()
        {
            var realTimeHandler = new BacktestingRealTimeHandler();
            var algo            = new TestAlgorithm();

            algo.SubscriptionManager.SetDataManager(new DataManagerStub(algo));
            algo.AddEquity("SPY");
            algo.SetStartDate(2019, 1, 1);
            algo.SetDateTime(new DateTime(2019, 1, 1));
            algo.SetEndDate(2020, 1, 1);
            realTimeHandler.Setup(algo,
                                  new AlgorithmNodePacket(PacketType.AlgorithmNode),
                                  new BacktestingResultHandler(),
                                  null,
                                  null);

            realTimeHandler.SetTime(DateTime.UtcNow);
            Assert.IsTrue(algo.OnEndOfDayFired);
        }
        public void TriggersScheduledEventsSameTimeInOrder()
        {
            var realTimeHandler = new BacktestingRealTimeHandler();
            var eventTime       = DateTime.UtcNow;

            var count = 0;

            for (var i = 0; i < 100; i++)
            {
                var id = i;
                realTimeHandler.Add(new ScheduledEvent($"{id}", eventTime,
                                                       (s, time) =>
                {
                    Assert.AreEqual(id, count);
                    count++;
                }));
            }

            realTimeHandler.SetTime(DateTime.UtcNow);
            Assert.AreEqual(100, count);
        }
Exemple #6
0
        public void SetTime(bool oneStep)
        {
            var realTimeHandler = new BacktestingRealTimeHandler();

            realTimeHandler.Setup(new AlgorithmStub(new NullDataFeed()),
                                  new AlgorithmNodePacket(PacketType.AlgorithmNode),
                                  new BacktestingResultHandler(),
                                  null,
                                  null);
            var date = new DateTime(2020, 1, 1);

            var count   = 0;
            var asserts = 0;

            realTimeHandler.Add(new ScheduledEvent("1",
                                                   new List <DateTime> {
                date, date.AddMinutes(10)
            },
                                                   (s, time) =>
            {
                count++;
                if (count == 1)
                {
                    asserts++;
                    Assert.AreEqual(date, time);
                }
                else if (oneStep ? count == 2 : count == 4)
                {
                    asserts++;
                    Assert.AreEqual(date.AddMinutes(10), time);
                }
            }));

            realTimeHandler.Add(new ScheduledEvent("2",
                                                   new List <DateTime> {
                date.AddMinutes(1), date.AddMinutes(2)
            },
                                                   (s, time) =>
            {
                count++;
                if (oneStep ? count == 3 : count == 2)
                {
                    asserts++;
                    Assert.AreEqual(date.AddMinutes(1), time);
                }
                else if (oneStep ? count == 4 : count == 3)
                {
                    asserts++;
                    Assert.AreEqual(date.AddMinutes(2), time);
                }
            }));

            if (oneStep)
            {
                realTimeHandler.SetTime(date.AddDays(1));
            }
            else
            {
                realTimeHandler.SetTime(date);
                realTimeHandler.SetTime(date.AddMinutes(1));
                realTimeHandler.SetTime(date.AddMinutes(2));
                realTimeHandler.SetTime(date.AddMinutes(10));
            }
            realTimeHandler.Exit();
            Assert.AreEqual(4, count);
            Assert.AreEqual(4, asserts);
        }