Beispiel #1
0
        public void BestProfitPeriod2Data()
        {
            PureData pureData = new PureData(new List <DataElement>
            {
                new DataElement(new DateTime(2018, 2, 19), new[] { 1.1D, 2.1D, 3.1D, 4.1D, 5.1D }),
                new DataElement(new DateTime(2018, 2, 20), new[] { 1.2D, 2.2D, 3.2D, 4.2D, 5.2D }),
                new DataElement(new DateTime(2018, 2, 21), new[] { 1.3D, 2.3D, 3.3D, 4.3D, 5.3D }),
                new DataElement(new DateTime(2018, 2, 21), new[] { 1.4D, 2.4D, 3.4D, 4.4D, 5.4D })
            });

            var sut = new BestProfit(2, pureData, StockIndex.High);

            DataElement[] inputData = sut.CreateData().ToArray();

            Assert.NotNull(inputData);
            Assert.AreEqual(2, inputData.Length);
            Assert.IsTrue(Math.Abs(0.083333333333D - inputData[0].Values[0]) < 0.0001, $"{inputData[0].Values[0]}");
            Assert.IsTrue(Math.Abs(0.076923D - inputData[1].Values[0]) < 0.001, $"{inputData[1].Values[0]}");
            Assert.AreEqual(pureData.GetData()[0].Key, inputData[0].Key);
            Assert.AreEqual(pureData.GetData()[1].Key, inputData[1].Key);
        }
Beispiel #2
0
        public void MovingAverageData2()
        {
            PureData pureData = new PureData(new List <DataElement>
            {
                new DataElement(new DateTime(2018, 2, 19), new[] { 1.1D, 2.1D, 3.1D, 4.1D, 5.1D }),
                new DataElement(new DateTime(2018, 2, 20), new[] { 1.2D, 2.2D, 3.2D, 4.2D, 5.2D }),
                new DataElement(new DateTime(2018, 2, 21), new[] { 1.3D, 2.3D, 3.3D, 4.3D, 5.3D }),
                new DataElement(new DateTime(2018, 2, 21), new[] { 1.4D, 2.4D, 3.4D, 4.4D, 5.4D })
            });

            var sut = new MovingAverage(2, pureData, StockIndex.Close);

            DataElement[] inputData = sut.CreateData().ToArray();

            Assert.NotNull(inputData);
            Assert.AreEqual(3, inputData.Length);
            Assert.IsTrue(Math.Abs(2.15D - inputData[0].Values[0]) < 0.00000001);
            Assert.IsTrue(Math.Abs(2.25D - inputData[1].Values[0]) < 0.00000001);
            Assert.AreEqual(pureData.GetData()[1].Key, inputData[0].Key);
            Assert.AreEqual(pureData.GetData()[2].Key, inputData[1].Key);
            Assert.AreEqual(pureData.GetData()[3].Key, inputData[2].Key);
        }
Beispiel #3
0
        private IEnumerable <DataSet> CollateData()
        {
            if (_inputs.Count == 0)
            {
                return(Enumerable.Empty <DataSet>());
            }
            if (_targets.Count == 0)
            {
                return(Enumerable.Empty <DataSet>());
            }

            IList <DataElement> source = _pureData.GetData();

            DateTime today = source.Last().Key.Date;
            DateTime first = _inputs.Select(i => i.Min(x => x.Key)).Max(y => y).Date;
            DateTime last  = _targets.Select(t => t.Max(x => x.Key)).Min(y => y).Date;

            var targeted = new List <DataSet>();

            while (first <= last)
            {
                var    sourceData = source.First(s => s.Key.Date == first);
                double close      = sourceData.Values[(int)StockIndex.Close];

                IList <double> inputs = new List <double>();
                foreach (var input in _inputs)
                {
                    foreach (var d in input.First(i => i.Key.Date == first).Values)
                    {
                        inputs.Add(d / close);
                    }
                }

                IList <double> targets = new List <double>();
                foreach (var target in _targets)
                {
                    foreach (var d in target.First(i => i.Key.Date == first).Values)
                    {
                        targets.Add(d);
                    }
                }

                targeted.Add(new DataSet(inputs.ToArray(), targets.ToArray()));

                first = first.AddDays(1).Date;
                while (source.FirstOrDefault(d => d.Key.Date == first) == null && first <= last)
                {
                    first = first.AddDays(1).Date;
                }
            }

            _unTargeted = new List <DataSet>();

            while (first <= today)
            {
                var    sourceData = source.First(s => s.Key.Date == first);
                double close      = sourceData.Values[(int)StockIndex.Close];

                IList <double> inputs = new List <double>();
                foreach (var input in _inputs)
                {
                    foreach (var d in input.First(i => i.Key.Date == first).Values)
                    {
                        inputs.Add(d / close);
                    }
                }

                UnTargeted.Add(new DataSet(inputs.ToArray(), Enumerable.Empty <double>().ToArray()));

                first = first.AddDays(1).Date;
                while (source.FirstOrDefault(d => d.Key.Date == first) == null && first <= today)
                {
                    first = first.AddDays(1).Date;
                }
            }

            return(FixTargets(targeted));
        }
Beispiel #4
0
 public BestProfit(int period, PureData pureData, StockIndex index)
 {
     _period   = period;
     _index    = index;
     _elements = pureData.GetData();
 }
 public MovingAverage(int periods, PureData pureData, StockIndex stockIndex)
 {
     _periods    = periods;
     _elements   = pureData.GetData();
     _stockIndex = stockIndex;
 }