# Position management
Cassandre provides a class to manage your positions automatically.
# Long position
In your strategy, you can create a long position with the createLongPosition() (opens 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 PositionRulesDTO (opens 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 (At that moment, 1 ETH costs 1500 USDT), and this will cost us 750 USDT. The position status will be OPENING (opens new window), and when all the corresponding trades have arrived, the status will move to OPENED (opens 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() (opens new window) method.
From now on, for every ticker received, Cassandre will automatically calculate, with the new price, 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 CLOSING (opens new window), and when all the corresponding trades have arrived, the status will move to CLOSED (opens new window).
You can then know your exact gain on this position by calling the getGain() (opens new window) method.
# Short position
A short position (opens 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 that 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() (opens new window) method.
On a position you can get the:
- The lowest calculated gain with getLowestPrice() (opens new window)
- The highest calculated gain with getHighestGainPrice() (opens new window)
- The latest calculated gain with getLatestGainPrice (opens new window)
Once the position is closed, you can get the gain & fees with getGain() (opens new window)