Beispiel #1
0
        public void ItemsT_Enumerating_Returns_Correct_Values()
        {
            using (TeaFile <OHLCV> f = TeaFile <OHLCV> .Create(Guid.NewGuid() + "lab1.tea"))
            {
                var firstValue = new OHLCV {
                    Open = 111
                };
                var someValue = new OHLCV {
                    Open = 222
                };
                var lastValue = new OHLCV {
                    Open = 333
                };

                List <OHLCV> values = new List <OHLCV>();
                values.Add(firstValue);
                values.Add(someValue);
                values.Add(lastValue);

                int i = 0;
                foreach (OHLCV ohlcv in f.Items)
                {
                    Assert.AreEqual(values[i], ohlcv);
                }
            }
        }
Beispiel #2
0
        public void AddTransformedValue(OHLCV Value)
        {
            if (_currentTransformedData == null)
            {
                throw new ApplicationException("Must first start new transformation.");
            }

            AddTransformedValue(this.ConversionStrategy.Convert(Value));
        }
Beispiel #3
0
        public void Test_Bin_EmptyData()
        {
            IList <OHLCV> data     = new OHLCV[] { };
            Periods       period   = Periods.OneMinute;
            TimeZoneInfo  timeZone = TimeZoneInfo.Utc;

            var expected = new List <OHLCV>();

            var source = new Mock <ISourceSpecification>();

            source.Setup(x => x.DataStartTime).Returns(new TimeSpan(0, 0, 0));
            source.Setup(x => x.TimeZoneInfo).Returns(TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"));

            var target = new Binner(source.Object);

            var actual = target.Bin(data, period, timeZone);

            Assert.IsTrue(actual.SequenceEqual(expected));
        }
Beispiel #4
0
        public void CouldAggregateWithHoles()
        {
            var count  = 1000000;
            var start  = DateTime.UtcNow.Date;
            var source = new List <Tick>();

            source.Add(new Tick(start.AddMilliseconds(250), new Price(1.0), 1));
            source.Add(new Tick(start.AddMilliseconds(750), new Price(2.0), 2));
            source.Add(new Tick(start.AddMilliseconds(2500), new Price(3.0), 3));

            var seconds = source.Select(x => new KeyValuePair <DateTime, Tick>(x.DateTimeUtc, x)).TimeSlice((t) => new OHLCV(t.Price, t.Price, t.Price, t.Price, t.Volume),
                                                                                                            (st, t) =>
            {
                var res = new OHLCV(
                    st.Open,
                    t.Price > st.High ? t.Price : st.High,
                    t.Price < st.Low ? t.Price : st.Low,
                    t.Price,
                    st.Volume + t.Volume
                    );
                return(res);
            }, UnitPeriod.Second, 1, 0);     //.ToSortedMap();

            var sw = new Stopwatch();

            //Console.WriteLine("Spreads");
            sw.Restart();
            var total = 0L;

            foreach (var kvp in seconds)
            {
                total += kvp.Value.Volume;
                Console.WriteLine($"{kvp.Key} - {kvp.Value.Open} - {kvp.Value.High} - {kvp.Value.Low} - {kvp.Value.Close} - {kvp.Value.Volume}");
            }
            if (total == short.MaxValue)
            {
                Console.WriteLine("avoid optimizations");
            }
        }
Beispiel #5
0
        /// <inheritdoc/>
        public IList <OHLCV> Bin(IList <OHLCV> data, Periods period, TimeZoneInfo targetTimeZone, TimeSpan phase = default(TimeSpan))
        {
            if ((data == null) || (data.Count == 0) || (period == Periods.NotSet))
            {
                return(new List <OHLCV>());
            }

            TimeSpan interval = PeriodIntervals.Instance[period];

            IList <OHLCV> binnedData = new List <OHLCV>();

            DateTime       targetStartMidnight = (TimeZoneInfo.ConvertTime(data[0].BeginDateTimeOffset, targetTimeZone) + phase).Date;
            DateTimeOffset currentBinEnd       = new DateTimeOffset(
                targetStartMidnight,
                targetTimeZone.GetUtcOffset(targetStartMidnight));

            DateTimeOffset currentBinStart;
            OHLCV          ohlcv = default(OHLCV);

            var lastDate = TimeZoneInfo.ConvertTime(data.Last().EndDateTimeOffset, targetTimeZone) + phase;

            foreach (var d in data)
            {
                DateTimeOffset dataStartDateTime = TimeZoneInfo.ConvertTime(d.BeginDateTimeOffset, targetTimeZone) + phase;

                // if new bin add the current value, move bin to correct location and open a new one
                if (currentBinEnd <= dataStartDateTime)
                {
                    // add the current value
                    if (!(ohlcv.Equals(default(OHLCV))))
                    {
                        binnedData.Add(ohlcv);
                    }

                    // move bin to correct location
                    while (currentBinEnd <= dataStartDateTime)
                    {
                        currentBinEnd += interval;
                    }

                    // do not bin beyond the source data (i.e. don't create partial bins
                    if (currentBinEnd > lastDate)
                    {
                        break;
                    }

                    currentBinStart = currentBinEnd - interval;

                    // open a new one
                    ohlcv = new OHLCV(
                        currentBinStart - phase,
                        d.Open,
                        0,
                        currentBinEnd - phase);

                    ohlcv.Open = d.Open;
                }

                // update bin values
                ohlcv.AddData(d);

                // if this is the last data then add the current bin
                if (d.Equals(data[data.Count - 1]))
                {
                    binnedData.Add(ohlcv);
                }
            }

            return(binnedData);
        }
Beispiel #6
0
        public IList <OHLCV> Bin(IList <OHLCV> data, Periods period, TimeZoneInfo targetTimeZone)
        {
            if ((data == null) || (data.Count == 0) || (period == Periods.NotSet))
            {
                return(new List <OHLCV>());
            }

            TimeSpan interval = PeriodIntervals[period];

            IList <OHLCV> binnedData = new List <OHLCV>();

            var sourceStartDate = new DateTimeOffset(
                TimeZoneInfo.ConvertTime(data[0].BeginDateTimeOffset, Source.TimeZoneInfo).Date + Source.DataStartTime,
                Source.TimeZoneInfo.BaseUtcOffset);

            // go back an entire day to ensure that the time zone differences between source and target are defo taken into account
            var currentBinEnd = TimeZoneInfo.ConvertTime(sourceStartDate, targetTimeZone).AddDays(-1);

            DateTimeOffset currentBinStart;
            OHLCV          ohlcv = default(OHLCV);

            foreach (var d in data)
            {
                DateTimeOffset dataStartDateTime = TimeZoneInfo.ConvertTime(d.BeginDateTimeOffset, targetTimeZone);

                // if new bin add the current value, move bin to correct location and open a new one
                if (currentBinEnd <= dataStartDateTime)
                {
                    // add the current value
                    if (!(ohlcv.Equals(default(OHLCV))))
                    {
                        binnedData.Add(ohlcv);
                    }

                    // move bin to correct location
                    while (currentBinEnd <= dataStartDateTime)
                    {
                        currentBinEnd += interval;
                    }

                    currentBinStart = currentBinEnd - interval;

                    // open a new one
                    ohlcv = new OHLCV(
                        currentBinStart,
                        0.0,
                        double.MinValue,
                        double.MaxValue,
                        0.0,
                        0,
                        currentBinEnd);

                    ohlcv.Open = d.Open;
                }

                // update bin values
                ohlcv.High    = (d.High > ohlcv.High) ? d.High : ohlcv.High;
                ohlcv.Low     = (d.Low < ohlcv.Low) ? d.Low : ohlcv.Low;
                ohlcv.Close   = d.Close;
                ohlcv.Volume += d.Volume;

                // if this is the last data then add the current bin
                if (d.Equals(data[data.Count - 1]))
                {
                    binnedData.Add(ohlcv);
                }
            }

            return(binnedData);
        }
Beispiel #7
0
 public override double Convert(OHLCV Value)
 {
     return((double)Value.High);
 }
Beispiel #8
0
 public override double Convert(OHLCV Value)
 {
     return((double)Value.Close);
 }
 public abstract double Convert(OHLCV Value);
Beispiel #10
0
        public void CouldAggregate()
        {
            var count  = 10000;
            var start  = DateTime.UtcNow.Date;
            var source = new List <Tick>();

            for (int i = 0; i < count; i++)
            {
                source.Add(new Tick(start.AddMilliseconds((i / 5) * 5), new Price((double)i), i));
            }

            var seconds = source.Select(x => new KeyValuePair <DateTime, Tick>(x.DateTimeUtc, x)).TimeSlice((t) => new OHLCV(t.Price, t.Price, t.Price, t.Price, t.Volume),
                                                                                                            (st, t) =>
            {
                var res = new OHLCV(
                    st.Open,
                    t.Price > st.High ? t.Price : st.High,
                    t.Price < st.Low ? t.Price : st.Low,
                    t.Price,
                    st.Volume + t.Volume
                    );
                return(res);
            }, UnitPeriod.Second, 1, 0);     //.ToSortedMap();

            var grouped =
                source.GroupBy(t => new DateTime((t.DateTimeUtc.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond, t.DateTimeUtc.Kind))
                .Select(gr => gr.Aggregate(new KeyValuePair <DateTime, OHLCV>(default(DateTime), new OHLCV((decimal)(-1), -1, -1, -1, 0, 5)), (st, t) =>
            {
                if (st.Value.Open == -1)
                {
                    var res = new OHLCV(
                        t.Price,
                        t.Price,
                        t.Price,
                        t.Price,
                        t.Volume
                        );
                    return(new KeyValuePair <DateTime, OHLCV>(gr.Key, res));
                }
                else
                {
                    var res = new OHLCV(
                        st.Value.Open,
                        t.Price > st.Value.High ? t.Price : st.Value.High,
                        t.Price < st.Value.Low ? t.Price : st.Value.Low,
                        t.Price,
                        st.Value.Volume + t.Volume
                        );
                    return(new KeyValuePair <DateTime, OHLCV>(gr.Key, res));
                }
            }));

            var sw = new Stopwatch();

            Console.WriteLine($"Memory: {GC.GetTotalMemory(false)}");
            for (int r = 0; r < 1; r++)
            {
                //Console.WriteLine("Spreads");
                sw.Restart();
                var total = 0L;
                foreach (var kvp in seconds)
                {
                    total += kvp.Value.Volume;
                    Console.WriteLine($"{kvp.Key} - {kvp.Value.Open} - {kvp.Value.High} - {kvp.Value.Low} - {kvp.Value.Close} - {kvp.Value.Volume}");
                }
                if (total == short.MaxValue)
                {
                    Console.WriteLine("avoid optimizations");
                }
                sw.Stop();
                Console.WriteLine($"Spreads: {sw.ElapsedMilliseconds}");

                Console.WriteLine("LINQ");
                sw.Restart();
                var total2 = 0L;
                foreach (var kvp in grouped)
                {
                    total2 += kvp.Value.Volume;
                    Console.WriteLine($"{kvp.Key} - {kvp.Value.Open} - {kvp.Value.High} - {kvp.Value.Low} - {kvp.Value.Close} - {kvp.Value.Volume}");
                }
                if (total2 == short.MaxValue)
                {
                    Console.WriteLine("avoid optimizations");
                }
                sw.Stop();
                Console.WriteLine($"LINQ: {sw.ElapsedMilliseconds}");
                Assert.AreEqual(total, total2);
                Console.WriteLine($"GC: {GC.CollectionCount(0)} - {GC.CollectionCount(1)} - {GC.CollectionCount(2)}");
            }
            Console.WriteLine($"Memory: {GC.GetTotalMemory(false)}");
            Console.WriteLine($"GC: {GC.CollectionCount(0)} - {GC.CollectionCount(1)} - {GC.CollectionCount(2)}");
        }
 public override double Convert(OHLCV Value)
 {
     return(((double)Value.High + (double)Value.Low) / 2.0);
 }