Search in sources :

Example 1 with ExchangeOrder

use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.

the class PortfolioManager method cancelRemovedEntryOrders.

/**
 * Cancel the removed entry orders
 * Position is at the moment not interesting for an entry
 *
 * @param entries
 * @throws APIException
 * @throws InterruptedException
 */
private void cancelRemovedEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
    final List<ExchangeOrder> entryOrders = getAllOpenEntryOrders();
    for (final ExchangeOrder order : entryOrders) {
        final String symbol = order.getSymbol();
        final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
        if (!entries.containsKey(currencyPair)) {
            logger.info("Entry order for {} is not contained, canceling", currencyPair);
            cancelOrder(order);
        }
    }
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 2 with ExchangeOrder

use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.

the class PortfolioManager method placeNewExitOrders.

/**
 * Place the exit orders
 * @param exits
 * @throws APIException
 * @throws InterruptedException
 */
private void placeNewExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
    for (final BitfinexCurrencyPair currency : exits.keySet()) {
        final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
        final double exitPrice = exits.get(currency);
        // Check old orders
        if (order != null) {
            final double orderPrice = order.getPrice();
            if (orderPrice >= exitPrice || MathHelper.almostEquals(orderPrice, exitPrice)) {
                logger.info("Old order price for {} is fine (price: order {} model {})", currency, orderPrice, exitPrice);
                continue;
            }
            logger.info("Exit price for {} has moved form {} to {}, canceling old order", currency, orderPrice, exitPrice);
            cancelOrder(order);
        }
        final double positionSize = getOpenPositionSizeForCurrency(currency.getCurrency1());
        // * -1.0 for sell order
        final double positionSizeSell = positionSize * -1.0;
        final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSizeSell).withPrice(exitPrice).setPostOnly().build();
        placeOrder(newOrder);
    }
}
Also used : BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 3 with ExchangeOrder

use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.

the class PortfolioManager method cleanupOldExitOrders.

/**
 * Cleanup the old exit orders (remove duplicates, unknown orders)
 * @param exits
 * @throws APIException
 * @throws InterruptedException
 */
private void cleanupOldExitOrders(final Map<BitfinexCurrencyPair, Double> exits) throws APIException, InterruptedException {
    final List<ExchangeOrder> oldExitOrders = getAllOpenExitOrders();
    // Remove unknown orders
    for (final ExchangeOrder order : oldExitOrders) {
        final BitfinexCurrencyPair symbol = BitfinexCurrencyPair.fromSymbolString(order.getSymbol());
        if (!exits.containsKey(symbol)) {
            logger.error("Found old and unknown order {}, canceling", order);
            cancelOrder(order);
        }
    }
    // Remove duplicates
    final Map<String, List<ExchangeOrder>> oldOrders = oldExitOrders.stream().collect(Collectors.groupingBy(ExchangeOrder::getSymbol));
    for (final String symbol : oldOrders.keySet()) {
        final List<ExchangeOrder> symbolOrderList = oldOrders.get(symbol);
        if (symbolOrderList.size() > 1) {
            logger.error("Found duplicates {}", symbolOrderList);
            for (final ExchangeOrder order : symbolOrderList) {
                cancelOrder(order);
            }
        }
    }
}
Also used : List(java.util.List) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 4 with ExchangeOrder

use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project crypto-bot by jnidzwetzki.

the class EMABot method updateScreen.

public synchronized void updateScreen() {
    if (!UPDATE_SCREEN) {
        return;
    }
    final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
    CliTools.clearScreen();
    System.out.println("");
    System.out.println("==========");
    System.out.println("Last ticks");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
        System.out.println(symbol + " " + tickerManager.getLastTick(tickerSymbol));
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("Last bars");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        System.out.println(currency + " " + timeSeries.get(currency).getLastBar());
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("P/L");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
        final Trade trade = getOpenTrade(currency);
        if (trade != null) {
            final double priceIn = trade.getExpectedPriceOpen();
            final double currentPrice = tickerManager.getLastTick(tickerSymbol).getClose();
            System.out.println(symbol + ": price in " + priceIn + " / " + (currentPrice - priceIn));
        }
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("Trades");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final List<Trade> lastTrades = trades.get(currency);
        if (lastTrades == null) {
            continue;
        }
        lastTrades.sort((t1, t2) -> Long.compare(t2.getTid(), t1.getTid()));
        final List<Trade> lastTwoTrades = lastTrades.subList(Math.max(lastTrades.size() - 2, 0), lastTrades.size());
        for (final Trade trade : lastTwoTrades) {
            System.out.println(symbol + " " + trade);
        }
    }
    try {
        System.out.println("");
        System.out.println("==========");
        System.out.println("Orders");
        System.out.println("==========");
        final List<ExchangeOrder> orders = new ArrayList<>(bitfinexApiBroker.getOrderManager().getOrders());
        orders.sort((o1, o2) -> Long.compare(o2.getCid(), o1.getCid()));
        final List<ExchangeOrder> lastOrders = orders.subList(Math.max(orders.size() - 3, 0), orders.size());
        for (final ExchangeOrder order : lastOrders) {
            System.out.println(order);
        }
    } catch (APIException e) {
        logger.error("Got eception while reading wallets", e);
    }
}
Also used : Trade(com.github.jnidzwetzki.cryptobot.entity.Trade) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) ArrayList(java.util.ArrayList) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 5 with ExchangeOrder

use of com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class OrderManagerTest method testPlaceOrder.

/**
 * Test the placement of an order
 * @throws InterruptedException
 * @throws APIException
 */
@Test(timeout = 60000)
public void testPlaceOrder() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    Mockito.when(bitfinexApiBroker.isAuthenticated()).thenReturn(true);
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.MARKET, 1).build();
    final Runnable r = () -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            return;
        }
        final ExchangeOrder exchangeOrder = new ExchangeOrder();
        exchangeOrder.setCid(order.getCid());
        exchangeOrder.setState(ExchangeOrderState.STATE_ACTIVE);
        orderManager.updateOrder(exchangeOrder);
    };
    // Cancel event
    (new Thread(r)).start();
    orderManager.placeOrderAndWaitUntilActive(order);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Test(org.junit.Test)

Aggregations

ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)12 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)6 BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)6 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)5 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)4 ExchangeOrderState (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrderState)4 ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)3 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Consumer (java.util.function.Consumer)3 Test (org.junit.Test)3 CancelOrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand)2 CancelOrderGroupCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand)2 OrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand)2 Callable (java.util.concurrent.Callable)2 TimeUnit (java.util.concurrent.TimeUnit)2 Retryer (org.bboxdb.commons.Retryer)2 Logger (org.slf4j.Logger)2