React to signals

Now that your strategy is in place, Cassandre will call shouldEnter() when it's time to buy and shouldExit() when it's time to sell. You can do things manually by creating orders but you can also use positions.

For example, you can do something like that on shouldEnter() :

// Create rules.
PositionRulesDTO rules = PositionRulesDTO
.builder()
.stopGainPercentage(10)
.stopLossPercentage(5)
.create();
// Create position.
getPositionService().createPosition(
new CurrencyPairDTO(BTC, USDT),
new BigDecimal("0.01"),
rules);

Cassandre trading provides positions to automatically manage your trade this way. First, we created a rule saying this position should be closed if the gain is at least 10% or if the loss is at last 5%.

Then we called the createPosition() method. This will automatically create a buy order. From now, with every ticker received, Cassandre will check the gain or loss made on this position, if it triggers one of the rules, Cassandre will automatically make a sell order to close this position.

Inside your strategy, you can call canBuy() and canSell() method to see if your account has enough money to buy or sell assets.