@Test @SuppressWarnings("unchecked") public void calculatesRSIcorrectly() { when(upMa.indicate(any())).thenReturn(opt(new Price(100)), opt(new Price(150)), opt(new Price(95)), opt(new Price(90)), opt(new Price(10))); when(downMa.indicate(any())).thenReturn(opt(new Price(100)), opt(new Price(100)), opt(new Price(94)), opt(new Price(40)), opt(new Price(6))); assertThat(cut.indicate(CANDLE)).isEmpty(); assertThat(cut.indicate(CANDLE).get()).isApproximatelyEqualTo(new Ratio(50, PERCENT), OFFSET); assertThat(cut.indicate(CANDLE).get()).isApproximatelyEqualTo(new Ratio(60, PERCENT), OFFSET); assertThat(cut.indicate(CANDLE).get()).isApproximatelyEqualTo(new Ratio(50.26455, PERCENT), OFFSET); assertThat(cut.indicate(CANDLE).get()).isApproximatelyEqualTo(new Ratio(69.23077, PERCENT), OFFSET); assertThat(cut.indicate(CANDLE).get()).isApproximatelyEqualTo(new Ratio(62.5, PERCENT), OFFSET); }
@Override public Optional<Ratio> indicate(final MP price) { final Price lastPrice = this.lastPrice; final Price currentPrice = price.getClose(); this.lastPrice = price.getClose(); if (lastPrice == null) { return Optional.empty(); } if (lastPrice.equals(currentPrice)) { return rsi(noChange, noChange); } if (lastPrice.isLessThan(currentPrice)) { return rsi(new BasicMarketData(currentPrice.minus(lastPrice)), noChange); } else { return rsi(noChange, new BasicMarketData(lastPrice.minus(currentPrice))); } }
RelativeStrengthIndex implements Indicator<Ratio, MP> { @Override public Optional<Ratio> indicate(final MP price) { final Price lastPrice = this.lastPrice; final Price currentPrice = price.getClose(); this.lastPrice = price.getClose(); if (lastPrice == null) { return Optional.empty(); } if (lastPrice.equals(currentPrice)) { return rsi(noChange, noChange); } if (lastPrice.isLessThan(currentPrice)) { return rsi(new BasicMarketData(currentPrice.minus(lastPrice)), noChange); } else { return rsi(noChange, new BasicMarketData(lastPrice.minus(currentPrice))); } } }
RelativeStrengthIndex implements Indicator<Ratio, MP> { @Override public Optional<Ratio> indicate(final MP price) { final Price lastPrice = this.lastPrice; final Price currentPrice = price.getClose(); this.lastPrice = price.getClose(); if (lastPrice == null) { return Optional.empty(); } if (lastPrice.equals(currentPrice)) { return rsi(noChange, noChange); } if (lastPrice.isLessThan(currentPrice)) { return rsi(new BasicMarketData(currentPrice.minus(lastPrice)), noChange); } else { return rsi(noChange, new BasicMarketData(lastPrice.minus(currentPrice))); } } RelativeStrengthIndex(final int aggregationCount);  RelativeStrengthIndex(final Indicator<Price, MarketData> upMa,
            final Indicator<Price, MarketData> downMa); }
RelativeStrengthIndex implements Indicator<Ratio, MP> { @Override public Optional<Ratio> indicate(final MP price) { final Price lastPrice = this.lastPrice; final Price currentPrice = price.getClose(); this.lastPrice = price.getClose(); if (lastPrice == null) { return Optional.empty(); } if (lastPrice.equals(currentPrice)) { return rsi(noChange, noChange); } if (lastPrice.isLessThan(currentPrice)) { return rsi(new BasicMarketData(currentPrice.minus(lastPrice)), noChange); } else { return rsi(noChange, new BasicMarketData(lastPrice.minus(currentPrice))); } } RelativeStrengthIndex(final int aggregationCount);  RelativeStrengthIndex(final Indicator<Price, MarketData> upMa,
            final Indicator<Price, MarketData> downMa); @Override Optional<Ratio> indicate(final MP price); }
RelativeStrengthIndex implements Indicator<Ratio, MP> { @Override public Optional<Ratio> indicate(final MP price) { final Price lastPrice = this.lastPrice; final Price currentPrice = price.getClose(); this.lastPrice = price.getClose(); if (lastPrice == null) { return Optional.empty(); } if (lastPrice.equals(currentPrice)) { return rsi(noChange, noChange); } if (lastPrice.isLessThan(currentPrice)) { return rsi(new BasicMarketData(currentPrice.minus(lastPrice)), noChange); } else { return rsi(noChange, new BasicMarketData(lastPrice.minus(currentPrice))); } } RelativeStrengthIndex(final int aggregationCount);  RelativeStrengthIndex(final Indicator<Price, MarketData> upMa,
            final Indicator<Price, MarketData> downMa); @Override Optional<Ratio> indicate(final MP price);  }