use of tech.cassandre.trading.bot.dto.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.
the class TradeDTOTest method checkNullTrades.
@Test
@DisplayName("Check null trades")
public void checkNullTrades() {
TradeDTO t1 = TradeDTO.builder().tradeId("0000001").build();
TradeDTO t2 = TradeDTO.builder().tradeId("0000002").build();
assertNotEquals(t1, t2);
assertNotEquals(t2, t1);
}
use of tech.cassandre.trading.bot.dto.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.
the class BasicTa4jCassandreStrategyTestMock method tradeService.
@Bean
@Primary
public TradeService tradeService() {
TradeService service = mock(TradeService.class);
// Returns three values.
Set<OrderDTO> reply = new LinkedHashSet<>();
// Order 01.
reply.add(OrderDTO.builder().orderId("000001").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
// Order 02.
reply.add(OrderDTO.builder().orderId("000002").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
// Order 03.
reply.add(OrderDTO.builder().orderId("000003").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
// Order 04.
reply.add(OrderDTO.builder().orderId("000004").type(BID).strategy(strategyDTO).currencyPair(BTC_USDT).build());
given(service.getOrders()).willReturn(reply);
// Returns three values for getTrades().
Set<TradeDTO> replyGetTrades = new LinkedHashSet<>();
// Trade 01.
replyGetTrades.add(TradeDTO.builder().tradeId("0000001").orderId("000001").type(BID).currencyPair(BTC_USDT).build());
// Trade 02.
replyGetTrades.add(TradeDTO.builder().tradeId("0000002").orderId("000002").type(BID).currencyPair(BTC_USDT).build());
// Trade 03.
replyGetTrades.add(TradeDTO.builder().tradeId("0000003").orderId("000003").type(BID).currencyPair(BTC_USDT).build());
given(service.getTrades()).willReturn(replyGetTrades);
return service;
}
use of tech.cassandre.trading.bot.dto.trade.TradeDTO 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.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.
the class TradeServiceTest method checkGetTrades.
@Test
@Tag("integration")
@DisplayName("Check get trades")
@Disabled("Gemini doesn't support market order")
public void checkGetTrades() {
final CurrencyPairDTO cp = new CurrencyPairDTO(ETH, BTC);
// Creates two orders of the same amount (one buy, one sell).
final OrderCreationResultDTO result1 = strategy.createBuyMarketOrder(cp, new BigDecimal("0.0001"));
final OrderCreationResultDTO result2 = strategy.createSellMarketOrder(cp, new BigDecimal("0.0001"));
// Check that the two orders appears in the trade history.
assertTrue(result1.isSuccessful());
await().untilAsserted(() -> assertTrue(tradeService.getTrades().stream().anyMatch(t -> t.getOrderId().equals(result1.getOrder().getOrderId()))));
assertNotNull(result2.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(ETH, t.get().getAmount().getCurrency());
assertNotNull(t.get().getPrice().getValue());
assertEquals(BTC, t.get().getAmount().getCurrency());
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.trade.TradeDTO in project cassandre-trading-bot by cassandre-tech.
the class UserServiceTest method checkBalancesUpdate.
@Test
@DisplayName("Check balances updates")
public void checkBalancesUpdate() {
// We retrieve the account information in the strategy.
assertTrue(strategy.getAccountsUpdatesReceived().isEmpty());
accountFlux.update();
await().untilAsserted(() -> assertEquals(3, strategy.getAccountsUpdatesReceived().size()));
// =============================================================================================================
// Received ticker for ETH/BTC - It means 1 ETH can be bought with 0.032661 BTC.
// last = 0.032661 (Last trade field is the price set during the last trade)
tickerFlux.emitValue(TickerDTO.builder().currencyPair(ETH_BTC).last(new BigDecimal("0.032666")).bid(new BigDecimal("0.032466")).ask(new BigDecimal("0.032657")).high(new BigDecimal("0.034441")).low(new BigDecimal("0.032355")).volume(new BigDecimal("33794.9795777")).quoteVolume(new BigDecimal("1146.8453384314658")).build());
await().untilAsserted(() -> assertEquals(1, strategy.getLastTickers().size()));
// =============================================================================================================
// Account before buying.
// Loaded from spring-boot-starter/autoconfigure/src/test/resources/user-trade.csv
// BTC => 0.99962937
// ETH => 10
Optional<UserDTO> user = userService.getUser();
assertTrue(user.isPresent());
AccountDTO tradeAccount = user.get().getAccounts().get("trade");
Optional<BalanceDTO> tradeBTC = tradeAccount.getBalance(BTC);
assertTrue(tradeBTC.isPresent());
assertEquals(0, new BigDecimal("0.99962937").compareTo(tradeBTC.get().getAvailable()));
Optional<BalanceDTO> tradeETH = tradeAccount.getBalance(ETH);
assertTrue(tradeETH.isPresent());
assertEquals(0, new BigDecimal("10").compareTo(tradeETH.get().getAvailable()));
// =============================================================================================================
// Buying 0.02 ETH for 0.00065332 BTC.
// Last price from ticker * amount ordered
// 0.032666 * 0.02 = 0.00065332 BTC
strategy.createBuyMarketOrder(ETH_BTC, new BigDecimal("0.02"));
// =============================================================================================================
// We expect one account update (with the new ETH and BTC balances).
accountFlux.update();
await().untilAsserted(() -> assertEquals(4, strategy.getAccountsUpdatesReceived().size()));
// =============================================================================================================
// Account after buying (from strategy).
// BTC => 0.99897605 (previous amount - amount bought = 0.99962937 - 0.00065332)
// ETH => 10.02 (we bought 0.02)
tradeAccount = strategy.getAccounts().get("trade");
assertNotNull(tradeAccount);
tradeBTC = tradeAccount.getBalance(BTC);
assertTrue(tradeBTC.isPresent());
assertEquals(0, new BigDecimal("0.99897605").compareTo(tradeBTC.get().getAvailable()));
tradeETH = tradeAccount.getBalance(ETH);
assertTrue(tradeETH.isPresent());
assertEquals(0, new BigDecimal("10.02").compareTo(tradeETH.get().getAvailable()));
// =============================================================================================================
// Account after buying (from user service).
// BTC => 0.99897605 (previous amount - amount bought = 0.99962937 - 0.00065332)
// ETH => 10.02
user = userService.getUser();
assertTrue(user.isPresent());
tradeAccount = user.get().getAccounts().get("trade");
tradeBTC = tradeAccount.getBalance(BTC);
assertTrue(tradeBTC.isPresent());
assertEquals(0, new BigDecimal("0.99897605").compareTo(tradeBTC.get().getAvailable()));
tradeETH = tradeAccount.getBalance(ETH);
assertTrue(tradeETH.isPresent());
assertEquals(0, new BigDecimal("10.02").compareTo(tradeETH.get().getAvailable()));
// =============================================================================================================
// Testing the trade received.
// Amount => 0.02
// Price => 0.032666
await().untilAsserted(() -> {
orderFlux.update();
tradeFlux.update();
assertEquals(1, tradeRepository.count());
});
final Optional<TradeDTO> buyingTrade = tradeRepository.findByTradeId("DRY_TRADE_000000001").map(TRADE_MAPPER::mapToTradeDTO);
assertTrue(buyingTrade.isPresent());
assertEquals(BID, buyingTrade.get().getType());
assertEquals(0, new BigDecimal("0.02").compareTo(buyingTrade.get().getAmount().getValue()));
assertEquals(ETH, buyingTrade.get().getAmount().getCurrency());
assertEquals(0, new BigDecimal("0.032666").compareTo(buyingTrade.get().getPrice().getValue()));
assertEquals(BTC, buyingTrade.get().getPrice().getCurrency());
// =============================================================================================================
// Received ticker for ETH/BTC - It means 1 ETH can be bought with 0.032466 BTC.
// last = 0.032466 (Last trade field is the price set during the last trade)
tickerFlux.emitValue(TickerDTO.builder().currencyPair(ETH_BTC).last(new BigDecimal("0.032466")).bid(new BigDecimal("0.032466")).ask(new BigDecimal("0.032657")).high(new BigDecimal("0.034441")).low(new BigDecimal("0.032355")).volume(new BigDecimal("33794.9795777")).quoteVolume(new BigDecimal("1146.8453384314658")).build());
await().untilAsserted(() -> assertEquals(2, strategy.getTickersUpdatesReceived().size()));
// =============================================================================================================
// Selling 0.02 ETH.
// Amount * Last price from ticker
// 0.02 * 0.032466 = 0.00064932 ETH
strategy.createSellMarketOrder(ETH_BTC, new BigDecimal("0.02"));
// =============================================================================================================
// We expect one account update (with the new ETH and BTC balances).
accountFlux.update();
await().untilAsserted(() -> assertEquals(5, strategy.getAccountsUpdatesReceived().size()));
// =============================================================================================================
// Account values in strategy should be :
// BTC => 0.99962537 (previous sold + amount sold = 0.99897605 + 0.00064932)
// ETH => 10 (0.02 sold)
tradeAccount = strategy.getAccounts().get("trade");
assertNotNull(tradeAccount);
tradeBTC = tradeAccount.getBalance(BTC);
assertTrue(tradeBTC.isPresent());
assertEquals(0, new BigDecimal("0.99962537").compareTo(tradeBTC.get().getAvailable()));
tradeETH = tradeAccount.getBalance(ETH);
assertTrue(tradeETH.isPresent());
assertEquals(0, new BigDecimal("10").compareTo(tradeETH.get().getAvailable()));
// =============================================================================================================
// Testing the trade.
// Amount => 0.02
// Price => 0.032466
await().untilAsserted(() -> {
orderFlux.update();
tradeFlux.update();
assertEquals(2, tradeRepository.count());
});
final Optional<TradeDTO> sellingTrade = tradeRepository.findByTradeId("DRY_TRADE_000000002").map(TRADE_MAPPER::mapToTradeDTO);
assertTrue(sellingTrade.isPresent());
assertEquals(ASK, sellingTrade.get().getType());
assertEquals(0, new BigDecimal("0.02").compareTo(sellingTrade.get().getAmount().getValue()));
assertEquals(ETH, sellingTrade.get().getAmount().getCurrency());
assertEquals(0, new BigDecimal("0.032466").compareTo(sellingTrade.get().getPrice().getValue()));
}
Aggregations