Java Clean Architecture Masterclass

Java Clean Architecture MasterclassNov 20-21

Join

ta4j Build and Test Discord License: MIT Maven Central JDK

Technical Analysis for Java

Ta4j main chart

Build, test, and deploy trading bots in Java. With more than 190 (and counting) indicators, readable APIs, and production-minded tooling, you can explore markets, validate trading ideas, visualize signals, and ship automated bots without leaving the JVM.


Table of Contents


Why Ta4j?

Build, test, and deploy trading bots in Java—without leaving your favorite language or IDE. Ta4j gives you everything you need to explore markets, validate trading ideas, and ship production-ready automated trading systems.

What can you build?

Why Java developers choose Ta4j

Install in seconds

Add Ta4j from Maven Central:

<dependency>
  <groupId>org.ta4j</groupId>
  <artifactId>ta4j-core</artifactId>
  <version>0.21.0</version>
</dependency>

Prefer living on the edge? Use the snapshot repository and version:

<repository>
  <id>central-portal-snapshots</id>
  <url>https://central.sonatype.com/repository/maven-snapshots/</url>
</repository>

<dependency>
  <groupId>org.ta4j</groupId>
  <artifactId>ta4j-core</artifactId>
  <version>0.22.0-SNAPSHOT</version>
</dependency>

Sample applications are also published so you can copy/paste entire flows:

<dependency>
  <groupId>org.ta4j</groupId>
  <artifactId>ta4j-examples</artifactId>
  <version>0.21.0</version>
</dependency>

Like living on the edge? Use the snapshot version of ta4j-examples for the latest experimental/beta features:

<dependency>
  <groupId>org.ta4j</groupId>
  <artifactId>ta4j-examples</artifactId>
  <version>0.22.0-SNAPSHOT</version>
</dependency>

šŸ’” Tip: The ta4j-examples module includes runnable demos, data loaders, and charting utilities. It's a great way to see Ta4j in action and learn by example.

Try it now

Option 1: Run the Quickstart example (2-3 minutes)

# Clone the repository
git clone https://github.com/ta4j/ta4j.git
cd ta4j

# Build the project first
mvn clean install -DskipTests

# Run the Quickstart example (Quickstart is configured as the default)
mvn -pl ta4j-examples exec:java

Alternative: To run a different example class:

# On Linux/Mac/Git Bash
mvn -pl ta4j-examples exec:java -Dexec.mainClass=ta4jexamples.Quickstart

# On Windows CMD (use quotes)
mvn -pl ta4j-examples exec:java "-Dexec.mainClass=ta4jexamples.Quickstart"

This will load historical Bitcoin data, run a complete trading strategy, display performance metrics, and show an interactive chart—all in one go!

Option 2: Copy the code into your project (requires ta4j-core and ta4j-examples dependencies)

See the Quick start: Your first strategy section below for a complete, runnable example you can paste into your IDE.

Quick start: Your first strategy

Load price data, plug in indicators, and describe when to enter/exit. The API reads like the trading notes you already keep.

šŸ’” Want to see this in action? The Quickstart example includes this same pattern plus performance metrics and charting. Run it with:

mvn -pl ta4j-examples exec:java -Dexec.mainClass=ta4jexamples.Quickstart

Key concepts:

Note: The example below uses BitStampCsvTradesFileBarSeriesDataSource from ta4j-examples for convenience. See the Sourcing market data section below for more options.

import org.ta4j.core.*;
import org.ta4j.core.indicators.EMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.rules.*;
import org.ta4j.core.backtest.BarSeriesManager;
import ta4jexamples.datasources.BitStampCsvTradesFileBarSeriesDataSource;  // Requires ta4j-examples dependency

// Load historical price data (or use your own data source)
BarSeries series = BitStampCsvTradesFileBarSeriesDataSource.loadBitstampSeries();

// Create indicators: calculate moving averages from close prices
ClosePriceIndicator close = new ClosePriceIndicator(series);
EMAIndicator fastEma = new EMAIndicator(close, 12);  // 12-period EMA
EMAIndicator slowEma = new EMAIndicator(close, 26);  // 26-period EMA

// Define entry rule: buy when fast EMA crosses above slow EMA (golden cross)
Rule entry = new CrossedUpIndicatorRule(fastEma, slowEma);

// Define exit rule: sell when price gains 3% OR loses 1.5%
Rule exit = new StopGainRule(close, 3.0)      // take profit at +3%
        .or(new StopLossRule(close, 1.5));    // or cut losses at -1.5%

// Combine rules into a strategy
Strategy strategy = new BaseStrategy("EMA Crossover", entry, exit);

// Run the strategy on historical data
BarSeriesManager manager = new BarSeriesManager(series);
TradingRecord record = manager.run(strategy);

// See the results
System.out.println("Number of trades: " + record.getTradeCount());
System.out.println("Number of positions: " + record.getPositionCount());

Sourcing market data

New to trading and not sure where to get historical price data? You're not alone! Ta4j makes it easy to get started with real market data.

Quick solution: Yahoo Finance (no API key required)

The easiest way to get started is using the built-in YahooFinanceBarSeriesDataSource from ta4j-examples. It fetches real market data from Yahoo Finance's public API—no registration or API key needed.

import ta4jexamples.datasources.YahooFinanceBarSeriesDataSource;

// Enable response caching to avoid hitting API limits during development
YahooFinanceBarSeriesDataSource dataSource = new YahooFinanceBarSeriesDataSource(true);

// Load data by desired bar count (e.g., 2 years of daily candles)
BarSeries series = dataSource.loadSeriesInstance("AAPL", 
    YahooFinanceBarSeriesDataSource.YahooFinanceInterval.DAY_1, 730);

// Or load by date range
BarSeries series = dataSource.loadSeriesInstance("AAPL",
    YahooFinanceBarSeriesDataSource.YahooFinanceInterval.DAY_1,
    Instant.parse("2023-01-01T00:00:00Z"),
    Instant.parse("2023-12-31T23:59:59Z"));

Supported assets:

Supported intervals:

šŸ’” Tip: Enable caching (new YahooFinanceBarSeriesDataSource(true)) to cache API responses locally. This speeds up development and reduces API calls. Cached data is automatically reused for the same requests.

See it in action: Run the complete example with:

mvn -pl ta4j-examples exec:java -Dexec.mainClass=ta4jexamples.backtesting.YahooFinanceBacktest

This example demonstrates loading data from Yahoo Finance, building an advanced multi-indicator strategy (Bollinger Bands, RSI, ATR stops), running a backtest, and visualizing results.

Other data sources

Ta4j works with any OHLCV (Open, High, Low, Close, Volume) data. The ta4j-examples module includes examples for several data sources:

Create your own data source: Simply implement a method that returns a BarSeries. You can load data from:

See the Data Loading Examples section for more details.

Evaluate performance with metrics

Turn ideas into numbers. Add trading costs for realism and measure what matters—returns, risk, drawdowns, and more.

import org.ta4j.core.criteria.pnl.NetReturnCriterion;
import org.ta4j.core.criteria.drawdown.MaximumDrawdownCriterion;
import org.ta4j.core.cost.LinearTransactionCostModel;
import org.ta4j.core.cost.LinearBorrowingCostModel;

// Run backtest with realistic trading costs
// Transaction cost: 0.1% per trade (typical for crypto exchanges)
// Borrowing cost: 0.01% per period (for margin/short positions)
TradingRecord record = new BarSeriesManager(series,
        new LinearTransactionCostModel(0.001),      // 0.1% fee per trade
        new LinearBorrowingCostModel(0.0001))       // 0.01% borrowing cost
        .run(strategy);

// Calculate performance metrics
System.out.printf("Trades executed: %d%n", record.getTradeCount());
System.out.printf("Net return: %.2f%%%n", 
    new NetReturnCriterion().calculate(series, record).multipliedBy(series.numOf(100)));
System.out.printf("Max drawdown: %.2f%%%n", 
    new MaximumDrawdownCriterion().calculate(series, record).multipliedBy(series.numOf(100)));

// Explore more metrics: Sharpe ratio, win rate, profit factor, etc.
// See the wiki for the full list of available criteria

Backtest hundreds or even thousands of strategies

Want to find the top performers? Generate strategies with varying parameters and compare them:

// Generate strategies with varying parameters
List<Strategy> strategies = new ArrayList<>();
for (int fastPeriod = 5; fastPeriod <= 20; fastPeriod += 5) {
    for (int slowPeriod = 20; slowPeriod <= 50; slowPeriod += 10) {
        EMAIndicator fastEma = new EMAIndicator(close, fastPeriod);
        EMAIndicator slowEma = new EMAIndicator(close, slowPeriod);
        Rule entry = new CrossedUpIndicatorRule(fastEma, slowEma);
        Rule exit = new CrossedDownIndicatorRule(fastEma, slowEma);
        strategies.add(new BaseStrategy("EMA(" + fastPeriod + "," + slowPeriod + ")", entry, exit));
    }
}

// Run all strategies with progress tracking
BacktestExecutionResult result = new BacktestExecutor(series)
    .executeWithRuntimeReport(strategies, 
        series.numFactory().numOf(1),  // position size: 1 unit
        Trade.TradeType.BUY,           // long positions (use Trade.TradeType.SELL for shorts)
        ProgressCompletion.loggingWithMemory(); // logs progress with memory stats

// Get top 10 strategies sorted by net profit, then by RoMaD (for ties)
// You can sort by any combination of AnalysisCriterion - mix and match to find strategies that meet your goals
AnalysisCriterion returnOverMaxDradownCriterion = new ReturnOverMaxDrawdownCriterion();
AnalysisCriterion netProfitCriterion = new NetProfitCriterion();

List<TradingStatement> topStrategies = result.getTopStrategies(10,
    netProfitCriterion,    // primary sort: highest net profit first
    returnOverMaxDradownCriterion);  // secondary sort: highest RoMaD for ties

// Review the winners
topStrategies.forEach(statement -> {
    System.out.printf("Strategy: %s, Net Profit: %.2f, Return over Max Drawdown: %.2f%n",
        statement.getStrategy().getName(),
        statement.getCriterionScore(netProfitCriterion).orElse(series.numOf(0)),
        statement.getCriterionScore(returnOverMaxDradownCriterion).orElse(series.numOf(0)));
});

Visualize and share strategies

See your strategies in action. Ta4j includes charting helpers, but you're not locked in—serialize to JSON and use any visualization stack you prefer.

Built-in Java charting (using JFreeChart):

Basic strategy visualization with indicator overlays:

// Generate simplified chart - just price, indicators, and signals (no subchart)
ChartWorkflow chartWorkflow = new ChartWorkflow();
JFreeChart chart = chartWorkflow.builder()
        .withTitle("EMA Crossover Strategy")
        .withSeries(series) // Price bars (candlesticks)
        .withIndicatorOverlay(fastEma) // Overlay indicators on price chart
        .withIndicatorOverlay(slowEma)
        .withTradingRecordOverlay(record) // Mark entry/exit points with arrows
        .toChart();
chartWorkflow.saveChartImage(chart, series, "ema-crossover-strategy", "output/charts"); // Save as image

EMA Crossover Strategy Chart

The chart above shows candlestick price data with EMA lines overlaid and buy/sell signals marked with arrows. This demonstrates basic strategy visualization with indicator overlays.

Adding indicator subcharts for indicators with different scales (like RSI, which ranges from 0-100):

// Create indicators
ClosePriceIndicator close = new ClosePriceIndicator(series);
RSIIndicator rsi = new RSIIndicator(close, 14);

// RSI strategy: buy when RSI crosses below 30 (oversold), sell when RSI crosses
// above 70 (overbought)
Rule entry = new CrossedDownIndicatorRule(rsi, 30);
Rule exit = new CrossedUpIndicatorRule(rsi, 70);
Strategy strategy = new BaseStrategy("RSI Strategy", entry, exit);
TradingRecord record = new BarSeriesManager(series).run(strategy);

ChartWorkflow chartWorkflow = new ChartWorkflow();
JFreeChart chart = chartWorkflow.builder()
        .withTitle("RSI Strategy with Subchart")
        .withSeries(series) // Price bars (candlesticks)
        .withTradingRecordOverlay(record) // Mark entry/exit points
        .withSubChart(rsi) // RSI indicator in separate subchart panel
        .toChart();

RSI Strategy with Subchart

Visualizing performance metrics alongside your strategy:

// Create indicators: multiple moving averages
ClosePriceIndicator close = new ClosePriceIndicator(series);
SMAIndicator sma20 = new SMAIndicator(close, 20);
EMAIndicator ema12 = new EMAIndicator(close, 12);

// Strategy: buy when EMA crosses above SMA, sell when EMA crosses below SMA
Rule entry = new CrossedUpIndicatorRule(ema12, sma20);
Rule exit = new CrossedDownIndicatorRule(ema12, sma20);
Strategy strategy = new BaseStrategy("EMA/SMA Crossover", entry, exit);
TradingRecord record = new BarSeriesManager(series).run(strategy);

ChartWorkflow chartWorkflow = new ChartWorkflow();
JFreeChart chart = chartWorkflow.builder()
        .withTitle("Strategy Performance Analysis")
        .withSeries(series) // Price bars (candlesticks)
        .withIndicatorOverlay(sma20) // Overlay SMA on price chart
        .withIndicatorOverlay(ema12) // Overlay EMA on price chart
        .withTradingRecordOverlay(record) // Mark entry/exit points
        .withSubChart(new MaximumDrawdownCriterion(), record) // Performance metric in subchart
        .toChart();

Strategy Performance Analysis

This chart shows price action with indicator overlays, trading signals, and a performance subchart displaying maximum drawdown over time—helping you understand risk alongside returns.

Advanced multi-indicator analysis with multiple subcharts:

// Create indicators
ClosePriceIndicator close = new ClosePriceIndicator(series);
SMAIndicator sma50 = new SMAIndicator(close, 50);
EMAIndicator ema12 = new EMAIndicator(close, 12);
MACDIndicator macd = new MACDIndicator(close, 12, 26);
RSIIndicator rsi = new RSIIndicator(close, 14);

// Strategy: buy when EMA crosses above SMA and RSI > 50, sell when EMA crosses
// below SMA
Rule entry = new CrossedUpIndicatorRule(ema12, sma50).and(new OverIndicatorRule(rsi, 50));
Rule exit = new CrossedDownIndicatorRule(ema12, sma50);
Strategy strategy = new BaseStrategy("Advanced Multi-Indicator Strategy", entry, exit);
TradingRecord record = new BarSeriesManager(series).run(strategy);

ChartWorkflow chartWorkflow = new ChartWorkflow();
JFreeChart chart = chartWorkflow.builder()
        .withTitle("Advanced Multi-Indicator Strategy")
        .withSeries(series) // Price bars (candlesticks)
        .withIndicatorOverlay(sma50) // Overlay SMA on price chart
        .withIndicatorOverlay(ema12) // Overlay EMA on price chart
        .withTradingRecordOverlay(record) // Mark entry/exit points
        .withSubChart(macd) // MACD indicator in subchart
        .withSubChart(rsi) // RSI indicator in subchart
        .withSubChart(new NetProfitLossCriterion(), record) // Net profit/loss performance metric
        .toChart();

Advanced Multi-Indicator Strategy

This comprehensive chart demonstrates combining multiple indicators (MACD, RSI) in separate subcharts with performance metrics, giving you a complete view of strategy behavior.

See the chart at the top of this README for another example, or check the wiki's charting guide for more examples.

Export to any stack (Python, TypeScript, etc.):

Serialize indicators, rules, and strategies to JSON for persistence, sharing, or integration with other systems:

// Serialize an indicator (RSI) to JSON
ClosePriceIndicator close = new ClosePriceIndicator(series);
RSIIndicator rsi = new RSIIndicator(close, 14);
String rsiJson = rsi.toJson();
LOG.info("Output: {}", rsiJson);
// Output:
// {"type":"RSIIndicator","parameters":{"barCount":14},"components":[{"type":"ClosePriceIndicator"}]}
// Serialize a rule (AndRule) to JSON
Rule rule1 = new OverIndicatorRule(rsi, 50);
Rule rule2 = new UnderIndicatorRule(rsi, 80);
Rule andRule = new AndRule(rule1, rule2);
String ruleJson = ComponentSerialization.toJson(RuleSerialization.describe(andRule));
LOG.info("Output: {}", ruleJson);
// Output:
// {"type":"AndRule","label":"AndRule","components":[{"type":"OverIndicatorRule","label":"OverIndicatorRule","components":[{"type":"RSIIndicator","parameters":{"barCount":14},"components":[{"type":"ClosePriceIndicator"}]}],"parameters":{"threshold":50.0}},{"type":"UnderIndicatorRule","label":"UnderIndicatorRule","components":[{"type":"RSIIndicator","parameters":{"barCount":14},"components":[{"type":"ClosePriceIndicator"}]}],"parameters":{"threshold":80.0}}]}
// Serialize a strategy (EMA Crossover) to JSON
EMAIndicator fastEma = new EMAIndicator(close, 12);
EMAIndicator slowEma = new EMAIndicator(close, 26);
Rule entry = new CrossedUpIndicatorRule(fastEma, slowEma);
Rule exit = new CrossedDownIndicatorRule(fastEma, slowEma);
Strategy strategy = new BaseStrategy("EMA Crossover", entry, exit);
String strategyJson = strategy.toJson();
LOG.info("Output: {}", strategyJson);
// Output: {"type":"BaseStrategy","label":"EMA
// Crossover","parameters":{"unstableBars":0},"rules":[{"type":"CrossedUpIndicatorRule","label":"entry","components":[{"type":"EMAIndicator","parameters":{"barCount":12},"components":[{"type":"ClosePriceIndicator"}]},{"type":"EMAIndicator","parameters":{"barCount":26},"components":[{"type":"ClosePriceIndicator"}]}]},{"type":"CrossedDownIndicatorRule","label":"exit","components":[{"type":"EMAIndicator","parameters":{"barCount":12},"components":[{"type":"ClosePriceIndicator"}]},{"type":"EMAIndicator","parameters":{"barCount":26},"components":[{"type":"ClosePriceIndicator"}]}]}]}

Restore from JSON:

// Restore indicators and strategies from JSON
Indicator<?> restoredIndicator = Indicator.fromJson(series, indicatorJson);
Strategy restoredStrategy = Strategy.fromJson(series, strategyJson);

Features at a glance

From backtest to live trading

The same strategies you backtest can run live. Ta4j's deterministic calculations make it safe to deploy—test thoroughly, then execute with confidence.

import org.ta4j.core.builder.BaseBarSeriesBuilder;

// Create a live series (starts empty, grows as bars arrive)
BarSeries liveSeries = new BaseBarSeriesBuilder()
        .withName("BTC-USD")
        .build();

// Build your strategy (same code as backtesting!)
Strategy strategy = buildStrategy(liveSeries);

// Main trading loop: check for signals on each new bar
while (true) {
    // Fetch latest bar from your exchange/broker API
    Bar latest = fetchLatestBarFromBroker();  // Your integration here
    liveSeries.addBar(latest);

    int endIndex = liveSeries.getEndIndex();
    
    // Check entry/exit signals (same API as backtesting)
    if (strategy.shouldEnter(endIndex)) {
        placeBuyOrder();  // Your order execution logic
    } else if (strategy.shouldExit(endIndex)) {
        placeSellOrder(); // Your order execution logic
    }
    
    Thread.sleep(60000); // Wait 1 minute (or your bar interval)
}

Why this works:

Real-world examples

The ta4j-examples module includes runnable examples demonstrating common patterns and strategies:

Strategy Examples

Data Loading Examples

Analysis & Backtesting Examples

Charting Examples

šŸ’” Tip: Run any example with mvn -pl ta4j-examples exec:java -Dexec.mainClass=ta4jexamples.Quickstart (replace Quickstart with the class name).

Performance

Ta4j is designed for performance and scalability:

For detailed performance characteristics and benchmarks, see the wiki's performance guide (TODO: add link when available).

Community & Support

Get help, share ideas, and connect with other Ta4j users:

What's next?

New to technical analysis?

Ready to go deeper?

Need help?

Contributing

Release & snapshot publishing

Ta4j uses automated workflows for publishing both snapshot and stable releases.

Snapshots

Every push to master triggers a snapshot deployment:

mvn deploy

Snapshots are available at:

https://central.sonatype.com/repository/maven-snapshots/

Stable releases

For detailed information about the release process, see RELEASE_PROCESS.md.

Powered by

JetBrains logo.

Join libs.tech

...and unlock some superpowers

GitHub

We won't share your data with anyone else.