protected override decimal ComputeNextValue(IndicatorDataPoint input)
 {
     _fast.Update(input);
     _slow.Update(input);
     _smooth.Update(input.Time, 2 * _fast - _slow);
     return(_smooth);
 }
예제 #2
0
 /// <summary>
 /// Computes the next value of this indicator from the given state
 /// </summary>
 /// <param name="input">The input given to the indicator</param>
 /// <returns>A new value for this indicator</returns>
 protected override decimal ComputeNextValue(IndicatorDataPoint input)
 {
     _shortRoc.Update(input);
     _longRoc.Update(input);
     if (_longRoc.IsReady && _shortRoc.IsReady)
     {
         var rocSum = _shortRoc + _longRoc;
         _lwma.Update(input.Time, rocSum);
         return(_lwma);
     }
     return(decimal.Zero);
 }
예제 #3
0
        // See http://en.wikipedia.org/wiki/Moving_average
        // for the formula and the numbers in this test.
        public void ComputesCorrectly(int period)
        {
            var values  = new[] { 77m, 79m, 79m, 81m, 83m };
            var weights = Enumerable.Range(1, period).ToArray();
            var current = weights.Sum(i => i * values[i - 1]) / weights.Sum();

            var lwma = new LinearWeightedMovingAverage(period);
            var time = DateTime.UtcNow;

            for (var i = 0; i < period; i++)
            {
                lwma.Update(time.AddSeconds(i), values[i]);
            }
            Assert.AreEqual(current, lwma.Current.Value);
        }
        public void Lwma2ComputesCorrectly()
        {
            const int period = 2;
            decimal[] values = { 1m, 2m };

            var lwma = new LinearWeightedMovingAverage(period);

            decimal current = 0m;
            for (int i = 0; i < values.Length; i++)
            {
                lwma.Update(new IndicatorDataPoint(DateTime.UtcNow.AddSeconds(i), values[i]));
            }
            current = ((2 * 2m) + (1 * 1m)) / 3;
            Assert.AreEqual(current, lwma.Current.Value);
        }
        // See http://en.wikipedia.org/wiki/Moving_average
        // for the formula and the numbers in this test.
        public void Lwma5ComputesCorrectly()
        {
            const int period = 5;
            decimal[] values = { 77m, 79m, 79m, 81m, 83m };

            var lwma = new LinearWeightedMovingAverage(period);

            decimal current = 0m;
            for (int i = 0; i < values.Length; i++)
            {
                lwma.Update(new IndicatorDataPoint(DateTime.UtcNow.AddSeconds(i), values[i]));

            }
            current = 83 * (5m / 15) + 81 * (4m / 15) + 79 * (3m / 15) + 79 * (2m / 15) + 77 * (1m / 15);
            Assert.AreEqual(current, lwma.Current.Value);
        }
        // See http://en.wikipedia.org/wiki/Moving_average
        // for the formula and the numbers in this test.
        public void Lwma5ComputesCorrectly()
        {
            const int period = 5;

            decimal[] values = { 77m, 79m, 79m, 81m, 83m };

            var lwma = new LinearWeightedMovingAverage(period);

            decimal current = 0m;

            for (int i = 0; i < values.Length; i++)
            {
                lwma.Update(new IndicatorDataPoint(DateTime.UtcNow.AddSeconds(i), values[i]));
            }
            current = 83 * (5m / 15) + 81 * (4m / 15) + 79 * (3m / 15) + 79 * (2m / 15) + 77 * (1m / 15);
            Assert.AreEqual(current, lwma.Current.Value);
        }
        public void Lwma2ComputesCorrectly()
        {
            const int period = 2;

            decimal[] values = { 1m, 2m };

            var lwma = new LinearWeightedMovingAverage(period);

            decimal current = 0m;

            for (int i = 0; i < values.Length; i++)
            {
                lwma.Update(new IndicatorDataPoint(DateTime.UtcNow.AddSeconds(i), values[i]));
            }
            current = ((2 * 2m) + (1 * 1m)) / 3;
            Assert.AreEqual(current, lwma.Current.Value);
        }
        public void Lwma4ComputesCorrectly()
        {
            const int period = 4;

            decimal[] values = { 1m, 2m, 3m, 4m };

            var lwma = new LinearWeightedMovingAverage(period);

            decimal current = 0m;

            for (int i = 0; i < values.Length; i++)
            {
                lwma.Update(new IndicatorDataPoint(DateTime.UtcNow.AddSeconds(i), TimeZone.Utc, values[i]));
            }
            current = ((4 * .4m) + (3 * .3m) + (2 * .2m) + (1 * .1m));
            Assert.Equal(current, lwma.Current.Price);
        }
예제 #9
0
        // See http://en.wikipedia.org/wiki/Moving_average
        // for the formula and the numbers in this test.
        public void WarmsUpProperly(int period)
        {
            var values  = new[] { 77m, 79m, 79m, 81m, 83m };
            var weights = Enumerable.Range(1, period).ToArray();
            var current = weights.Sum(i => i * values[i - 1]) / weights.Sum();

            var lwma = new LinearWeightedMovingAverage(period);
            var time = DateTime.UtcNow;

            var warmUpPeriod = (lwma as IIndicatorWarmUpPeriodProvider)?.WarmUpPeriod;

            for (var i = 0; i < warmUpPeriod; i++)
            {
                lwma.Update(time.AddSeconds(i), values[i]);
                Assert.AreEqual(i == warmUpPeriod - 1, lwma.IsReady);
            }
            Assert.AreEqual(current, lwma.Current.Value);
        }
        public void ResetsProperly()
        {
            const int period = 4;

            decimal[] values = { 1m, 2m, 3m, 4m, 5m };

            var lwma = new LinearWeightedMovingAverage(period);

            for (int i = 0; i < values.Length; i++)
            {
                lwma.Update(new IndicatorDataPoint(DateTime.UtcNow.AddSeconds(i), TimeZone.Utc, values[i]));
            }
            Assert.True(lwma.IsReady);
            Assert.NotEqual(0m, lwma.Current.Price);
            Assert.NotEqual(0, lwma.Samples);

            lwma.Reset();

            TestHelper.AssertIndicatorIsInDefaultState(lwma);
        }
예제 #11
0
        // See http://en.wikipedia.org/wiki/Moving_average
        // for the formula and the numbers in this test.
        public void ResetsProperly(int period)
        {
            var values  = new[] { 77m, 79m, 79m, 81m, 83m };
            var weights = Enumerable.Range(1, period).ToArray();
            var current = weights.Sum(i => i * values[i - 1]) / weights.Sum();

            var lwma = new LinearWeightedMovingAverage(period);
            var time = DateTime.UtcNow;

            for (var i = 0; i < period; i++)
            {
                lwma.Update(time.AddSeconds(i), values[i]);
            }
            Assert.AreEqual(current, lwma.Current.Value);
            Assert.IsTrue(lwma.IsReady);
            Assert.AreNotEqual(0m, lwma.Current.Value);
            Assert.AreNotEqual(0, lwma.Samples);

            lwma.Reset();

            TestHelper.AssertIndicatorIsInDefaultState(lwma);
        }
        public void ResetsProperly()
        {
            const int period = 4;
            decimal[] values = { 1m, 2m, 3m, 4m, 5m };

            var lwma = new LinearWeightedMovingAverage(period);


            for (int i = 0; i < values.Length; i++)
            {
                lwma.Update(new IndicatorDataPoint(DateTime.UtcNow.AddSeconds(i), values[i]));
            }
            Assert.IsTrue(lwma.IsReady);
            Assert.AreNotEqual(0m, lwma.Current.Value);
            Assert.AreNotEqual(0, lwma.Samples);

            lwma.Reset();

            TestHelper.AssertIndicatorIsInDefaultState(lwma);
        }