use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.USDT in project cassandre-trading-bot by cassandre-tech.
the class PositionGainsServiceTest method checkGainsCalculation.
@Test
@DisplayName("Check gains calculation")
public void checkGainsCalculation() {
// =============================================================================================================
/*
Position 1 - Bought 10 BTC with USDT.
TRADE_11 - Bought 7 for 11 = 77.
TRADE_12 - Bought 3 for 12 = 36.
TRADE_13 - Sold 1 for 13 = 13.
TRADE_14 - Sold 1 for 14 = 14.
TRADE_15 - Sold 8 for 15 = 120.
We bought 10 BTC for 113 USDT and sold them for 147 USDT.
Amount gain : 34 USDT.
Amount percentage : 30.08 % - ((147 - 113) / 113) * 100.
Fees : 15 USDT.
*/
final Optional<PositionDTO> p1 = positionService.getPositionByUid(1L);
assertTrue(p1.isPresent());
final GainDTO gain1 = p1.get().getGain();
// Gain (amount).
assertEquals(0, new BigDecimal("34").compareTo(gain1.getAmount().getValue()));
assertEquals(USDT, gain1.getAmount().getCurrency());
// Gain (percentage).
assertEquals(30.09, gain1.getPercentage());
// Gain (fees).
final Map<CurrencyDTO, CurrencyAmountDTO> gain1Fees = gain1.getFeesByCurrency();
assertEquals(1, gain1Fees.size());
assertNotNull(gain1Fees.get(USDT));
assertEquals(0, new BigDecimal("15").compareTo(gain1Fees.get(USDT).getValue()));
assertEquals(USDT, gain1Fees.get(USDT).getCurrency());
// =============================================================================================================
/*
Position 2 - Bought 20 ETH with BTC.
TRADE_21 - Bought 20 ETH for 100 = 2000.
TRADE_22 - Sold 20 ETH for 50 = 1000.
We bought 20 BTC for 2 000 BTC and sold them for 1 000 BTC.
Amount gain : -1 000 BTC.
Amount percentage : -50 % - ((1 000 - 2 000) / 2 000) * 100.
Fees : 10 BTC.
*/
final Optional<PositionDTO> p2 = positionService.getPositionByUid(2L);
assertTrue(p2.isPresent());
final GainDTO gain2 = p2.get().getGain();
// Gain (amount).
assertEquals(0, new BigDecimal("-1000").compareTo(gain2.getAmount().getValue()));
assertEquals(BTC, gain2.getAmount().getCurrency());
// Gain (percentage).
assertEquals(-50, gain2.getPercentage());
// Gain (fees).
final Map<CurrencyDTO, CurrencyAmountDTO> gain2Fees = gain2.getFeesByCurrency();
assertEquals(1, gain2Fees.size());
assertNotNull(gain2Fees.get(BTC));
assertEquals(0, new BigDecimal("10").compareTo(gain2Fees.get(BTC).getValue()));
assertEquals(BTC, gain2Fees.get(BTC).getCurrency());
// =============================================================================================================
/*
Position 3 - Bought 30 BTC with USDT.
TRADE_31 - Bought 30 BTC for 20 = 600.
TRADE_32 - Bought 30 BTC for 25 = 750.
We bought 30 BTC for 600 USDT and sold them for 750 USDT.
Amount gain : 150 USDT.
Amount percentage : 25% - ((750 - 600) / 600) * 100.
Fees : 11 USDT.
*/
final Optional<PositionDTO> p3 = positionService.getPositionByUid(3L);
assertTrue(p3.isPresent());
final GainDTO gain3 = p3.get().getGain();
// Gain (amount).
assertEquals(0, new BigDecimal("150").compareTo(gain3.getAmount().getValue()));
assertEquals(USDT, gain3.getAmount().getCurrency());
// Gain (percentage).
assertEquals(25, gain3.getPercentage());
// Gain (fees).
final Map<CurrencyDTO, CurrencyAmountDTO> gain3Fees = gain3.getFeesByCurrency();
assertEquals(1, gain3Fees.size());
assertNotNull(gain3Fees.get(USDT));
assertEquals(0, new BigDecimal("11").compareTo(gain3Fees.get(USDT).getValue()));
assertEquals(USDT, gain3Fees.get(USDT).getCurrency());
// There should be no gain for positions 4,5 & 6.
final Optional<PositionDTO> p4 = positionService.getPositionByUid(4L);
assertTrue(p4.isPresent());
assertEquals(0, p4.get().getGain().getPercentage());
assertEquals(0, ZERO.compareTo(p4.get().getGain().getAmount().getValue()));
final Optional<PositionDTO> p5 = positionService.getPositionByUid(5L);
assertTrue(p5.isPresent());
assertEquals(0, p5.get().getGain().getPercentage());
assertEquals(0, ZERO.compareTo(p5.get().getGain().getAmount().getValue()));
final Optional<PositionDTO> p6 = positionService.getPositionByUid(6L);
assertTrue(p6.isPresent());
assertEquals(0, p6.get().getGain().getPercentage());
assertEquals(0, ZERO.compareTo(p6.get().getGain().getAmount().getValue()));
// =============================================================================================================
/*
Position 7 (SHORT) - Sold 10 ETH for USDT.
TRADE_63 - Sold 10 ETH with a price of 5 = 50 USDT.
TRADE_64 - Bought ETH with 50 USDT with a price of 10 = 5 ETH
Amount gain : -5 ETH.
Amount percentage : -50%.
Fees : 4 USDT.
*/
final Optional<PositionDTO> p7 = positionService.getPositionByUid(7L);
assertTrue(p7.isPresent());
final GainDTO gain7 = p7.get().getGain();
// Gain (amount).
assertEquals(0, new BigDecimal("-5").compareTo(gain7.getAmount().getValue()));
assertEquals(ETH, gain7.getAmount().getCurrency());
// Gain (percentage).
assertEquals(-50, gain7.getPercentage());
// Gain (fees).
final Map<CurrencyDTO, CurrencyAmountDTO> gain7Fees = gain7.getFeesByCurrency();
assertEquals(1, gain7Fees.size());
assertNotNull(gain7Fees.get(ETH));
assertEquals(0, new BigDecimal("4").compareTo(gain7Fees.get(ETH).getValue()));
assertEquals(ETH, gain7Fees.get(ETH).getCurrency());
// =============================================================================================================
// Check all gains.
final Map<CurrencyDTO, GainDTO> gains = positionService.getGains();
assertEquals(3, gains.size());
// USDT Gains.
final GainDTO usdtGain = gains.get(USDT);
assertNotNull(usdtGain);
assertEquals(25.81, usdtGain.getPercentage());
assertEquals(0, new BigDecimal("184").compareTo(usdtGain.getAmount().getValue()));
assertEquals(USDT, usdtGain.getAmount().getCurrency());
// BTC Gains.
final GainDTO btcGain = gains.get(BTC);
assertNotNull(btcGain);
assertEquals(-50, btcGain.getPercentage());
assertEquals(0, new BigDecimal("-1000").compareTo(btcGain.getAmount().getValue()));
assertEquals(BTC, btcGain.getAmount().getCurrency());
// ETH Gains.
final GainDTO ethGain = gains.get(ETH);
assertNotNull(ethGain);
assertEquals(-50, ethGain.getPercentage());
assertEquals(0, new BigDecimal("-5").compareTo(ethGain.getAmount().getValue()));
assertEquals(ETH, ethGain.getAmount().getCurrency());
// ALl fees with getOrdersFees().
final Map<CurrencyDTO, CurrencyAmountDTO> fees = ethGain.getFeesByCurrency();
assertEquals(3, fees.size());
// USDT.
assertNotNull(fees.get(USDT));
assertEquals(0, new BigDecimal("26").compareTo(fees.get(USDT).getValue()));
assertEquals(USDT, fees.get(USDT).getCurrency());
// BTC.
assertNotNull(fees.get(BTC));
assertEquals(0, new BigDecimal("10").compareTo(fees.get(BTC).getValue()));
assertEquals(BTC, fees.get(BTC).getCurrency());
// ETH.
assertNotNull(fees.get(ETH));
assertEquals(0, new BigDecimal("4").compareTo(fees.get(ETH).getValue()));
assertEquals(ETH, fees.get(ETH).getCurrency());
// ALl fees with getOrdersFees().
assertEquals(3, ethGain.getFees().size());
// USDT.
Optional<CurrencyAmountDTO> usdtFees = ethGain.getFees().stream().filter(currencyAmountDTO -> currencyAmountDTO.getCurrency().equals(USDT)).findAny();
assertTrue(usdtFees.isPresent());
assertEquals(0, new BigDecimal("26").compareTo(usdtFees.get().getValue()));
assertEquals(USDT, usdtFees.get().getCurrency());
// BTC.
Optional<CurrencyAmountDTO> btcFees = ethGain.getFees().stream().filter(currencyAmountDTO -> currencyAmountDTO.getCurrency().equals(BTC)).findAny();
assertTrue(btcFees.isPresent());
assertEquals(0, new BigDecimal("10").compareTo(btcFees.get().getValue()));
assertEquals(BTC, btcFees.get().getCurrency());
// ETH.
Optional<CurrencyAmountDTO> ethFees = ethGain.getFees().stream().filter(currencyAmountDTO -> currencyAmountDTO.getCurrency().equals(ETH)).findAny();
assertTrue(ethFees.isPresent());
assertEquals(0, new BigDecimal("4").compareTo(ethFees.get().getValue()));
assertEquals(ETH, ethFees.get().getCurrency());
}
use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.USDT in project cassandre-trading-bot by cassandre-tech.
the class PositionTest method checkLoadPositionFromDatabase.
@Test
@DisplayName("Check load position from database")
public void checkLoadPositionFromDatabase() {
// =============================================================================================================
// Check position 1 - OPENING.
PositionDTO position1 = strategy.getPositions().get(1L);
assertNotNull(position1);
assertEquals(1L, position1.getUid());
assertEquals(1L, position1.getPositionId());
assertEquals(LONG, position1.getType());
assertNotNull(position1.getStrategy());
assertEquals(1, position1.getStrategy().getUid());
assertEquals("01", position1.getStrategy().getStrategyId());
assertEquals(new CurrencyPairDTO("BTC/USDT"), position1.getCurrencyPair());
assertEquals(0, new BigDecimal("10").compareTo(position1.getAmount().getValue()));
assertEquals(BTC, position1.getAmount().getCurrency());
assertFalse(position1.getRules().isStopGainPercentageSet());
assertFalse(position1.getRules().isStopLossPercentageSet());
assertEquals(OPENING, position1.getStatus());
assertEquals("BACKUP_OPENING_ORDER_01", position1.getOpeningOrder().getOrderId());
assertTrue(position1.getOpeningOrder().getTrades().isEmpty());
assertNull(position1.getClosingOrder());
assertNull(position1.getLowestGainPrice());
assertNull(position1.getHighestGainPrice());
assertNull(position1.getLatestGainPrice());
// Test equals.
Optional<PositionDTO> position1Bis = strategy.getPositionByPositionId(1L);
assertTrue(position1Bis.isPresent());
assertEquals(position1, position1Bis.get());
// =============================================================================================================
// Check position 2 - OPENED.
PositionDTO position2 = strategy.getPositions().get(2L);
assertNotNull(position2);
assertEquals(2L, position2.getUid());
assertEquals(2L, position2.getPositionId());
assertEquals(LONG, position2.getType());
assertNotNull(position2.getStrategy());
assertEquals(1, position2.getStrategy().getUid());
assertEquals("01", position2.getStrategy().getStrategyId());
assertEquals(new CurrencyPairDTO("BTC/USDT"), position2.getCurrencyPair());
assertEquals(0, new BigDecimal("20").compareTo(position2.getAmount().getValue()));
assertEquals(BTC, position2.getAmount().getCurrency());
assertTrue(position2.getRules().isStopGainPercentageSet());
assertEquals(10, position2.getRules().getStopGainPercentage());
assertFalse(position2.getRules().isStopLossPercentageSet());
assertEquals(OPENED, position2.getStatus());
assertEquals("BACKUP_OPENING_ORDER_02", position2.getOpeningOrder().getOrderId());
assertEquals(1, position2.getOpeningOrder().getTrades().size());
assertTrue(position2.getOpeningOrder().getTrade("BACKUP_TRADE_01").isPresent());
assertNull(position2.getClosingOrder());
assertEquals(0, new BigDecimal("1").compareTo(position2.getLowestGainPrice().getValue()));
assertEquals(USDT, position2.getLowestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("2").compareTo(position2.getHighestGainPrice().getValue()));
assertEquals(USDT, position2.getHighestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("3").compareTo(position2.getLatestGainPrice().getValue()));
assertEquals(USDT, position2.getLatestGainPrice().getCurrency());
// =============================================================================================================
// Check position 3 - CLOSING.
PositionDTO position3 = strategy.getPositions().get(3L);
assertNotNull(position3);
assertEquals(3L, position3.getUid());
assertEquals(3L, position3.getPositionId());
assertEquals(LONG, position3.getType());
assertNotNull(position3.getStrategy());
assertEquals(1, position3.getStrategy().getUid());
assertEquals("01", position3.getStrategy().getStrategyId());
assertEquals(new CurrencyPairDTO("BTC/USDT"), position3.getCurrencyPair());
assertEquals(0, new BigDecimal("30").compareTo(position3.getAmount().getValue()));
assertEquals(BTC, position3.getAmount().getCurrency());
assertFalse(position3.getRules().isStopGainPercentageSet());
assertTrue(position3.getRules().isStopLossPercentageSet());
assertEquals(20, position3.getRules().getStopLossPercentage());
assertEquals(CLOSING, position3.getStatus());
assertEquals("BACKUP_OPENING_ORDER_03", position3.getOpeningOrder().getOrderId());
assertEquals(1, position3.getOpeningOrder().getTrades().size());
assertTrue(position3.getOpeningOrder().getTrade("BACKUP_TRADE_02").isPresent());
assertEquals("BACKUP_CLOSING_ORDER_01", position3.getClosingOrder().getOrderId());
assertEquals(1, position3.getClosingOrder().getTrades().size());
assertTrue(position3.getClosingOrder().getTrade("BACKUP_TRADE_04").isPresent());
assertEquals(0, new BigDecimal("17").compareTo(position3.getLowestGainPrice().getValue()));
assertEquals(USDT, position3.getLowestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("68").compareTo(position3.getHighestGainPrice().getValue()));
assertEquals(USDT, position3.getHighestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("92").compareTo(position3.getLatestGainPrice().getValue()));
assertEquals(USDT, position3.getLatestGainPrice().getCurrency());
// =============================================================================================================
// Check position 4 - CLOSED.
PositionDTO position4 = strategy.getPositions().get(4L);
assertNotNull(position4);
assertEquals(4L, position4.getUid());
assertEquals(4L, position4.getPositionId());
assertEquals(LONG, position4.getType());
assertNotNull(position4.getStrategy());
assertEquals(1, position4.getStrategy().getUid());
assertEquals("01", position4.getStrategy().getStrategyId());
assertEquals(new CurrencyPairDTO("BTC/USDT"), position4.getCurrencyPair());
assertEquals(0, new BigDecimal("40").compareTo(position4.getAmount().getValue()));
assertEquals(BTC, position4.getAmount().getCurrency());
assertTrue(position4.getRules().isStopGainPercentageSet());
assertEquals(30, position4.getRules().getStopGainPercentage());
assertTrue(position4.getRules().isStopLossPercentageSet());
assertEquals(40, position4.getRules().getStopLossPercentage());
assertEquals(CLOSED, position4.getStatus());
assertEquals("BACKUP_OPENING_ORDER_04", position4.getOpeningOrder().getOrderId());
assertEquals(1, position4.getOpeningOrder().getTrades().size());
assertTrue(position4.getOpeningOrder().getTrade("BACKUP_TRADE_03").isPresent());
assertEquals("BACKUP_CLOSING_ORDER_02", position4.getClosingOrder().getOrderId());
assertEquals(1, position4.getClosingOrder().getTrades().size());
assertTrue(position4.getClosingOrder().getTrade("BACKUP_TRADE_05").isPresent());
assertEquals(0, new BigDecimal("17").compareTo(position4.getLowestGainPrice().getValue()));
assertEquals(USDT, position4.getLowestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("68").compareTo(position4.getHighestGainPrice().getValue()));
assertEquals(USDT, position4.getHighestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("93").compareTo(position4.getLatestGainPrice().getValue()));
assertEquals(USDT, position4.getLatestGainPrice().getCurrency());
// =============================================================================================================
// Check position 5 - CLOSED with several trades.
PositionDTO position5 = strategy.getPositions().get(5L);
assertNotNull(position5);
assertEquals(5L, position5.getUid());
assertEquals(5L, position5.getPositionId());
assertEquals(LONG, position5.getType());
assertNotNull(position5.getStrategy());
assertEquals(1, position5.getStrategy().getUid());
assertEquals("01", position5.getStrategy().getStrategyId());
assertEquals(new CurrencyPairDTO("ETH/USD"), position5.getCurrencyPair());
assertEquals(0, new BigDecimal("50").compareTo(position5.getAmount().getValue()));
assertEquals(ETH, position5.getAmount().getCurrency());
assertTrue(position5.getRules().isStopGainPercentageSet());
assertEquals(30, position5.getRules().getStopGainPercentage());
assertTrue(position5.getRules().isStopLossPercentageSet());
assertEquals(40, position5.getRules().getStopLossPercentage());
assertEquals(CLOSED, position5.getStatus());
assertEquals("BACKUP_OPENING_ORDER_05", position5.getOpeningOrder().getOrderId());
assertEquals("BACKUP_CLOSING_ORDER_03", position5.getClosingOrder().getOrderId());
assertEquals(0, new BigDecimal("17").compareTo(position5.getLowestGainPrice().getValue()));
assertEquals(USD, position5.getLowestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("68").compareTo(position5.getHighestGainPrice().getValue()));
assertEquals(USD, position5.getHighestGainPrice().getCurrency());
assertEquals(0, new BigDecimal("94").compareTo(position5.getLatestGainPrice().getValue()));
assertEquals(USD, position5.getLatestGainPrice().getCurrency());
// Open trades.
assertEquals(2, position5.getOpeningOrder().getTrades().size());
assertTrue(position5.getOpeningOrder().getTrade("BACKUP_TRADE_06").isPresent());
assertEquals("BACKUP_TRADE_06", position5.getOpeningOrder().getTrade("BACKUP_TRADE_06").get().getTradeId());
assertTrue(position5.getOpeningOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_06".equals(t.getTradeId())));
assertTrue(position5.getOpeningOrder().getTrade("BACKUP_TRADE_07").isPresent());
assertTrue(position5.getOpeningOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_07".equals(t.getTradeId())));
assertEquals("BACKUP_TRADE_07", position5.getOpeningOrder().getTrade("BACKUP_TRADE_07").get().getTradeId());
// Close trades.
assertEquals(3, position5.getClosingOrder().getTrades().size());
assertTrue(position5.getClosingOrder().getTrade("BACKUP_TRADE_08").isPresent());
assertTrue(position5.getClosingOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_08".equals(t.getTradeId())));
assertEquals("BACKUP_TRADE_08", position5.getClosingOrder().getTrade("BACKUP_TRADE_08").get().getTradeId());
assertTrue(position5.getClosingOrder().getTrade("BACKUP_TRADE_09").isPresent());
assertTrue(position5.getClosingOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_09".equals(t.getTradeId())));
assertEquals("BACKUP_TRADE_09", position5.getClosingOrder().getTrade("BACKUP_TRADE_09").get().getTradeId());
assertTrue(position5.getClosingOrder().getTrade("BACKUP_TRADE_10").isPresent());
assertTrue(position5.getClosingOrder().getTrades().stream().anyMatch(t -> "BACKUP_TRADE_10".equals(t.getTradeId())));
assertEquals("BACKUP_TRADE_10", position5.getClosingOrder().getTrade("BACKUP_TRADE_10").get().getTradeId());
// Check trade orders.
final Iterator<TradeDTO> openTradesIterator = position5.getOpeningOrder().getTrades().iterator();
assertEquals("BACKUP_TRADE_06", openTradesIterator.next().getTradeId());
assertEquals("BACKUP_TRADE_07", openTradesIterator.next().getTradeId());
final Iterator<TradeDTO> closeTradesIterator = position5.getClosingOrder().getTrades().iterator();
assertEquals("BACKUP_TRADE_08", closeTradesIterator.next().getTradeId());
assertEquals("BACKUP_TRADE_09", closeTradesIterator.next().getTradeId());
assertEquals("BACKUP_TRADE_10", closeTradesIterator.next().getTradeId());
}
Aggregations