/// <inheritdoc />
        public IList <IndicatorValue> Calculate(IList <Price> prices)
        {
            var gains  = new List <IndicatorValue>();
            var losses = new List <IndicatorValue>();

            for (var i = 1; i < prices.Count; i++)
            {
                var change = prices[i].ClosePrice - prices[i - 1].ClosePrice;
                if (change > 0)
                {
                    gains.Add(new IndicatorValue {
                        Date = prices[i].Date, Value = change
                    });
                    losses.Add(new IndicatorValue {
                        Date = prices[i].Date, Value = 0
                    });
                }
                else
                {
                    losses.Add(new IndicatorValue {
                        Date = prices[i].Date, Value = -change
                    });
                    gains.Add(new IndicatorValue {
                        Date = prices[i].Date, Value = 0
                    });
                }
            }
            var averageGains  = MovingAverageHelper.SmoothedMovingAverage(gains, Term);
            var averageLosses = MovingAverageHelper.SmoothedMovingAverage(losses, Term);

            return(ComputeRsiValues(averageGains, averageLosses));
        }
Esempio n. 2
0
        /// <inheritdoc />
        public IList <Signal> GenerateSignals(IList <Price> prices)
        {
            var signals = new List <Signal>();
            var values  = Calculate(prices);
            var trend   = MovingAverageHelper.ExpotentialMovingAverage(prices, Term);

            for (int i = Term; i < prices.Count; i++)
            {
                if (prices[i].ClosePrice > trend[i - Term + 1].Value && trend[i - Term].Value < trend[i - Term + 1].Value && values[i - Term].Value < 0)
                {
                    signals.Add(new Signal(SignalAction.Buy)
                    {
                        Date = prices[i].Date
                    });
                }
                else if (prices[i].ClosePrice <trend[i - Term + 1].Value && trend[i - Term].Value> trend[i - Term + 1].Value && values[i - Term].Value > 0)
                {
                    signals.Add(new Signal(SignalAction.Sell)
                    {
                        Date = prices[i].Date
                    });
                }
            }
            return(signals);
        }
        /// <inheritdoc />
        public IList <IndicatorValue> Calculate(IList <Price> prices)
        {
            var longEma    = MovingAverageHelper.ExpotentialMovingAverage(prices, LongTerm);
            var shortEma   = MovingAverageHelper.ExpotentialMovingAverage(prices, ShortTerm);
            var macdLine   = SubstractLongEmaFromShortEma(shortEma, longEma);
            var signalLine = MovingAverageHelper.ExpotentialMovingAverage(macdLine, SignalTerm);

            return(PrepareResult(macdLine, signalLine));
        }
        /// <inheritdoc />
        public IList <IndicatorValue> Calculate(IList <Price> prices)
        {
            var ret = new List <IndicatorValue>();

            for (var i = 0; i < prices.Count - Term + 1; ++i)
            {
                ret.Add(MovingAverageHelper.SimpleMovingAverage(prices.Skip(i).Take(Term).ToList()));
            }
            return(ret);
        }
Esempio n. 5
0
        public void Moving_averages_should_ignore_holes_between_dates()
        {
            var data = new List <Price>
            {
                new Price {
                    ClosePrice = 10, Date = new DateTime(2016, 10, 1)
                },
                new Price {
                    ClosePrice = 20, Date = new DateTime(2016, 10, 4)
                }
            };
            var ema = MovingAverageHelper.ExpotentialMovingAverage(data, 1);

            ema.Count.Should().Be(2);
        }
        /// <inheritdoc />
        public IList <IndicatorValue> Calculate(IList <Price> prices)
        {
            var rsValues = GetRsValues(prices);

            return(MovingAverageHelper.SmoothedMovingAverage(rsValues, Term));
        }
Esempio n. 7
0
 /// <inheritdoc />
 public IList <Signal> GenerateSignals(IList <Price> prices)
 {
     return(MovingAverageHelper.GenerateSignalsForMovingAverages(this, Term, prices));
 }
Esempio n. 8
0
 /// <inheritdoc />
 public IList <IndicatorValue> Calculate(IList <Price> prices)
 {
     return(MovingAverageHelper.ExpotentialMovingAverage(prices, Term));
 }
Esempio n. 9
0
        public void ExpotentialMovingAverage12Test()
        {
            var actual12DaysEma = MovingAverageHelper.ExpotentialMovingAverage(MacdData.HistoricalData, 12);

            actual12DaysEma.ShouldAllBeEquivalentTo(MacdData.Ema12DaysResults);
        }
Esempio n. 10
0
        private void CalculateHelper(IList <Price> prices,
                                     out IList <IndicatorValue> smaValues,
                                     out IList <IndicatorValue> diplus,
                                     out IList <IndicatorValue> diminus,
                                     out IList <IndicatorValue> differences,
                                     out IList <IndicatorValue> differences2)
        {
            var plusDms  = new List <IndicatorValue>();
            var minusDms = new List <IndicatorValue>();

            for (var i = 1; i < prices.Count; ++i)
            {
                decimal plusDm, minusDm;
                var     upMove   = prices[i].HighPrice - prices[i - 1].HighPrice;
                var     downMove = prices[i - 1].LowPrice - prices[i].LowPrice;
                if (upMove > downMove && upMove > 0)
                {
                    plusDm = upMove;
                }
                else
                {
                    plusDm = 0;
                }
                if (downMove > upMove && downMove > 0)
                {
                    minusDm = downMove;
                }
                else
                {
                    minusDm = 0;
                }
                plusDms.Add(new IndicatorValue {
                    Date = prices[i].Date, Value = plusDm
                });
                minusDms.Add(new IndicatorValue {
                    Date = prices[i].Date, Value = minusDm
                });
            }
            var plusDisMovingAverage  = MovingAverageHelper.SmoothedSum(plusDms, Term);
            var minusDisMovingAverage = MovingAverageHelper.SmoothedSum(minusDms, Term);
            var atr            = MovingAverageHelper.SmoothedSum(GetRsValues(prices.Skip(1).ToList()), Term);
            var plusDis        = new List <IndicatorValue>();
            var minusDis       = new List <IndicatorValue>();
            var diDifferences  = new List <IndicatorValue>();
            var diDifferences2 = new List <IndicatorValue>();
            var dxs            = new List <IndicatorValue>();

            for (var i = 0; i < atr.Count; ++i)
            {
                if (atr[i].Value > 0)
                {
                    plusDis.Add(new IndicatorValue {
                        Date = atr[i].Date, Value = 100 * plusDisMovingAverage[i].Value / atr[i].Value
                    });
                    minusDis.Add(new IndicatorValue {
                        Date = atr[i].Date, Value = 100 * minusDisMovingAverage[i].Value / atr[i].Value
                    });
                    diDifferences.Add(new IndicatorValue {
                        Date = plusDis[i].Date, Value = Math.Abs(plusDis[i].Value - minusDis[i].Value)
                    });
                    diDifferences2.Add(new IndicatorValue {
                        Date = plusDis[i].Date, Value = plusDis[i].Value + minusDis[i].Value
                    });
                    dxs.Add(new IndicatorValue {
                        Date = atr[i].Date, Value = 100 * diDifferences[i].Value / diDifferences2[i].Value
                    });
                }
                else
                {
                    plusDis.Add(new IndicatorValue()
                    {
                        Date = atr[i].Date, Value = decimal.MaxValue
                    });
                    minusDis.Add(new IndicatorValue()
                    {
                        Date = atr[i].Date, Value = decimal.MaxValue
                    });
                    diDifferences.Add(new IndicatorValue()
                    {
                        Date = plusDis[i].Date, Value = 0
                    });
                    diDifferences2.Add(new IndicatorValue()
                    {
                        Date = minusDis[i].Date, Value = decimal.MaxValue
                    });
                    dxs.Add(new IndicatorValue()
                    {
                        Date = atr[i].Date, Value = 0
                    });
                }
            }
            var sma = MovingAverageHelper.SmoothedMovingAverage2(dxs, Term);

            diDifferences2 = diDifferences2.Skip(Term - 1).ToList();
            smaValues      = sma;
            diplus         = plusDis;
            diminus        = minusDis;
            differences    = diDifferences;
            differences2   = diDifferences2;
        }