public void AggregatesPeriodInCountModeWithDailyData()
        {
            TradeBar consolidated = null;
            var period = TimeSpan.FromDays(1);
            var consolidator = new TradeBarConsolidator(2);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

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

            consolidator.Update(new TradeBar { Time = reference.AddDays(1), Period = period });
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(TimeSpan.FromDays(2), consolidated.Period);
            consolidated = null;

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

            consolidator.Update(new TradeBar { Time = reference.AddDays(3), Period = period });
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(TimeSpan.FromDays(2), consolidated.Period);
        }
        public void TwoCountFiresEveryOther()
        {
            // defining a TradeBarConsolidator with a two max count should cause it to fire every other TradeBar

            TradeBar consolidated = null;
            var consolidator = new TradeBarConsolidator(2);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            consolidator.Update(new TradeBar());
            Assert.IsNull(consolidated);

            consolidator.Update(new TradeBar());
            Assert.IsNotNull(consolidated);

            consolidated = null;

            consolidator.Update(new TradeBar());
            Assert.IsNull(consolidated);

            consolidator.Update(new TradeBar());
            Assert.IsNotNull(consolidated);
        }
Exemple #3
0
        public void AggregatesPeriodInCountModeWithDailyData()
        {
            TradeBar consolidated = null;
            var      period       = TimeSpan.FromDays(1);
            var      consolidator = new TradeBarConsolidator(2);

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

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

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

            consolidator.Update(new TradeBar {
                Time = reference.AddDays(1), Period = period
            });
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(TimeSpan.FromDays(2), consolidated.Period);
            consolidated = null;

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

            consolidator.Update(new TradeBar {
                Time = reference.AddDays(3), Period = period
            });
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(TimeSpan.FromDays(2), consolidated.Period);
        }
        public void OneCountAlwaysFires()
        {
            // defining a TradeBarConsolidator with a one max count should cause it to always fire identity

            TradeBar consolidated = null;
            var consolidator = new TradeBarConsolidator(1);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            consolidator.Update(new TradeBar());
            Assert.IsNotNull(consolidated);
        }
        public void OneCountAlwaysFires()
        {
            // defining a TradeBarConsolidator with a one max count should cause it to always fire identity

            TradeBar consolidated = null;
            var      consolidator = new TradeBarConsolidator(1);

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

            consolidator.Update(new TradeBar());
            Assert.IsNotNull(consolidated);
        }
Exemple #6
0
        private static void PushBarsThrough(int barCount, TimeSpan period, TradeBarConsolidator consolidator, ref DateTime time)
        {
            TradeBar bar;

            for (int i = 0; i < barCount; i++)
            {
                bar = new TradeBar {
                    Time = time, Period = period
                };
                consolidator.Update(bar);

                // Advance time
                time += period;
            }
        }
Exemple #7
0
        public void ThrowsWhenPeriodIsSmallerThanDataPeriod()
        {
            TradeBar consolidated = null;

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

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

            Assert.Throws <ArgumentException>(() => consolidator.Update(new TradeBar {
                Time = reference, Period = Time.OneDay
            }));
        }
        public void AggregatesTradeBarToCalendarTradeBarProperly()
        {
            // Monday
            var reference = new DateTime(2019, 3, 18);
            var bars      = new List <TradeBar>
            {
                new TradeBar(reference.AddDays(1), Symbols.SPY, 9, 11, 8, 10, 100, Time.OneDay),
                new TradeBar(reference.AddDays(3), Symbols.SPY, 10, 12, 8, 11, 100, Time.OneDay),
                new TradeBar(reference.AddDays(5), Symbols.SPY, 11, 13, 9, 10, 100, Time.OneDay),
                new TradeBar(reference.AddDays(7), Symbols.SPY, 11, 13, 9, 11, 100, Time.OneDay),
                new TradeBar(reference.AddDays(14), Symbols.SPY, 11, 13, 9, 11, 100, Time.OneDay)
            };

            var weeklyConsolidator = new TradeBarConsolidator(CalendarType.Weekly);

            weeklyConsolidator.DataConsolidated += (s, e) =>
            {
                AssertTradeBar(
                    bars.Take(3),
                    reference,
                    reference.AddDays(7),
                    Symbols.SPY,
                    e);
            };

            var monthlyConsolidator = new TradeBarConsolidator(CalendarType.Monthly);

            monthlyConsolidator.DataConsolidated += (s, e) =>
            {
                AssertTradeBar(
                    bars.Take(4),
                    new DateTime(reference.Year, reference.Month, 1),
                    new DateTime(reference.Year, reference.Month + 1, 1),
                    Symbols.SPY,
                    e);
            };

            foreach (var bar in bars.Take(4))
            {
                weeklyConsolidator.Update(bar);
            }

            foreach (var bar in bars)
            {
                monthlyConsolidator.Update(bar);
            }
        }
        public void ConsolidatedTimeIsFromBeginningOfBar()
        {
            // verifies that the consolidated bar uses the time from the beginning of the first bar
            // in the period that covers the current bar

            var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(1));

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

            var reference = new DateTime(2014, 12, 1, 10, 00, 0);

            //10:00 - new
            consolidator.Update(new TradeBar {Time = reference});
            Assert.IsNull(consolidated);

            //10:01 - aggregate/fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(1)});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);

            //10:02 - new/fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(2)});
            Assert.AreEqual(reference.AddMinutes(2), consolidated.Time);

            //10:03 - new/fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(3)});
            Assert.AreEqual(reference.AddMinutes(3), consolidated.Time);

            //10:05 - new/fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(5)});
            Assert.AreEqual(reference.AddMinutes(5), consolidated.Time);

            //10:08 - new/fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(8)});
            Assert.AreEqual(reference.AddMinutes(8), consolidated.Time);

            //10:08:01 - new
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(8).AddSeconds(1)});
            Assert.AreEqual(reference.AddMinutes(8), consolidated.Time);

            //10:08 - new/fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(9)});
            Assert.AreEqual(reference.AddMinutes(8).AddSeconds(1), consolidated.Time);
        }
Exemple #10
0
        public void ZeroSpanAlwaysThrows()
        {
            // defining a TradeBarConsolidator with a zero period should cause it to always throw an exception

            TradeBar consolidated = null;

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

            var reference = new DateTime(2014, 12, 01, 01, 01, 00);

            Assert.Throws <ArgumentException>(() => consolidator.Update(new TradeBar {
                Time = reference, Period = Time.OneDay
            }));
        }
        public void HandlesDataGapsInMixedMode()
        {
            // define a three minute consolidator on a one minute stream of data
            var consolidator = new TradeBarConsolidator(3, TimeSpan.FromMinutes(3));

            TradeBar consolidated = null;

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

            var reference = new DateTime(2014, 12, 1, 10, 00, 0);

            //10:00 - new
            consolidator.Update(new TradeBar {
                Time = reference
            });
            Assert.IsNull(consolidated);

            //10:01 - aggregate
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(1)
            });
            Assert.IsNull(consolidated);

            //10:02 - fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(2)
            });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);

            //10:03 - new
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(3)
            });
            Assert.AreEqual(reference, consolidated.Time);

            //10:06 - aggregate/fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(6)
            });
            Assert.AreEqual(reference.AddMinutes(3), consolidated.Time);

            //10:08 - new/fire -- will have timestamp from 10:08, instead of 10:06
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(8)
            });
            Assert.AreEqual(reference.AddMinutes(8), consolidated.Time);
        }
Exemple #12
0
        public void GentlyHandlesPeriodAndDataAreSameResolution()
        {
            TradeBar consolidated = null;

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

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

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

            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);
        }
Exemple #13
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);
        }
Exemple #14
0
        public void HandlesGappingAcrossDays()
        {
            var consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1));

            TradeBar consolidated = null;

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

            // from 1/1 9:30 to 1/2 12:00 by minute
            var start = new DateTime(2014, 01, 01, 09, 30, 00, 00);
            var end   = new DateTime(2014, 01, 02, 12, 00, 00, 00);

            foreach (var bar in StreamTradeBars(start, end, TimeSpan.FromMinutes(1)))
            {
                consolidator.Update(bar);
            }
        }
Exemple #15
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);
        }
Exemple #16
0
        public void ClosedLeftOpenRightInTimeSpanModeTest()
        {
            // define a three minute consolidator
            int timeSpanUnits = 3;

            using var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(timeSpanUnits));

            TradeBar consolidated = null;

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

            var refDateTime = new DateTime(2014, 12, 1, 10, 00, 0);

            // loop for 3 times the timeSpanUnits + 1, so it would consolidate the bars 3 times
            for (int i = 0; i < 3 * timeSpanUnits + 1; ++i)
            {
                consolidator.Update(new TradeBar {
                    Time = refDateTime
                });

                if (i < timeSpanUnits)  // before initial consolidation happens
                {
                    Assert.IsNull(consolidated);
                }
                else
                {
                    Assert.IsNotNull(consolidated);
                    if (i % timeSpanUnits == 0) // i = 3, 6, 9
                    {
                        Assert.AreEqual(refDateTime.AddMinutes(-timeSpanUnits), consolidated.Time);
                    }
                }

                refDateTime = refDateTime.AddMinutes(1);
            }
        }
Exemple #17
0
        public void HandlesGappingAcrossDays()
        {
            // this test requires inspection to verify we're getting clean bars on the correct times

            var consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1));

            TradeBar consolidated = null;

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

            // from 1/1 9:30 to 1/2 12:00 by minute
            var start = new DateTime(2014, 01, 01, 09, 30, 00, 00);
            var end   = new DateTime(2014, 01, 02, 12, 00, 00, 00);

            foreach (var bar in StreamTradeBars(start, end, TimeSpan.FromMinutes(1)))
            {
                consolidator.Update(bar);
            }
        }
Exemple #18
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);
        }
        public void ConsolidatedTimeIsFromBeginningOfBar()
        {
            // verifies that the consolidated bar uses the time from the beginning of the first bar
            // in the period that covers the current bar

            var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(1));

            TradeBar consolidated = null;

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

            var reference = new DateTime(2014, 12, 1, 10, 00, 0);

            //10:00 - new
            consolidator.Update(new TradeBar {
                Time = reference
            });
            Assert.IsNull(consolidated);

            //10:01 - aggregate/fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(1)
            });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);

            //10:02 - new/fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(2)
            });
            Assert.AreEqual(reference.AddMinutes(1), consolidated.Time);

            //10:03 - new/fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(3)
            });
            Assert.AreEqual(reference.AddMinutes(2), consolidated.Time);

            //10:05 - new/fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(5)
            });
            Assert.AreEqual(reference.AddMinutes(3), consolidated.Time);

            //10:08 - new/fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(8)
            });
            Assert.AreEqual(reference.AddMinutes(5), consolidated.Time);

            //10:08:01 - new
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(8).AddSeconds(1)
            });
            Assert.AreEqual(reference.AddMinutes(5), consolidated.Time);

            //10:09 - new/fire
            consolidator.Update(new TradeBar {
                Time = reference.AddMinutes(9)
            });
            Assert.AreEqual(reference.AddMinutes(8), consolidated.Time);
        }
Exemple #20
0
        public void AggregatesTradeBarsProperly()
        {
            TradeBar newTradeBar = null;
            var      creator     = new TradeBarConsolidator(4);

            creator.DataConsolidated += (sender, args) =>
            {
                newTradeBar = args;
            };

            var time   = DateTime.Today;
            var period = TimeSpan.FromMinutes(1);
            var bar1   = new TradeBar
            {
                Time   = time,
                Symbol = Symbols.SPY,
                Open   = 1,
                High   = 2,
                Low    = 0.75m,
                Close  = 1.25m,
                Period = period
            };

            creator.Update(bar1);
            Assert.IsNull(newTradeBar);

            var bar2 = new TradeBar
            {
                Time   = time + TimeSpan.FromMinutes(1),
                Symbol = Symbols.SPY,
                Open   = 1.1m,
                High   = 2.2m,
                Low    = 0.9m,
                Close  = 2.1m,
                Period = period
            };

            creator.Update(bar2);
            Assert.IsNull(newTradeBar);

            var bar3 = new TradeBar
            {
                Time   = time + TimeSpan.FromMinutes(2),
                Symbol = Symbols.SPY,
                Open   = 1,
                High   = 2,
                Low    = 0.1m,
                Close  = 1.75m,
                Period = period
            };

            creator.Update(bar3);
            Assert.IsNull(newTradeBar);

            var bar4 = new TradeBar
            {
                Time   = time + TimeSpan.FromMinutes(3),
                Symbol = Symbols.SPY,
                Open   = 1,
                High   = 7,
                Low    = 0.5m,
                Close  = 4.4m,
                Period = period
            };

            creator.Update(bar4);
            Assert.IsNotNull(newTradeBar);
            Assert.AreEqual(bar1.Symbol, newTradeBar.Symbol);
            Assert.AreEqual(1, newTradeBar.Open);
            Assert.AreEqual(7, newTradeBar.High);
            Assert.AreEqual(0.1m, newTradeBar.Low);
            Assert.AreEqual(4.4m, newTradeBar.Close);
            Assert.AreEqual(newTradeBar.Close, newTradeBar.Value);
            Assert.AreEqual(bar4.EndTime, newTradeBar.EndTime);
            Assert.AreEqual(TimeSpan.FromMinutes(4), newTradeBar.Period);

            Assert.AreEqual(bar1.Volume + bar2.Volume + bar3.Volume + bar4.Volume, newTradeBar.Volume);
        }
        public void ConsolidatesOHLCV()
        {
            // verifies that the TradeBarConsolidator correctly consolidates OHLCV data into a new TradeBar instance

            TradeBar consolidated = null;
            var consolidator = new TradeBarConsolidator(3);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var tb1 = new TradeBar
            {
                Symbol = "SPY",
                Open = 10,
                High = 100,
                Low = 1,
                Close = 50,
                Volume = 75,
                DataType = MarketDataType.TradeBar
            };

            var tb2 = new TradeBar
            {
                Symbol = "SPY",
                Open = 50,
                High = 123,
                Low = 35,
                Close = 75,
                Volume = 100,
                DataType = MarketDataType.TradeBar
            };

            var tb3 = new TradeBar
            {
                Symbol = "SPY",
                Open = 75,
                High = 100,
                Low = 50,
                Close = 83,
                Volume = 125,
                DataType = MarketDataType.TradeBar
            };

            consolidator.Update(tb1);
            consolidator.Update(tb2);
            consolidator.Update(tb3);

            Assert.IsNotNull(consolidated);
            Assert.AreEqual("SPY", consolidated.Symbol);
            Assert.AreEqual(10m, consolidated.Open);
            Assert.AreEqual(123m, consolidated.High);
            Assert.AreEqual(1m, consolidated.Low);
            Assert.AreEqual(83m, consolidated.Close);
            Assert.AreEqual(300L, consolidated.Volume);
        }
        public void HandlesGappingAcrossDays()
        {
            // this test requires inspection to verify we're getting clean bars on the correct times

            var consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1));

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

            // from 1/1 9:30 to 1/2 12:00 by minute
            var start = new DateTime(2014, 01, 01, 09, 30, 00, 00);
            var end =   new DateTime(2014, 01, 02, 12, 00, 00, 00);
            foreach (var bar in StreamTradeBars(start, end, TimeSpan.FromMinutes(1)))
            {
                consolidator.Update(bar);
            }
        }
        public void ZeroSpanAlwaysFires()
        {
            // defining a TradeBarConsolidator with a zero period should cause it to always fire identity

            TradeBar consolidated = null;
            var consolidator = new TradeBarConsolidator(TimeSpan.Zero);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2014, 12, 01, 01, 01, 00);
            consolidator.Update(new TradeBar {Close = 1m, Time = reference});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(1, consolidated.Close);

            consolidator.Update(new TradeBar {Close = 2m, Time = reference});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(2, consolidated.Close);

            consolidator.Update(new TradeBar {Close = 3m, Time = reference});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(3, consolidated.Close);
        }
        public void OneMinuteAlwaysFiresEveryTimeOnMinueDataExceptFirstPoint()
        {
            // defining a TradeBarConsolidator with the same period as the resolution of input data will cause
            // it to not fire on the first piece of data as it is initializing, but will thenfire for each
            // consecutive data point

            TradeBar consolidated = null;
            var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(1));
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2014, 12, 01, 01, 01, 00);
            consolidator.Update(new TradeBar {Close = 1m, Time = reference});
            Assert.IsNull(consolidated);

            consolidator.Update(new TradeBar {Close = 2m, Time = reference.AddMinutes(1)});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(2, consolidated.Close);

            consolidator.Update(new TradeBar {Close = 3m, Time = reference.AddMinutes(2)});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(3, consolidated.Close);
        }
        public void HandlesGappingAcrossDays()
        {
            var consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1));

            TradeBar consolidated = null;
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
                Console.WriteLine(bar.Time);
            };

            // from 1/1 9:30 to 1/2 12:00 by minute
            var start = new DateTime(2014, 01, 01, 09, 30, 00, 00);
            var end =   new DateTime(2014, 01, 02, 12, 00, 00, 00);
            foreach (var bar in StreamTradeBars(start, end, TimeSpan.FromMinutes(1)))
            {
                consolidator.Update(bar);
            }
        }
        public void HandlesDataGapsInMixedMode()
        {
            // define a three minute consolidator on a one minute stream of data
            var consolidator = new TradeBarConsolidator(3, TimeSpan.FromMinutes(3));

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

            var reference = new DateTime(2014, 12, 1, 10, 00, 0);

            //10:00 - new
            consolidator.Update(new TradeBar {Time = reference});
            Assert.IsNull(consolidated);

            //10:01 - aggregate
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(1)});
            Assert.IsNull(consolidated);

            //10:02 - fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(2)});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(reference, consolidated.Time);

            //10:03 - new
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(3)});
            Assert.AreEqual(reference, consolidated.Time);

            //10:06 - aggregate/fire
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(6)});
            Assert.AreEqual(reference.AddMinutes(3), consolidated.Time);

            //10:08 - new/fire -- will have timestamp from 10:08, instead of 10:06
            consolidator.Update(new TradeBar {Time = reference.AddMinutes(8)});
            Assert.AreEqual(reference.AddMinutes(8), consolidated.Time);
        }
        public void ClosedLeftOpenRightInTimeSpanModeTest()
        {
            // define a three minute consolidator 
            int timeSpanUnits = 3;
            var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(timeSpanUnits));

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

            var refDateTime = new DateTime(2014, 12, 1, 10, 00, 0);

            // loop for 3 times the timeSpanUnits + 1, so it would consolidate the bars 3 times
            for (int i=0; i < 3*timeSpanUnits + 1 ; ++i) 
            {
                consolidator.Update(new TradeBar { Time = refDateTime });

                if (i < timeSpanUnits)  // before initial consolidation happens
                {
                    Assert.IsNull(consolidated);
                }
                else 
                {
                    Assert.IsNotNull(consolidated);
                    if (i % timeSpanUnits == 0) // i = 3, 6, 9
                    {
                        Assert.AreEqual(refDateTime.AddMinutes(-timeSpanUnits), consolidated.Time);
                    }
                }

                refDateTime = refDateTime.AddMinutes(1);
            }
        }
        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);
        }
        public void AggregatesPeriodInPeriodModeWithDailyDataAndRoundedTime()
        {
            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.Update(new TradeBar { Time = reference.AddDays(1).AddMinutes(1), Period = period });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(period, consolidated.Period);
            Assert.AreEqual(reference, consolidated.Time);
            consolidated = null;

            consolidator.Update(new TradeBar { Time = reference.AddDays(2).AddHours(1), Period = period });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(period, consolidated.Period);
            Assert.AreEqual(reference.AddDays(1), consolidated.Time);
            consolidated = null;

            consolidator.Update(new TradeBar { Time = reference.AddDays(3).AddMinutes(1).AddSeconds(1), Period = period });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(period, consolidated.Period);
            Assert.AreEqual(reference.AddDays(2), consolidated.Time);
        }
        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);
        }
        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            // initialize algorithm level parameters
            SetStartDate(2015, 01, 01);
            SetEndDate(2015, 06, 01);
            SetCash(100000);

            // leverage tradier $1 traders
            SetBrokerageModel(BrokerageName.TradierBrokerage);

            // request high resolution equity data
            AddSecurity(SecurityType.Equity, Symbol, Resolution.Second);

            // save off our security so we can reference it quickly later
            Security = Securities[Symbol];

            // Set our max leverage
            Security.SetLeverage(MaximumLeverage);

            // define a hull for trend detection
            HMA = new HullMovingAverage(Symbol + "_HMA14", 4);
            var hmaDaily = new TradeBarConsolidator(TimeSpan.FromMinutes(30));

            RegisterIndicator(Symbol, HMA, hmaDaily, Field.Close);

            // define our longer term indicators
            STD14   = STD(Symbol, 14, Resolution.Daily);
            ATR14   = ATR(Symbol, 14, resolution: Resolution.Daily);
            PSARMin = new ParabolicStopAndReverse(Symbol, afStart: 0, afIncrement: 0.000025m);

            // smooth our ATR over a week, we'll use this to determine if recent volatilty warrants entrance
            var oneWeekInMarketHours = (int)(5 * 6.5);

            SmoothedATR14 = new ExponentialMovingAverage("Smoothed_" + ATR14.Name, oneWeekInMarketHours).Of(ATR14);
            // smooth our STD over a week as well
            SmoothedSTD14 = new ExponentialMovingAverage("Smoothed_" + STD14.Name, oneWeekInMarketHours).Of(STD14);

            // initialize our charts
            var chart = new Chart(Symbol);

            chart.AddSeries(new Series(HMA.Name));
            chart.AddSeries(new Series("Enter", SeriesType.Scatter));
            chart.AddSeries(new Series("Exit", SeriesType.Scatter));
            chart.AddSeries(new Series(PSARMin.Name, SeriesType.Scatter));
            AddChart(chart);

            var history = History(Symbol, 20, Resolution.Daily);

            foreach (var bar in history)
            {
                hmaDaily.Update(bar);
                ATR14.Update(bar);
                STD14.Update(bar.EndTime, bar.Close);
            }

            // schedule an event to run every day at five minutes after our Symbol's market open
            Schedule.Event("MarketOpenSpan")
            .EveryDay(Symbol)
            .AfterMarketOpen(Symbol, minutesAfterOpen: OpeningSpanInMinutes)
            .Run(MarketOpeningSpanHandler);

            Schedule.Event("MarketOpen")
            .EveryDay(Symbol)
            .AfterMarketOpen(Symbol, minutesAfterOpen: -1)
            .Run(() => PSARMin.Reset());
        }
        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);
        }