Search in sources :

Example 6 with BitfinexOrder

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

the class OrderManager method placeOrder.

/**
 * Place a new order
 * @throws APIException
 */
public void placeOrder(final BitfinexOrder order) throws APIException {
    final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
    if (!capabilities.isHavingOrdersWriteCapability()) {
        throw new APIException("Unable to place order " + order + " connection has not enough capabilities: " + capabilities);
    }
    logger.info("Executing new order {}", order);
    final OrderCommand orderCommand = new OrderCommand(order);
    bitfinexApiBroker.sendCommand(orderCommand);
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) OrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)

Example 7 with BitfinexOrder

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

the class PortfolioOrderManager method closeTrade.

/**
 * Close a trade
 * @param trade
 */
public void closeTrade(final Trade trade) {
    if (!bitfinexApiBroker.isAuthenticated()) {
        logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
        return;
    }
    final double amount = trade.getAmount() * -1.0;
    final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
    try {
        trade.setTradeState(TradeState.CLOSING);
        trade.addCloseOrder(order);
        bitfinexApiBroker.getOrderManager().placeOrder(order);
        trade.setTradeState(TradeState.CLOSED);
    } catch (APIException e) {
        logger.error("Got an exception while closing trade {}", trade);
        trade.setTradeState(TradeState.ERROR);
    } finally {
        persistTrade(trade);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)

Example 8 with BitfinexOrder

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

the class PortfolioOrderManager method openTrade.

/**
 * Open a new trade
 * @param trade
 */
public void openTrade(final Trade trade) {
    if (!bitfinexApiBroker.isAuthenticated()) {
        logger.error("Unable to execute trade {} on marketplace, conecction is not authenticated", trade);
        return;
    }
    final double amount = trade.getAmount();
    final BitfinexOrder order = BitfinexOrderBuilder.create(trade.getSymbol(), BitfinexOrderType.EXCHANGE_MARKET, amount).build();
    try {
        trade.setTradeState(TradeState.OPENING);
        trade.addOpenOrder(order);
        bitfinexApiBroker.getOrderManager().placeOrder(order);
        trade.setTradeState(TradeState.OPEN);
    } catch (APIException e) {
        logger.error("Got an exception while opening trade {}", trade);
        trade.setTradeState(TradeState.ERROR);
    } finally {
        persistTrade(trade);
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)

Example 9 with BitfinexOrder

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

the class PortfolioManager method placeNewEntryOrders.

/**
 * Place the new entry orders
 * @param entries
 * @throws APIException
 * @throws InterruptedException
 */
private void placeNewEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
    // Check current limits and position sizes
    for (final BitfinexCurrencyPair currency : entries.keySet()) {
        final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
        final CurrencyEntry entry = entries.get(currency);
        final double entryPrice = entry.getEntryPrice();
        final double positionSize = entry.getPositionSize();
        if (positionSize < currency.getMinimumOrderSize()) {
            logger.info("Not placing order for {}, position size is too small {}", currency, positionSize);
            continue;
        }
        // Old order present
        if (order != null) {
            if (hasEntryOrderChanged(order, entryPrice, positionSize)) {
                logger.info("Entry order for {}, values changed (amount: {} / {}} (price: {} / {})", currency, order.getAmount(), positionSize, order.getPrice(), entryPrice);
                cancelOrder(order);
            } else {
                logger.info("Not placing a new order for {}, old order still active", currency);
                continue;
            }
        }
        final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSize).withPrice(entryPrice).setPostOnly().build();
        placeOrder(newOrder);
    }
}
Also used : BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) CurrencyEntry(com.github.jnidzwetzki.cryptobot.CurrencyEntry) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 10 with BitfinexOrder

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

the class TestPersistence method testStoreAndFetchOrder.

/**
 * Test order persistence
 */
@Test
public void testStoreAndFetchOrder() {
    final BitfinexOrder order = new BitfinexOrder(BitfinexCurrencyPair.BTC_USD, BitfinexOrderType.EXCHANGE_LIMIT, 0, 0, 0, 0, false, false, -1);
    final Session session = sessionFactory.openSession();
    @SuppressWarnings("unchecked") final List<BitfinexOrder> result1 = session.createQuery("from BitfinexOrder").list();
    // Assert.assertTrue(result1.isEmpty());
    for (final BitfinexOrder orderFetched : result1) {
        System.out.println("Order " + orderFetched);
    }
    session.beginTransaction();
    session.save(order);
    session.getTransaction().commit();
    @SuppressWarnings("unchecked") final List<BitfinexOrder> result2 = session.createQuery("from BitfinexOrder").list();
    Assert.assertEquals(1, result2.size());
    session.close();
}
Also used : BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)11 Test (org.junit.Test)6 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)5 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)5 CancelOrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand)4 OrderCommand (com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand)4 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)4 ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)3 CancelOrderGroupCommand (com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand)2 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)2 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)2 Retryer (org.bboxdb.commons.Retryer)2 Session (org.hibernate.Session)2 AbstractAPICommand (com.github.jnidzwetzki.bitfinex.v2.commands.AbstractAPICommand)1 AuthCommand (com.github.jnidzwetzki.bitfinex.v2.commands.AuthCommand)1 PingCommand (com.github.jnidzwetzki.bitfinex.v2.commands.PingCommand)1 SubscribeCandlesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand)1 SubscribeOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand)1 SubscribeRawOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)1 SubscribeTickerCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand)1