Esempio n. 1
0
 /**
  * Constructor.
  *
  * @param indicator a price indicator
  * @param timeFrame the time frame
  */
 public CMOIndicator(IIndicator <decimal> indicator, int timeFrame)
     : base(indicator)
 {
     _gainIndicator = new GainIndicator(indicator);
     _lossIndicator = new LossIndicator(indicator);
     _timeFrame     = timeFrame;
 }
Esempio n. 2
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));
        }
Esempio n. 3
0
        public void lossUsingClosePrice()
        {
            LossIndicator loss = new LossIndicator(new ClosePriceIndicator(data));

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