Search in sources :

Example 21 with MarketOrder

use of org.knowm.xchange.dto.trade.MarketOrder in project cassandre-trading-bot by cassandre-tech.

the class PositionServiceTestMock method getXChangeTradeServiceMock.

@Override
public TradeService getXChangeTradeServiceMock() throws IOException {
    TradeService mock = mock(TradeService.class);
    // No trades returned - they will be directly emitted by the test.
    given(mock.getTradeHistory(any())).willReturn(new UserTrades(Collections.emptyList(), SortByTimestamp));
    // Position 1 creation reply (order ORDER00010).
    MarketOrder m = new MarketOrder(BID, new BigDecimal("0.0001"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00010");
    // Position 2 creation reply (order ORDER00020).
    m = new MarketOrder(BID, new BigDecimal("0.0002"), XCHANGE_ETH_USDT, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00020");
    // Position 3 creation reply (order ORDER00030).
    m = new MarketOrder(BID, new BigDecimal("0.0003"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willThrow(new RuntimeException("Error exception"));
    // For checkLowestHighestAndLatestGain().
    // Position 1.
    // Opening reply (order ORDER00010).
    m = new MarketOrder(BID, new BigDecimal("10"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00010");
    // Closing reply (order ORDER00011).
    m = new MarketOrder(ASK, new BigDecimal("10"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00011");
    return mock;
}
Also used : UserTrades(org.knowm.xchange.dto.trade.UserTrades) TradeService(org.knowm.xchange.service.trade.TradeService) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal)

Example 22 with MarketOrder

use of org.knowm.xchange.dto.trade.MarketOrder in project cassandre-trading-bot by cassandre-tech.

the class Issue863TestMock method getXChangeTradeServiceMock.

@Override
public TradeService getXChangeTradeServiceMock() throws IOException {
    TradeService mock = mock(TradeService.class);
    // No trades returned - they will be directly emitted by the test.
    given(mock.getTradeHistory(any())).willReturn(new UserTrades(Collections.emptyList(), SortByTimestamp));
    // Position 1 opening & closing reply (order ORDER00010).
    MarketOrder m = new MarketOrder(Order.OrderType.BID, new BigDecimal("0.0001"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00010");
    // Position 2 opening & closing reply (order ORDER00020).
    m = new MarketOrder(Order.OrderType.BID, new BigDecimal("0.0002"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00020");
    return mock;
}
Also used : UserTrades(org.knowm.xchange.dto.trade.UserTrades) TradeService(org.knowm.xchange.service.trade.TradeService) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal)

Example 23 with MarketOrder

use of org.knowm.xchange.dto.trade.MarketOrder in project cassandre-trading-bot by cassandre-tech.

the class OrderFluxTestMock method getXChangeTradeServiceMock.

@Override
public org.knowm.xchange.service.trade.TradeService getXChangeTradeServiceMock() throws IOException {
    org.knowm.xchange.service.trade.TradeService mock = mock(org.knowm.xchange.service.trade.TradeService.class);
    given(mock.getTradeHistory(any())).willReturn(new UserTrades(Collections.emptyList(), SortByTimestamp));
    // =============================================================================================================
    // Order creation mock.
    // Order ORDER_000001 (ASK, 1, ETH/BTC).
    MarketOrder m = new MarketOrder(Order.OrderType.ASK, new BigDecimal("1"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER_000001");
    // Order ORDER_000002 (BID, 2, ETH/USDT).
    m = new MarketOrder(Order.OrderType.BID, new BigDecimal("2"), XCHANGE_ETH_USDT, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER_000002");
    // Order ORDER_000003 (ASK, 3, ETH/BTC).
    m = new MarketOrder(Order.OrderType.ASK, new BigDecimal("3"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER_000003");
    // Order ORDER_000004 (BID, 4, ETH/USDT).
    m = new MarketOrder(Order.OrderType.BID, new BigDecimal("4"), XCHANGE_ETH_USDT, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER_000004");
    // =============================================================================================================
    // Order creation mock.
    given(mock.getOpenOrders()).willReturn(// - Order ORDER_000003.
    getReply01(), // - Order ORDER_000004 : new order (but not yet created in database).
    getReply02(), // - Order ORDER_000004 : original Amount changed.
    getReply03());
    return mock;
}
Also used : UserTrades(org.knowm.xchange.dto.trade.UserTrades) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) BigDecimal(java.math.BigDecimal)

Example 24 with MarketOrder

use of org.knowm.xchange.dto.trade.MarketOrder in project cassandre-trading-bot by cassandre-tech.

the class PositionLongFluxTestMock method getXChangeTradeServiceMock.

@Override
public org.knowm.xchange.service.trade.TradeService getXChangeTradeServiceMock() throws IOException {
    org.knowm.xchange.service.trade.TradeService mock = mock(org.knowm.xchange.service.trade.TradeService.class);
    // No trades or orders returned - they will be directly emitted during the test.
    given(mock.getTradeHistory(any())).willReturn(new UserTrades(Collections.emptyList(), SortByTimestamp));
    given(mock.getOpenOrders()).willReturn(new OpenOrders(Collections.emptyList()));
    // Position 1.
    // Opening order creation result.
    MarketOrder m = new MarketOrder(Order.OrderType.BID, new BigDecimal("10"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00010");
    // Closing order creation result.
    m = new MarketOrder(Order.OrderType.ASK, new BigDecimal("10"), XCHANGE_ETH_BTC, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00011");
    // Position 2.
    // Opening order creation result.
    m = new MarketOrder(Order.OrderType.BID, new BigDecimal("0.0002"), XCHANGE_ETH_USDT, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00020");
    // Closing order creation result.
    m = new MarketOrder(Order.OrderType.ASK, new BigDecimal("0.0002"), XCHANGE_ETH_USDT, null, null);
    given(mock.placeMarketOrder(m)).willReturn("ORDER00021");
    return mock;
}
Also used : UserTrades(org.knowm.xchange.dto.trade.UserTrades) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) BigDecimal(java.math.BigDecimal)

Example 25 with MarketOrder

use of org.knowm.xchange.dto.trade.MarketOrder in project XChange by knowm.

the class BitfinexAdapters method adaptOrders.

public static OpenOrders adaptOrders(BitfinexOrderStatusResponse[] activeOrders) {
    List<LimitOrder> limitOrders = new ArrayList<>();
    List<Order> hiddenOrders = new ArrayList<>();
    for (BitfinexOrderStatusResponse order : activeOrders) {
        OrderType orderType = order.getSide().equalsIgnoreCase("buy") ? OrderType.BID : OrderType.ASK;
        OrderStatus status = adaptOrderStatus(order);
        CurrencyPair currencyPair = adaptCurrencyPair(order.getSymbol());
        Date timestamp = convertBigDecimalTimestampToDate(order.getTimestamp());
        Supplier<MarketOrder> marketOrderCreator = () -> new MarketOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getAvgExecutionPrice(), order.getExecutedAmount(), null, status);
        Supplier<LimitOrder> limitOrderCreator = () -> new LimitOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getPrice(), order.getAvgExecutionPrice(), order.getExecutedAmount(), null, status);
        Supplier<StopOrder> stopOrderCreator = () -> new StopOrder(orderType, order.getOriginalAmount(), currencyPair, String.valueOf(order.getId()), timestamp, order.getPrice(), null, order.getAvgExecutionPrice(), order.getExecutedAmount(), status);
        LimitOrder limitOrder = null;
        StopOrder stopOrder = null;
        MarketOrder marketOrder = null;
        Optional<BitfinexOrderType> bitfinexOrderType = Arrays.stream(BitfinexOrderType.values()).filter(v -> v.getValue().equals(order.getType())).findFirst();
        if (bitfinexOrderType.isPresent()) {
            switch(bitfinexOrderType.get()) {
                case FILL_OR_KILL:
                    limitOrder = limitOrderCreator.get();
                    limitOrder.addOrderFlag(BitfinexOrderFlags.FILL_OR_KILL);
                    break;
                case MARGIN_FILL_OR_KILL:
                    limitOrder = limitOrderCreator.get();
                    limitOrder.addOrderFlag(BitfinexOrderFlags.FILL_OR_KILL);
                    limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
                    break;
                case MARGIN_LIMIT:
                    limitOrder = limitOrderCreator.get();
                    limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
                    break;
                case MARGIN_STOP:
                    stopOrder = stopOrderCreator.get();
                    stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
                    stopOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
                    break;
                case MARGIN_STOP_LIMIT:
                    stopLimitWarning();
                    stopOrder = stopOrderCreator.get();
                    stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
                    stopOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
                    break;
                case MARGIN_TRAILING_STOP:
                    limitOrder = limitOrderCreator.get();
                    limitOrder.addOrderFlag(BitfinexOrderFlags.TRAILING_STOP);
                    limitOrder.addOrderFlag(BitfinexOrderFlags.MARGIN);
                    break;
                case STOP:
                    stopOrder = stopOrderCreator.get();
                    stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
                    break;
                case STOP_LIMIT:
                    stopLimitWarning();
                    stopOrder = stopOrderCreator.get();
                    stopOrder.addOrderFlag(BitfinexOrderFlags.STOP);
                    break;
                case TRAILING_STOP:
                    limitOrder = limitOrderCreator.get();
                    limitOrder.addOrderFlag(BitfinexOrderFlags.TRAILING_STOP);
                    break;
                case LIMIT:
                    limitOrder = limitOrderCreator.get();
                    break;
                case MARGIN_MARKET:
                case MARKET:
                    marketOrder = marketOrderCreator.get();
                    break;
                default:
                    log.warn("Unhandled Bitfinex order type [{}]. Defaulting to limit order", order.getType());
                    limitOrder = limitOrderCreator.get();
                    break;
            }
        } else {
            log.warn("Unknown Bitfinex order type [{}]. Defaulting to limit order", order.getType());
            limitOrder = limitOrderCreator.get();
        }
        if (limitOrder != null) {
            limitOrders.add(limitOrder);
        } else if (stopOrder != null) {
            hiddenOrders.add(stopOrder);
        } else if (marketOrder != null) {
            hiddenOrders.add(marketOrder);
        }
    }
    return new OpenOrders(limitOrders, hiddenOrders);
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) FixedRateLoanOrder(org.knowm.xchange.dto.trade.FixedRateLoanOrder) Order(org.knowm.xchange.dto.Order) FloatingRateLoanOrder(org.knowm.xchange.dto.trade.FloatingRateLoanOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) StopOrder(org.knowm.xchange.dto.trade.StopOrder) Arrays(java.util.Arrays) OrderType(org.knowm.xchange.dto.Order.OrderType) Date(java.util.Date) BitfinexBalancesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexBalancesResponse) LoggerFactory(org.slf4j.LoggerFactory) BitfinexLendLevel(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLendLevel) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal) CurrencyPairMetaData(org.knowm.xchange.dto.meta.CurrencyPairMetaData) BitfinexDepositWithdrawalHistoryResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexDepositWithdrawalHistoryResponse) Map(java.util.Map) BitfinexTrade(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTrade) BitfinexPublicTrade(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexPublicTrade) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) Wallet(org.knowm.xchange.dto.account.Wallet) FixedRateLoanOrder(org.knowm.xchange.dto.trade.FixedRateLoanOrder) MathContext(java.math.MathContext) BitfinexAccountInfosResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexAccountInfosResponse) BitfinexTradeResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexTradeResponse) Collection(java.util.Collection) Set(java.util.Set) UserTrades(org.knowm.xchange.dto.trade.UserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) Currency(org.knowm.xchange.currency.Currency) Stream(java.util.stream.Stream) BitfinexLevel(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexLevel) Entry(java.util.Map.Entry) Optional(java.util.Optional) BitfinexTicker(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexTicker) BitfinexDepth(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexDepth) Movement(org.knowm.xchange.bitfinex.v2.dto.account.Movement) CurrencyPairDeserializer(org.knowm.xchange.utils.jackson.CurrencyPairDeserializer) BitfinexUtils(org.knowm.xchange.bitfinex.v1.BitfinexUtils) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) FundingRecord(org.knowm.xchange.dto.account.FundingRecord) Supplier(java.util.function.Supplier) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) ArrayList(java.util.ArrayList) TradeSortType(org.knowm.xchange.dto.marketdata.Trades.TradeSortType) BitfinexTickerFundingCurrency(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerFundingCurrency) Order(org.knowm.xchange.dto.Order) BitfinexSymbolDetail(org.knowm.xchange.bitfinex.v1.dto.marketdata.BitfinexSymbolDetail) CurrencyMetaData(org.knowm.xchange.dto.meta.CurrencyMetaData) ExchangeMetaData(org.knowm.xchange.dto.meta.ExchangeMetaData) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Logger(org.slf4j.Logger) Fee(org.knowm.xchange.dto.account.Fee) BitfinexAccountFeesResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexAccountFeesResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Trades(org.knowm.xchange.dto.marketdata.Trades) FloatingRateLoanOrder(org.knowm.xchange.dto.trade.FloatingRateLoanOrder) BitfinexTradingFeeResponse(org.knowm.xchange.bitfinex.v1.dto.account.BitfinexTradingFeeResponse) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) Trade(org.knowm.xchange.dto.marketdata.Trade) BitfinexOrderFlags(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderFlags) BitfinexTickerTraidingPair(org.knowm.xchange.bitfinex.v2.dto.marketdata.BitfinexTickerTraidingPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) WalletHealth(org.knowm.xchange.dto.meta.WalletHealth) DateUtils(org.knowm.xchange.utils.DateUtils) ArrayList(java.util.ArrayList) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) Date(java.util.Date) OrderStatus(org.knowm.xchange.dto.Order.OrderStatus) OrderType(org.knowm.xchange.dto.Order.OrderType) BitfinexOrderType(org.knowm.xchange.bitfinex.v1.BitfinexOrderType) StopOrder(org.knowm.xchange.dto.trade.StopOrder) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BitfinexOrderStatusResponse(org.knowm.xchange.bitfinex.v1.dto.trade.BitfinexOrderStatusResponse) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Aggregations

MarketOrder (org.knowm.xchange.dto.trade.MarketOrder)45 BigDecimal (java.math.BigDecimal)37 Test (org.junit.Test)20 Date (java.util.Date)17 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)15 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)10 TradeService (org.knowm.xchange.service.trade.TradeService)9 UserTrades (org.knowm.xchange.dto.trade.UserTrades)8 Order (org.knowm.xchange.dto.Order)7 OrderType (org.knowm.xchange.dto.Order.OrderType)6 LgoProductsTest (org.knowm.xchange.lgo.dto.product.LgoProductsTest)6 OpenOrders (org.knowm.xchange.dto.trade.OpenOrders)5 IOException (java.io.IOException)4 ThrowingCallable (org.assertj.core.api.ThrowableAssert.ThrowingCallable)4 Exchange (org.knowm.xchange.Exchange)4 StopOrder (org.knowm.xchange.dto.trade.StopOrder)4 OrderStatus (org.knowm.xchange.dto.Order.OrderStatus)3 ArrayList (java.util.ArrayList)2 ExchangeSpecification (org.knowm.xchange.ExchangeSpecification)2 UserTrade (org.knowm.xchange.dto.trade.UserTrade)2