Пример #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);
        }
Пример #2
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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        public void ScanPastEvents(bool oneStep)
        {
            var realTimeHandler = new BacktestingRealTimeHandler();

            realTimeHandler.Setup(new AlgorithmStub(),
                                  new AlgorithmNodePacket(PacketType.AlgorithmNode)
            {
                Language = Language.CSharp
            },
                                  _resultHandler,
                                  null,
                                  new TestTimeLimitManager());

            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 (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 (count == 2)
                {
                    asserts++;
                    Assert.AreEqual(date.AddMinutes(1), time);
                }
                else if (count == 3)
                {
                    asserts++;
                    Assert.AreEqual(date.AddMinutes(2), time);
                }
            }));

            if (oneStep)
            {
                realTimeHandler.ScanPastEvents(date.AddDays(1));
            }
            else
            {
                realTimeHandler.ScanPastEvents(date.AddMilliseconds(1));
                realTimeHandler.ScanPastEvents(date.AddMinutes(1).AddMilliseconds(1));
                realTimeHandler.ScanPastEvents(date.AddMinutes(2).AddMilliseconds(1));
                realTimeHandler.ScanPastEvents(date.AddMinutes(10).AddMilliseconds(1));
            }
            realTimeHandler.Exit();
            Assert.AreEqual(4, count);
            Assert.AreEqual(4, asserts);
        }
Пример #5
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);
        }