Search in sources :

Example 1 with BitfinexCurrencyPair

use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair 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 BitfinexCurrencyPair

use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair 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 BitfinexCurrencyPair

use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair 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 BitfinexCurrencyPair

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

the class BarMegerTest method testBarAlignment1.

/**
 * Test the alignment of the Bars
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 10000)
public void testBarAlignment1() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(3);
    final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
        Assert.assertTrue(t.getEndTime().getSecond() == 59);
        latch.countDown();
    };
    final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
    BarMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:02:33").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("02:03:53").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
    BarMerger.close();
    latch.await();
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BiConsumer(java.util.function.BiConsumer) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Test(org.junit.Test) ParseException(java.text.ParseException) Assert(org.junit.Assert) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) Bar(org.ta4j.core.Bar) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 5 with BitfinexCurrencyPair

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

the class BarMegerTest method testBarAlignment2.

/**
 * Test the alignment of the Bars
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 10000)
public void testBarAlignment2() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(4);
    final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
        Assert.assertTrue(t.getEndTime().getMinute() == 14 || t.getEndTime().getMinute() == 29 || t.getEndTime().getMinute() == 44 || t.getEndTime().getMinute() == 59);
        Assert.assertEquals(59, t.getEndTime().getSecond());
        latch.countDown();
    };
    final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_15, BarConsumer);
    BarMerger.addNewPrice(parser.parse("01:01:00").getTime(), 1.0, 5.0);
    BarMerger.addNewPrice(parser.parse("02:41:33").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("10:33:11").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
    BarMerger.close();
    latch.await();
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BiConsumer(java.util.function.BiConsumer) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Test(org.junit.Test) ParseException(java.text.ParseException) Assert(org.junit.Assert) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) Bar(org.ta4j.core.Bar) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)35 Test (org.junit.Test)21 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 Bar (org.ta4j.core.Bar)17 BarMerger (com.github.jnidzwetzki.cryptobot.util.BarMerger)16 BiConsumer (java.util.function.BiConsumer)15 IOException (java.io.IOException)14 ParseException (java.text.ParseException)14 SimpleDateFormat (java.text.SimpleDateFormat)14 Assert (org.junit.Assert)14 HashMap (java.util.HashMap)10 CurrencyEntry (com.github.jnidzwetzki.cryptobot.CurrencyEntry)8 BasePortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager)7 PortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager)7 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)6 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)6 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)5 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)4 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)4