use of org.ta4j.core.trading.rules.UnderIndicatorRule in project crypto-bot by jnidzwetzki.
the class BBreakoutStrategy method getStrategy.
public Strategy getStrategy() {
final ClosePriceIndicator closePrice = new ClosePriceIndicator(timeSeries);
final SMAIndicator sma = new SMAIndicator(closePrice, bbPeriod);
final BollingerBandsMiddleIndicator bbmiddle = new BollingerBandsMiddleIndicator(sma);
final StandardDeviationIndicator sd = new StandardDeviationIndicator(closePrice, bbPeriod);
final BollingerBandsUpperIndicator bbup = new BollingerBandsUpperIndicator(bbmiddle, sd, Decimal.valueOf(deviationUp));
final BollingerBandsUpperIndicator bbdown = new BollingerBandsUpperIndicator(bbmiddle, sd, Decimal.valueOf(deviationDown));
final Rule buyingRule = new UnderIndicatorRule(closePrice, bbdown);
final Rule sellingRule = new OverIndicatorRule(closePrice, bbup).or(new StopLossRule(closePrice, Decimal.valueOf(2)));
final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
return strategy;
}
use of org.ta4j.core.trading.rules.UnderIndicatorRule in project ta4j by ta4j.
the class GlobalExtremaStrategy method buildStrategy.
/**
* @param series a time series
* @return a global extrema strategy
*/
public static Strategy buildStrategy(TimeSeries series) {
if (series == null) {
throw new IllegalArgumentException("Series cannot be null");
}
ClosePriceIndicator closePrices = new ClosePriceIndicator(series);
// Getting the max price over the past week
MaxPriceIndicator maxPrices = new MaxPriceIndicator(series);
HighestValueIndicator weekMaxPrice = new HighestValueIndicator(maxPrices, NB_BARS_PER_WEEK);
// Getting the min price over the past week
MinPriceIndicator minPrices = new MinPriceIndicator(series);
LowestValueIndicator weekMinPrice = new LowestValueIndicator(minPrices, NB_BARS_PER_WEEK);
// Going long if the close price goes below the min price
MultiplierIndicator downWeek = new MultiplierIndicator(weekMinPrice, Decimal.valueOf("1.004"));
Rule buyingRule = new UnderIndicatorRule(closePrices, downWeek);
// Going short if the close price goes above the max price
MultiplierIndicator upWeek = new MultiplierIndicator(weekMaxPrice, Decimal.valueOf("0.996"));
Rule sellingRule = new OverIndicatorRule(closePrices, upWeek);
return new BaseStrategy(buyingRule, sellingRule);
}
use of org.ta4j.core.trading.rules.UnderIndicatorRule in project ta4j by ta4j.
the class MovingMomentumStrategy method buildStrategy.
/**
* @param series a time series
* @return a moving momentum strategy
*/
public static Strategy buildStrategy(TimeSeries series) {
if (series == null) {
throw new IllegalArgumentException("Series cannot be null");
}
ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
// The bias is bullish when the shorter-moving average moves above the longer moving average.
// The bias is bearish when the shorter-moving average moves below the longer moving average.
EMAIndicator shortEma = new EMAIndicator(closePrice, 9);
EMAIndicator longEma = new EMAIndicator(closePrice, 26);
StochasticOscillatorKIndicator stochasticOscillK = new StochasticOscillatorKIndicator(series, 14);
MACDIndicator macd = new MACDIndicator(closePrice, 9, 26);
EMAIndicator emaMacd = new EMAIndicator(macd, 18);
// Entry rule
Rule entryRule = // Trend
new OverIndicatorRule(shortEma, longEma).and(// Signal 1
new CrossedDownIndicatorRule(stochasticOscillK, Decimal.valueOf(20))).and(// Signal 2
new OverIndicatorRule(macd, emaMacd));
// Exit rule
Rule exitRule = // Trend
new UnderIndicatorRule(shortEma, longEma).and(// Signal 1
new CrossedUpIndicatorRule(stochasticOscillK, Decimal.valueOf(80))).and(// Signal 2
new UnderIndicatorRule(macd, emaMacd));
return new BaseStrategy(entryRule, exitRule);
}
use of org.ta4j.core.trading.rules.UnderIndicatorRule in project ta4j by ta4j.
the class RSI2Strategy method buildStrategy.
/**
* @param series a time series
* @return a 2-period RSI strategy
*/
public static Strategy buildStrategy(TimeSeries series) {
if (series == null) {
throw new IllegalArgumentException("Series cannot be null");
}
ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
SMAIndicator shortSma = new SMAIndicator(closePrice, 5);
SMAIndicator longSma = new SMAIndicator(closePrice, 200);
// We use a 2-period RSI indicator to identify buying
// or selling opportunities within the bigger trend.
RSIIndicator rsi = new RSIIndicator(closePrice, 2);
// Entry rule
// The long-term trend is up when a security is above its 200-period SMA.
Rule entryRule = // Trend
new OverIndicatorRule(shortSma, longSma).and(// Signal 1
new CrossedDownIndicatorRule(rsi, Decimal.valueOf(5))).and(// Signal 2
new OverIndicatorRule(shortSma, closePrice));
// Exit rule
// The long-term trend is down when a security is below its 200-period SMA.
Rule exitRule = // Trend
new UnderIndicatorRule(shortSma, longSma).and(// Signal 1
new CrossedUpIndicatorRule(rsi, Decimal.valueOf(95))).and(// Signal 2
new UnderIndicatorRule(shortSma, closePrice));
return new BaseStrategy(entryRule, exitRule);
}
use of org.ta4j.core.trading.rules.UnderIndicatorRule in project ta4j by ta4j.
the class CachedIndicatorTest method strategyExecutionOnCachedIndicatorAndLimitedTimeSeries.
@Test
public void strategyExecutionOnCachedIndicatorAndLimitedTimeSeries() {
TimeSeries timeSeries = new MockTimeSeries(0, 1, 2, 3, 4, 5, 6, 7);
SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(timeSeries), 2);
// Theoretical values for SMA(2) cache: 0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5
timeSeries.setMaximumBarCount(6);
// Theoretical values for SMA(2) cache: null, null, 2, 2.5, 3.5, 4.5, 5.5, 6.5
Strategy strategy = new BaseStrategy(new OverIndicatorRule(sma, Decimal.THREE), new UnderIndicatorRule(sma, Decimal.THREE));
// Theoretical shouldEnter results: false, false, false, false, true, true, true, true
// Theoretical shouldExit results: false, false, true, true, false, false, false, false
// As we return the first bar/result found for the removed bars:
// -> Approximated values for ClosePrice cache: 2, 2, 2, 3, 4, 5, 6, 7
// -> Approximated values for SMA(2) cache: 2, 2, 2, 2.5, 3.5, 4.5, 5.5, 6.5
// Then enters/exits are also approximated:
// -> shouldEnter results: false, false, false, false, true, true, true, true
// -> shouldExit results: true, true, true, true, false, false, false, false
assertFalse(strategy.shouldEnter(0));
assertTrue(strategy.shouldExit(0));
assertFalse(strategy.shouldEnter(1));
assertTrue(strategy.shouldExit(1));
assertFalse(strategy.shouldEnter(2));
assertTrue(strategy.shouldExit(2));
assertFalse(strategy.shouldEnter(3));
assertTrue(strategy.shouldExit(3));
assertTrue(strategy.shouldEnter(4));
assertFalse(strategy.shouldExit(4));
assertTrue(strategy.shouldEnter(5));
assertFalse(strategy.shouldExit(5));
assertTrue(strategy.shouldEnter(6));
assertFalse(strategy.shouldExit(6));
assertTrue(strategy.shouldEnter(7));
assertFalse(strategy.shouldExit(7));
}
Aggregations