Search in sources :

Example 21 with BitfinexCurrencyPair

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

the class BarMegerTest method testTickAlignment2.

/**
 * Test the alignment of the ticks
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 10000)
public void testTickAlignment2() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(4);
    final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (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 tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_15, tickConsumer);
    tickMerger.addNewPrice(parser.parse("01:01:00").getTime(), 1.0, 5.0);
    tickMerger.addNewPrice(parser.parse("02:41:33").getTime(), 2.0, 5.0);
    tickMerger.addNewPrice(parser.parse("10:33:11").getTime(), 2.0, 5.0);
    tickMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
    tickMerger.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 22 with BitfinexCurrencyPair

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

the class BarMegerTest method testTickMerger1.

/**
 * Test one tick
 * @throws InterruptedException
 * @throws IOException
 */
@Test(timeout = 5000)
public void testTickMerger1() throws InterruptedException, IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
        latch.countDown();
    };
    final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
    tickMerger.addNewPrice(1000000, 1.0, 5.0);
    tickMerger.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) Test(org.junit.Test)

Example 23 with BitfinexCurrencyPair

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

the class BarMegerTest method testTickMergerMinMax.

/**
 * Test tick merger min max
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 6000)
public void testTickMergerMinMax() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(1);
    final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
        Assert.assertEquals(1.0, t.getMinPrice().doubleValue(), DELTA);
        Assert.assertEquals(8.0, t.getMaxPrice().doubleValue(), DELTA);
        Assert.assertEquals(3.0, t.getOpenPrice().doubleValue(), DELTA);
        Assert.assertEquals(4.5, t.getClosePrice().doubleValue(), DELTA);
        latch.countDown();
    };
    final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
    tickMerger.addNewPrice(parser.parse("01:01:01").getTime(), 3.0, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:02").getTime(), 2.0, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:03").getTime(), 8.0, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:04").getTime(), 1.5, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:05").getTime(), 2.5, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:06").getTime(), 1.0, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:07").getTime(), 4.5, 5.0);
    tickMerger.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 24 with BitfinexCurrencyPair

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

the class DonchianBot method registerTicker.

/**
 * Register the ticker
 * @throws InterruptedException
 * @throws APIException
 */
protected void registerTicker() throws InterruptedException, APIException {
    logger.info("Register ticker");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        // Subscribe ticket on all connections (needed for wallet in USD conversion)
        for (final BitfinexApiBroker bitfinexApiBroker : apiBrokerList) {
            final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
            bitfinexApiBroker.getQuoteManager().subscribeTicker(symbol);
            logger.info("Wait for ticker");
            while (!bitfinexApiBroker.isTickerActive(symbol)) {
                Thread.sleep(100);
            }
        }
        // Use only one connection for merging
        tickMerger.put(currency, new BarMerger(currency, TIMEFRAME, (s, t) -> barDoneCallback(s, t)));
        final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
        apiBrokerList.get(0).getQuoteManager().registerTickCallback(symbol, (s, c) -> handleBarCallback(s, c));
    }
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) Arrays(java.util.Arrays) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) DonchianChannelUpper(com.github.jnidzwetzki.cryptobot.strategy.indicator.DonchianChannelUpper) Decimal(org.ta4j.core.Decimal) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) ArrayList(java.util.ArrayList) MaxPriceIndicator(org.ta4j.core.indicators.helpers.MaxPriceIndicator) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) PortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager) Map(java.util.Map) BitfinexClientFactory(com.github.jnidzwetzki.cryptobot.util.BitfinexClientFactory) DonchianChannelLower(com.github.jnidzwetzki.cryptobot.strategy.indicator.DonchianChannelLower) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) Logger(org.slf4j.Logger) BasePortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) MinPriceIndicator(org.ta4j.core.indicators.helpers.MinPriceIndicator) MathUtil(org.bboxdb.commons.MathUtil) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)

Example 25 with BitfinexCurrencyPair

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

the class DonchianBot method applySystemToPortfolioManager.

/**
 * Apply the orders to the portfolio manager
 * @param portfolioManager
 */
private void applySystemToPortfolioManager(final PortfolioManager portfolioManager) {
    try {
        final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
        final Map<BitfinexCurrencyPair, Double> exits = new HashMap<>();
        for (final BitfinexCurrencyPair currencyPair : tradedCurrencies) {
            final boolean open = portfolioManager.isPositionOpen(currencyPair.getCurrency1());
            // The channel values
            final double upperValue = getUpperChannelValue(currencyPair).doubleValue();
            final double lowerValue = getLowerChannelValue(currencyPair).doubleValue();
            final double channelSize = upperValue - lowerValue;
            // The prices
            final double entryPrice = adjustEntryPrice(upperValue);
            final double exitPrice = adjustExitPrice(lowerValue);
            if (!open) {
                final double lastPrice = getLastPriceForSymbol(currencyPair);
                // Filter entry orders to reduce capital allocation
                final double upperChannelHalf = lowerValue + (channelSize / 2);
                if (lastPrice > upperChannelHalf) {
                    final CurrencyEntry currencyEntry = new CurrencyEntry(currencyPair, entryPrice, exitPrice);
                    entries.put(currencyPair, currencyEntry);
                } else {
                    logger.info("Entry order for {} suppressed because price {} is to low {}", currencyPair, lastPrice, upperChannelHalf);
                }
            } else {
                exits.put(currencyPair, exitPrice);
            }
        }
        portfolioManager.syncOrders(entries, exits);
    } catch (APIException e) {
        logger.error("Got exception while executing trading system", e);
    } catch (InterruptedException e) {
        logger.error("Got interrupted exception");
        Thread.currentThread().interrupt();
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) HashMap(java.util.HashMap) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

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