use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH in project cassandre-trading-bot by cassandre-tech.
the class MarketServiceTest method checkGetTicker.
@Test
@Tag("integration")
@DisplayName("Check get ticker")
public void checkGetTicker() {
CurrencyPairDTO cp = new CurrencyPairDTO(ETH, BTC);
Set<TickerDTO> tickers = marketService.getTickers(Collections.singleton(cp));
final Optional<TickerDTO> t = tickers.stream().filter(tickerDTO -> tickerDTO.getCurrencyPair().equals(cp)).findFirst();
assertTrue(t.isPresent());
// currencyPair.
assertNotNull(t.get().getCurrencyPair());
assertEquals(t.get().getCurrencyPair(), cp);
// open.
assertNull(t.get().getOpen());
// last.
assertNotNull(t.get().getLast());
assertTrue(t.get().getLast().compareTo(ZERO) >= 0);
// bid.
// assertNotNull(t.get().getBid());
// assertTrue(t.get().getBid().compareTo(ZERO) > 0);
// ask.
// assertNotNull(t.get().getAsk());
// assertTrue(t.get().getAsk().compareTo(ZERO) > 0);
// volume.
assertNotNull(t.get().getVolume());
// assertTrue(t.get().getVolume().compareTo(ZERO) > 0);
// quote volume.
assertNotNull(t.get().getQuoteVolume());
// assertTrue(t.get().getQuoteVolume().compareTo(ZERO) > 0);
// bidSize.
assertNull(t.get().getBidSize());
// askSize.
assertNull(t.get().getAskSize());
// timestamp.
assertNotNull(t.get().getTimestamp());
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.ETH 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.util.CurrencyDTO.ETH in project cassandre-trading-bot by cassandre-tech.
the class TradeServiceTest method checkCreateBuyLimitOrder.
@Test
@Tag("integration")
@DisplayName("Check creates a buy limit order")
public void checkCreateBuyLimitOrder() {
final CurrencyPairDTO cp = new CurrencyPairDTO(ETH, BTC);
// =============================================================================================================
// Making a buy limit order (Buy 0.0001 ETH).
final OrderCreationResultDTO result1 = strategy.createBuyLimitOrder(cp, new BigDecimal("0.0001"), new BigDecimal("0.1"));
assertTrue(result1.isSuccessful());
assertNull(result1.getErrorMessage());
assertNull(result1.getException());
assertNotNull(result1.getOrder().getOrderId());
// =============================================================================================================
// Getting a non-existing order.
assertFalse(tradeService.getOrders().stream().anyMatch(o -> o.getOrderId().equals("")));
// =============================================================================================================
// Getting the order and testing the data.
final Optional<OrderDTO> order1 = tradeService.getOrders().stream().filter(o -> o.getOrderId().equals(result1.getOrder().getOrderId())).findFirst();
assertTrue(order1.isPresent());
assertNotNull(order1.get().getOrderId());
assertEquals(result1.getOrder().getOrderId(), order1.get().getOrderId());
assertEquals(BID, order1.get().getType());
assertEquals(cp, order1.get().getCurrencyPair());
assertEquals(0, order1.get().getAmount().getValue().compareTo(new BigDecimal("0.0001")));
assertEquals(cp.getBaseCurrency(), order1.get().getAmount().getCurrency());
assertEquals(0, order1.get().getLimitPrice().getValue().compareTo(new BigDecimal("0.000001")));
assertEquals(cp.getQuoteCurrency(), order1.get().getLimitPrice().getCurrency());
assertNull(order1.get().getLeverage());
assertNull(order1.get().getUserReference());
assertNotNull(order1.get().getTimestamp());
assertTrue(order1.get().getTimestamp().isAfter(ZonedDateTime.now().minusMinutes(1)));
assertTrue(order1.get().getTimestamp().isBefore(ZonedDateTime.now().plusMinutes(1)));
// Cancel the order.
tradeService.cancelOrder(result1.getOrder().getOrderId());
}
use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH in project cassandre-trading-bot by cassandre-tech.
the class AccountDataFetcherTest method accountByAccountId.
@Test
@DisplayName("accountByAccountId(accountId: String): Account")
void accountByAccountId() {
// Query and fields definition.
GraphQLQueryRequest graphQLQueryRequest = new GraphQLQueryRequest(new AccountByAccountIdGraphQLQuery.Builder().accountId("trade").build(), new AccountByAccountIdProjectionRoot().accountId().name().balances().currency().code().getParent().total().available().frozen().loaned().borrowed().withdrawing().depositing());
// Query execution.
Account account = dgsQueryExecutor.executeAndExtractJsonPathAsObject(graphQLQueryRequest.serialize(), "data." + DgsConstants.QUERY.AccountByAccountId, new TypeRef<>() {
});
// Tests
assertNotNull(account);
assertEquals("trade", account.getAccountId());
assertEquals("trade account name", account.getName());
assertEquals(2, account.getBalances().size());
// Testing BTC balance.
final Optional<Balance> btcBalance = account.getBalances().stream().filter(balance -> balance.getCurrency().equals(BTC)).findAny();
assertTrue(btcBalance.isPresent());
assertEquals(BTC, btcBalance.get().getCurrency());
assertEquals(0, new BigDecimal("1").compareTo(btcBalance.get().getTotal()));
assertEquals(0, new BigDecimal("2").compareTo(btcBalance.get().getAvailable()));
assertEquals(0, new BigDecimal("3").compareTo(btcBalance.get().getFrozen()));
assertEquals(0, new BigDecimal("4").compareTo(btcBalance.get().getBorrowed()));
assertEquals(0, new BigDecimal("5").compareTo(btcBalance.get().getLoaned()));
assertEquals(0, new BigDecimal("6").compareTo(btcBalance.get().getWithdrawing()));
assertEquals(0, new BigDecimal("7").compareTo(btcBalance.get().getDepositing()));
// Testing ETH balance.
final Optional<Balance> ethBalance = account.getBalances().stream().filter(balance -> balance.getCurrency().equals(ETH)).findAny();
assertTrue(ethBalance.isPresent());
assertEquals(ETH, ethBalance.get().getCurrency());
assertEquals(0, new BigDecimal("11").compareTo(ethBalance.get().getTotal()));
assertEquals(0, new BigDecimal("22").compareTo(ethBalance.get().getAvailable()));
assertEquals(0, new BigDecimal("33").compareTo(ethBalance.get().getFrozen()));
assertEquals(0, new BigDecimal("44").compareTo(ethBalance.get().getBorrowed()));
assertEquals(0, new BigDecimal("55").compareTo(ethBalance.get().getLoaned()));
assertEquals(0, new BigDecimal("66").compareTo(ethBalance.get().getWithdrawing()));
assertEquals(0, new BigDecimal("77").compareTo(ethBalance.get().getDepositing()));
}
use of tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH 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());
}
Aggregations