Cassandre provides a simple way to manage your positions automatically.
In your strategy, you can create a long position with the createLongPosition()open in new window method.
It has three parameters :
- The currency pair, for example, ETH/USDT.
- The amount, for example, 0.5 ETH.
- The rules, for example, 100% stop gain and 50% stop loss.
The first step is to create the rules you want to apply to the position thanks to the PositionRulesDTOopen in new window class, for example:
PositionRulesDTO rules = PositionRulesDTO.builder() .stopGainPercentage(100) .stopLossPercentage(50) .build();
Then, you can create the position with that rule:
createLongPosition(new CurrencyPairDTO(ETH, BTC), new BigDecimal("0.5"), rules);
At this moment, Cassandre will create a buy order of 0.5 ETH (1 ETH costs 1500 USDT), and this will cost us 750 USDT. The position status will be OPENINGopen in new window, and when all the corresponding trades have arrived, the status will move to OPENEDopen in new window.
Note: if you want to check if you have enough funds available (at least 750 USDT in our case) before creating the position, you can use the canBuy()open in new window method.
From now on, for every ticker received, Cassandre will automatically calculate, with the new price (from the ticker), if closing the position at that price would trigger one of our two rules (100% stop gain and 50% stop loss).
For example, if we receive a new price of 3000 USDT for 1 ETH, Cassandre will calculate that if we sell our position right now (meaning "closing the position"), we will get 1 500 USDT, a 100% gain. As our rule is triggered, Cassandre will automatically create a selling order of our 0.5 ETH. The position status will move to CLOSINGopen in new window, and when all the corresponding trades have arrived, the status will move to CLOSEDopen in new window.
You can then know your exact gain on this position by calling the getGain()open in new window method.
A short positionopen in new window works the opposite way. With a short position, you bet that the price will go down.
Let's say you create a short position on 1 ETH with this command:
createShortPosition(new CurrencyPairDTO(ETH, BTC), new BigDecimal("1"), rules);
Cassandre will sell 1 ETH and get 1 500 USDT and wait until the price is down enough to buy 2 ETH with 1 500 USDT.
Note: if you want to check if you have enough funds available (at 1 ETH in our case) before creating the position, you can use the canSell()open in new window method.
On Positions, you can get the:
- The lowest calculated gain with getLowestCalculatedGain()open in new window
- The highest calculated gain with getHighestCalculatedGain()open in new window
- The latest calculated gain with getLatestCalculatedGain()open in new window
On a closed position, you can get the gain & fees with getGain()open in new window