Exemple #1
0
        public void onlineExampleTest()
        { // throws exception
          // from http://cns.bu.edu/~gsc/CN710/fincast/Technical%20_indicators/Relative%20Strength%20Index%20(RSI).htm
          // which uses a different calculation of RSI than ta4j
            ITimeSeries series = new MockTimeSeries(
                46.1250M,
                47.1250M, 46.4375M, 46.9375M, 44.9375M, 44.2500M, 44.6250M, 45.7500M,
                47.8125M, 47.5625M, 47.0000M, 44.5625M, 46.3125M, 47.6875M, 46.6875M,
                45.6875M, 43.0625M, 43.5625M, 44.8750M, 43.6875M);
            // ta4j RSI uses MMA for average gain and loss
            // then uses simple division of the two for RS
            IIndicator <decimal> indicator = getIndicator(new ClosePriceIndicator(
                                                              series), 14);
            IIndicator <decimal> close = new ClosePriceIndicator(series);
            IIndicator <decimal> gain  = new GainIndicator(close);
            IIndicator <decimal> loss  = new LossIndicator(close);
            // this site uses SMA for average gain and loss
            // then uses ratio of MMAs for RS (except for first calculation)
            IIndicator <decimal> avgGain = new SMAIndicator(gain, 14);
            IIndicator <decimal> avgLoss = new SMAIndicator(loss, 14);

            // first online calculation is simple division
            decimal onlineRs = avgGain.GetValue(14).DividedBy(avgLoss.GetValue(14));

            Assert.AreEqual(0.5848214285714285714285714286M, avgGain.GetValue(14));
            Assert.AreEqual(0.5446428571428571428571428571M, avgLoss.GetValue(14));
            Assert.AreEqual(1.0737704918032786885245901641M, onlineRs);
            decimal onlineRsi = 100M - (100M / (1M + onlineRs));

            // difference in RSI values:
            Assert.AreEqual(51.778656126482213438735177869M, onlineRsi);
            Assert.AreEqual(52.130477585417047385335308781M, indicator.GetValue(14));

            // strange, online average gain and loss is not a simple moving average!
            // but they only use them for the first RS calculation
            // Assert.AreEqual(0.5430, avgGain.getValue(15));
            // Assert.AreEqual(0.5772, avgLoss.getValue(15));
            // second online calculation uses MMAs
            // MMA of average gain
            decimal dividend = avgGain.GetValue(14).MultipliedBy(13M).Plus(gain.GetValue(15)).DividedBy(14M);
            // MMA of average loss
            decimal divisor = avgLoss.GetValue(14).MultipliedBy(13M).Plus(loss.GetValue(15)).DividedBy(14M);

            onlineRs = dividend / divisor;
            Assert.AreEqual(0.940883977900552486187845304M, onlineRs);
            onlineRsi = 100M - (100M / (1M + onlineRs));
            // difference in RSI values:
            Assert.AreEqual(48.477085112439510389980074014M, onlineRsi);
            Assert.AreEqual(47.37103140045740279363506511M, indicator.GetValue(15));
        }
Exemple #2
0
        protected override decimal Calculate(int index)
        {
            decimal sumOfGains = Decimals.Zero;

            for (int i = Math.Max(1, index - _timeFrame + 1); i <= index; i++)
            {
                sumOfGains = sumOfGains.Plus(_gainIndicator.GetValue(i));
            }
            decimal sumOfLosses = Decimals.Zero;

            for (int i = Math.Max(1, index - _timeFrame + 1); i <= index; i++)
            {
                sumOfLosses = sumOfLosses.Plus(_lossIndicator.GetValue(i));
            }
            return(sumOfGains.Minus(sumOfLosses)
                   .DividedBy(sumOfGains.Plus(sumOfLosses))
                   .MultipliedBy(Decimals.HUNDRED));
        }
Exemple #3
0
        public void gainUsingClosePrice()
        {
            GainIndicator gain = new GainIndicator(new ClosePriceIndicator(data));

            Assert.AreEqual(gain.GetValue(0), 0);
            Assert.AreEqual(gain.GetValue(1), 1);
            Assert.AreEqual(gain.GetValue(2), 1);
            Assert.AreEqual(gain.GetValue(3), 1);
            Assert.AreEqual(gain.GetValue(4), 0);
            Assert.AreEqual(gain.GetValue(5), 1);
            Assert.AreEqual(gain.GetValue(6), 3);
            Assert.AreEqual(gain.GetValue(7), 0);
            Assert.AreEqual(gain.GetValue(8), 0);
            Assert.AreEqual(gain.GetValue(9), 0);
            Assert.AreEqual(gain.GetValue(10), 2);
            Assert.AreEqual(gain.GetValue(11), 0);
            Assert.AreEqual(gain.GetValue(12), 0);
        }