Cassandre : Dry mode & backtesting
Cassandre provides a dry mode allowing you to simulate exchange interactions. You can enable it by setting the parameter cassandre.trading.bot.exchange.modes.dry
to true
in src/main/resources/application.properties
.
With this mode, you can simulate accounts, orders, and positions. Cassandre will simulate valid exchange replies but won't execute them on the exchange. This way, you can test your strategy, see the gains you will make, and validate you have the results you expect.
The first step is to configure your(s) account(s), in Dry mode, Cassandre will search for all files starting with user
and ending with .tsv
or .csv
in src/test/resources
or src/test/resources
. They describe for each account, the balances for each cryptocurrency. For example, this is the content of user-trade.csv :
BTC,0.99962937
USDT,1000
ETH,10
When you will start Cassandre, you will see this:
22:53:38 - Adding account 'trade'
22:53:38 - - Adding balance 0.99962937 BTC
22:53:38 - - Adding balance 1000 USDT
22:53:38 - - Adding balance 10 ETH
Now you can create orders and positions. This will increase/decrease your account. Of course, if you don't have enough assets, orders and positions will not work.
You can use the dry mode to test your trading bot with real data but you can also use it for backtesting.
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-test to your project dependency.
Edit your pom.xml file and add :
<dependencies>
...
<dependency>
<groupId>tech.cassandre.trading.bot</groupId
<artifactId>cassandre-trading-bot-spring-boot-starter-test</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
...
</dependencies>
Now, we need to generate the data we want to use during your JUnit tests. We can use the Kucoin API, to do so, you can run this on the command line :
startDate=`date --date="3 months ago" +"%s"`
endDate=`date +"%s"`
curl -s "https://api.kucoin.com/api/v1/market/candles?type=1day&symbol=BTC-USDT&startAt=${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:
@Import(TickerFluxMock.class)
Now, instead of receiving tickers from the exchange, you will receive tickers imported from the tsv
files you put in src/test/resources
.
You can see an example of dry mode and backtesting in the Technical analysis chapter.