Esempio n. 1
0
        public void ifCacheWorks()
        {
            SMAIndicator sma        = new SMAIndicator(new ClosePriceIndicator(series), 3);
            decimal      firstTime  = sma.GetValue(4);
            decimal      secondTime = sma.GetValue(4);

            Assert.AreEqual(firstTime, secondTime);
        }
Esempio n. 2
0
        public void stochasticOscilatorDParam14UsingSMA3AndGenericConstructer()
        {
            StochasticOscillatorKIndicator sof = new StochasticOscillatorKIndicator(data, 14);
            SMAIndicator sma = new SMAIndicator(sof, 3);
            StochasticOscillatorDIndicator sos = new StochasticOscillatorDIndicator(sma);

            Assert.AreEqual(sma.GetValue(0), sos.GetValue(0));
            Assert.AreEqual(sma.GetValue(1), sos.GetValue(1));
            Assert.AreEqual(sma.GetValue(2), sos.GetValue(2));
        }
Esempio n. 3
0
        public void getValueWithOldResultsRemoval()
        {
            decimal[] data = new decimal[20];
            Arrays.fill(data, 1);
            ITimeSeries  timeSeries = new MockTimeSeries(data);
            SMAIndicator sma        = new SMAIndicator(new ClosePriceIndicator(timeSeries), 10);

            Assert.AreEqual(sma.GetValue(5), 1);
            Assert.AreEqual(sma.GetValue(10), 1);
            timeSeries.SetMaximumBarCount(12);
            Assert.AreEqual(sma.GetValue(19), 1);
        }
Esempio n. 4
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. 5
0
        public void getValueWithNullTimeSeries()
        {
            ConstantIndicator <decimal> constant = new ConstantIndicator <decimal>(Decimals.TEN);

            Assert.AreEqual(Decimals.TEN, constant.GetValue(0));
            Assert.AreEqual(Decimals.TEN, constant.GetValue(100));
            Assert.IsNull(constant.TimeSeries);

            SMAIndicator sma = new SMAIndicator(constant, 10);

            Assert.AreEqual(Decimals.TEN, sma.GetValue(0));
            Assert.AreEqual(Decimals.TEN, sma.GetValue(100));
            Assert.IsNull(sma.TimeSeries);
        }
Esempio n. 6
0
        /**
         * @param index the bar/candle index
         * @return true if the bar/candle has a very short upper shadow, false otherwise
         */
        private bool hasVeryShortUpperShadow(int index)
        {
            decimal currentUpperShadow = _upperShadowInd.GetValue(index);
            // We use the black candle index to remove to bias of the previous soldiers
            decimal averageUpperShadow = _averageUpperShadowInd.GetValue(blackCandleIndex);

            return(currentUpperShadow.IsLessThan(averageUpperShadow.MultipliedBy(_factor)));
        }
Esempio n. 7
0
        public void getValueWithCacheLengthIncrease()
        {
            decimal[] data = new decimal[200];
            Arrays.fill(data, 10);
            SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(new MockTimeSeries(data)), 100);

            Assert.AreEqual(sma.GetValue(105), 10);
        }
Esempio n. 8
0
        /**
         * @param index the bar/candle index
         * @return true if the bar/candle has a very short lower shadow, false otherwise
         */
        private bool hasVeryShortLowerShadow(int index)
        {
            decimal currentLowerShadow = _lowerShadowInd.GetValue(index);
            // We use the white candle index to remove to bias of the previous crows
            decimal averageLowerShadow = _averageLowerShadowInd.GetValue(whiteCandleIndex);

            return(currentLowerShadow.IsLessThan(averageLowerShadow.MultipliedBy(_factor)));
        }
Esempio n. 9
0
        public void valuesLessThanTimeFrameMustBeEqualsToSMAValues()
        {
            ZLEMAIndicator zlema = new ZLEMAIndicator(new ClosePriceIndicator(data), 10);
            SMAIndicator   sma   = new SMAIndicator(new ClosePriceIndicator(data), 10);

            for (int i = 0; i < 9; i++)
            {
                Assert.AreEqual(sma.GetValue(i), zlema.GetValue(i));
            }
        }
        public void bollingerBandsMiddleUsingSMA()
        {
            SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(data), 3);
            BollingerBandsMiddleIndicator bbmSMA = new BollingerBandsMiddleIndicator(sma);

            for (int i = 0; i < data.GetBarCount(); i++)
            {
                Assert.AreEqual(sma.GetValue(i), bbmSMA.GetValue(i));
            }
        }
Esempio n. 11
0
        protected override bool Calculate(int index)
        {
            if (index < 1)
            {
                return(_bodyHeightInd.GetValue(index).IsZero());
            }

            decimal averageBodyHeight = _averageBodyHeightInd.GetValue(index - 1);
            decimal currentBodyHeight = _bodyHeightInd.GetValue(index);

            return(currentBodyHeight.IsLessThan(averageBodyHeight.MultipliedBy(_factor)));
        }
Esempio n. 12
0
        public void getValueOnResultsCalculatedFromRemovedBarsShouldReturnFirstRemainingResult()
        {
            ITimeSeries timeSeries = new MockTimeSeries(1, 1, 1, 1, 1);

            timeSeries.SetMaximumBarCount(3);
            Assert.AreEqual(2, timeSeries.GetRemovedBarsCount());

            SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(timeSeries), 2);

            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(1, sma.GetValue(i), $"failed for {i}");
            }
        }
Esempio n. 13
0
        protected override decimal Calculate(int index)
        {
            decimal absoluteDeviations = Decimals.Zero;

            decimal average    = _sma.GetValue(index);
            int     startIndex = Math.Max(0, index - _timeFrame + 1);
            int     nbValues   = index - startIndex + 1;

            for (int i = startIndex; i <= index; i++)
            {
                // For each period[]
                absoluteDeviations = absoluteDeviations.Plus(_indicator.GetValue(i).Minus(average).Abs());
            }
            return(absoluteDeviations.DividedBy(nbValues));
        }
Esempio n. 14
0
        protected override decimal Calculate(int index)
        {
            int     startIndex           = Math.Max(0, index - _timeFrame + 1);
            int     numberOfObservations = index - startIndex + 1;
            decimal variance             = Decimals.Zero;
            decimal average = _sma.GetValue(index);

            for (int i = startIndex; i <= index; i++)
            {
                decimal Pow = _indicator.GetValue(i).Minus(average).Pow(2);
                variance = variance.Plus(Pow);
            }
            variance = variance.DividedBy(numberOfObservations);
            return(variance);
        }
Esempio n. 15
0
        protected override decimal Calculate(int index)
        {
            int     startIndex           = Math.Max(0, index - _timeFrame + 1);
            int     numberOfObservations = index - startIndex + 1;
            decimal covariance           = Decimals.Zero;
            decimal average1             = _sma1.GetValue(index);
            decimal average2             = _sma2.GetValue(index);

            for (int i = startIndex; i <= index; i++)
            {
                decimal mul = _indicator1.GetValue(i).Minus(average1).MultipliedBy(_indicator2.GetValue(i).Minus(average2));
                covariance = covariance.Plus(mul);
            }
            covariance = covariance.DividedBy(numberOfObservations);
            return(covariance);
        }
Esempio n. 16
0
        public void dpo()
        {
            DPOIndicator        dpo = new DPOIndicator(series, 9);
            ClosePriceIndicator cp  = new ClosePriceIndicator(series);
            SMAIndicator        sma = new SMAIndicator(cp, 9);
            int timeShift           = 9 / 2 + 1;

            for (int i = series.GetBeginIndex(); i <= series.GetEndIndex(); i++)
            {
                Assert.AreEqual(dpo.GetValue(i), cp.GetValue(i).Minus(sma.GetValue(i - timeShift)));
            }

            Assert.AreEqual(dpo.GetValue(9), 0.112M);
            Assert.AreEqual(dpo.GetValue(10), -0.02M);
            Assert.AreEqual(dpo.GetValue(11), 0.211428571428571428571428571M);
            Assert.AreEqual(dpo.GetValue(12), 0.17M);
        }
Esempio n. 17
0
 protected override decimal Calculate(int index)
 {
     // z-score = (ref - mean) / sd
     return((_indicator.GetValue(index).Minus(_mean.GetValue(index))).DividedBy(_sd.GetValue(index)));
 }