Prerequisites
- JDK 21 or later. ta4j requires Java 21+. Check your version with
java -version. - Maven 3.9+ or Gradle 8+. Either build tool works. The examples below cover both.
Steps
Add the dependency
Add
ta4j-core to your project. If you want to use the built-in data loaders (Yahoo Finance, Coinbase, CSV), also add ta4j-examples.Load historical price data
A If you prefer not to add
BarSeries holds the OHLCV bars your strategy runs on. The quickest way to get real data is YahooFinanceHttpBarSeriesDataSource from ta4j-examples — no API key required.ta4j-examples, build a series manually with BaseBarSeriesBuilder and add bars from your own data source:Create indicators
Indicators calculate values from bar data. Start with You can inspect any indicator value at a specific bar index:
ClosePriceIndicator as the base, then layer derived indicators on top. Here we build a 12-period and 26-period exponential moving average.Define entry and exit rules
Rules are boolean conditions that the backtesting engine evaluates at each bar. The entry rule fires when the fast EMA crosses above the slow EMA — a classic golden cross signal. The exit rule closes the position when the price drops 1.5% (stop loss) or gains 3% (take profit).Rules compose with
.and(), .or(), and .xor(). For example, require an RSI confirmation on entry:Build the strategy
Wrap your entry and exit rules into a
BaseStrategy. Give it a name — this appears in reports and charts.Run the backtest
BarSeriesManager iterates over every bar in the series, evaluates your rules, and records every trade. Pass a transaction cost model to get realistic results.Complete example
Here is the full strategy assembled in one block, ready to paste into your project:Next steps
Core concepts
Understand how BarSeries, Indicators, Rules, and Strategies fit together.
Data sources
Load data from Yahoo Finance, Coinbase, CSV files, or your own broker API.
Performance metrics
Sharpe ratio, win rate, profit factor, return over max drawdown, and more.
Live trading
Use the same strategy code to drive live order execution.