use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.USD in project cassandre-trading-bot by cassandre-tech.
the class TradeServiceTest method checkGetTrades.
@Test
@Tag("integration")
@DisplayName("Check get trades")
public void checkGetTrades() {
final CurrencyPairDTO cp = new CurrencyPairDTO(BTC, USD);
// Creates two orders of the same amount (one buy, one sell).
final OrderCreationResultDTO result1 = strategy.createBuyMarketOrder(cp, new BigDecimal("0.1"));
final OrderCreationResultDTO result2 = strategy.createSellMarketOrder(cp, new BigDecimal("0.1"));
// Check that the two orders appears in the trade history.
assertTrue(result1.isSuccessful());
assertTrue(result2.isSuccessful());
await().untilAsserted(() -> assertTrue(tradeService.getTrades().stream().anyMatch(t -> t.getOrderId().equals(result1.getOrder().getOrderId()))));
await().untilAsserted(() -> assertTrue(tradeService.getTrades().stream().anyMatch(t -> t.getOrderId().equals(result2.getOrder().getOrderId()))));
// Retrieve trade & test values.
final Optional<TradeDTO> t = tradeService.getTrades().stream().filter(trade -> trade.getOrderId().equals(result1.getOrder().getOrderId())).findFirst();
assertTrue(t.isPresent());
assertNull(t.get().getId());
assertNotNull(t.get().getTradeId());
assertEquals(BID, t.get().getType());
assertEquals(result1.getOrderId(), t.get().getOrderId());
assertEquals(cp, t.get().getCurrencyPair());
assertNotNull(t.get().getAmount().getValue());
assertEquals(BTC, t.get().getAmount().getCurrency());
assertNotNull(t.get().getPrice().getValue());
assertNotNull(t.get().getFee().getValue());
assertNotNull(t.get().getFee().getCurrency());
assertTrue(t.get().getTimestamp().isAfter(ZonedDateTime.now().minusMinutes(1)));
assertTrue(t.get().getTimestamp().isBefore(ZonedDateTime.now().plusMinutes(1)));
}
use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.USD 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