Backtest your strategy

In simple words, backtesting a trading strategy is the process of testing a trading hypothesis/strategy on prior time periods.

Cassandre trading bot allows you to simulate your bots' reaction to historical data during tests.

The first step is to add cassandre-trading-bot-spring-boot-starter-testto your project dependency.

Edit your pom.xml file and add :


Maven Central

Now, we need to generate the data you want to use during your JUnit tests. To do so, you can run this on the command line :

startDate=`date --date="3 months ago" +"%s"`
endDate=`date +"%s"`
curl -s "${startDate}&endAt=${endDate}" \
| jq -r -c ".data[] | @tsv" \
| tac $1 > tickers-btc-usdt.tsv

It will create a file named tickers-btc-usdt.tsva that contains the historical rate of btc-usdt from startDate (3 months ago) to endDate (now). Of course, you can change dates and currency pair to choose which data you want to use.

Now place this file in the src/test/resources folder of our project and add this line to your JUnit test class:


Now, instead of receiving tickers from the exchange, you will receive tickers created from the tsv files you put in src/test/resources.

Your test is tech/cassandre/trading/bot/tutorial/ta/

To import real data, your test must import TickerFluxMock.class with the code:


Now we write the tests :

@DisplayName("Check gains")
public void gainTest() {
await().forever().until(() -> tickerFluxMock.isFluxDone());
final BigDecimal gains = strategy.getPositions()
.filter(p -> p.getStatus().equals(CLOSED))
.map(p -> p.getGain().getAmount().getValue())
.reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("Cumulated gains > " + gains);
assertTrue(gains.compareTo(BigDecimal.ZERO) > 0);
System.out.println("Position still opened :");
.filter(p -> p.getStatus().equals(OPENED))
.forEach(p -> System.out.println(" - " + p.getId()));

The first thing we do with the await() method is to wait until all data from btc-usdt.csv are imported.

Then, we calculate the gains of every closed position and we check that the gains are superior to zero.

The last thing we do is to display the list of positions that are not closed.