Search in sources :

Example 11 with APIException

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

the class HistoricalCandlesHelper method requestHistoricalCandles.

/**
 * Request historical candles
 *
 * @param bitfinexApiBroker
 * @param timeframe
 * @param tradedCurrencies
 * @return
 * @throws InterruptedException
 * @throws APIException
 */
public static Map<BitfinexCandlestickSymbol, TimeSeries> requestHistoricalCandles(final BitfinexApiBroker bitfinexApiBroker, final Timeframe timeframe, final List<BitfinexCurrencyPair> tradedCurrencies) throws InterruptedException, APIException {
    logger.info("Request historical candles");
    final Map<BitfinexCandlestickSymbol, TimeSeries> timeSeries = new HashMap<>();
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String bitfinexString = currency.toBitfinexString();
        final BaseTimeSeries currencyTimeSeries = new BaseTimeSeries(bitfinexString);
        final BitfinexCandlestickSymbol barSymbol = new BitfinexCandlestickSymbol(currency, timeframe);
        timeSeries.put(barSymbol, currencyTimeSeries);
        final CountDownLatch tickCountdown = new CountDownLatch(100);
        // Add bars to timeseries callback
        final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (channelSymbol, tick) -> {
            final TimeSeries timeSeriesToAdd = timeSeries.get(channelSymbol);
            final Bar bar = BarConverter.convertBitfinexTick(tick);
            try {
                timeSeriesToAdd.addBar(bar);
                tickCountdown.countDown();
            } catch (IllegalArgumentException e) {
                logger.error("Unable to add tick {} to time series, last tick is {}", bar, timeSeriesToAdd.getLastBar());
            }
        };
        bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
        bitfinexApiBroker.getQuoteManager().subscribeCandles(barSymbol);
        // Wait for 100 bars or 10 seconds. All snapshot ticks are handled in
        // a synchronized block, so we receive the full snapshot even if we
        // call removeTickCallback.
        tickCountdown.await(10, TimeUnit.SECONDS);
        bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
        bitfinexApiBroker.getQuoteManager().unsubscribeCandles(barSymbol);
        logger.info("Loaded ticks for symbol {} {}", bitfinexString, +timeSeries.get(barSymbol).getEndIndex());
    }
    return timeSeries;
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) Bar(org.ta4j.core.Bar) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) TimeUnit(java.util.concurrent.TimeUnit) BaseTimeSeries(org.ta4j.core.BaseTimeSeries) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) BarConverter(com.github.jnidzwetzki.cryptobot.util.BarConverter) TimeSeries(org.ta4j.core.TimeSeries) BaseTimeSeries(org.ta4j.core.BaseTimeSeries) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Bar(org.ta4j.core.Bar) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BaseTimeSeries(org.ta4j.core.BaseTimeSeries) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

Example 12 with APIException

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

the class ExecutedTradesHandlerTest method testExecutedTradesSnapshotUpdateAndNotify.

/**
 * Test the parsing of a executed trades snapshot
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testExecutedTradesSnapshotUpdateAndNotify() throws APIException, InterruptedException {
    final String callbackValue = "[[190631057,1518037080162,0.007,8175.9],[190631052,1518037080110,-0.25,8175.8]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final QuoteManager quoteManager = new QuoteManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(quoteManager);
    final CountDownLatch latch = new CountDownLatch(2);
    quoteManager.registerExecutedTradeCallback(symbol, (s, c) -> {
        try {
            Assert.assertEquals(symbol, s);
            if (c.getId() == 190631057) {
                Assert.assertEquals(190631057, c.getId());
                Assert.assertEquals(1518037080162l, c.getTimestamp());
                Assert.assertEquals(0.007, c.getAmount().doubleValue(), DELTA);
                Assert.assertEquals(8175.9, c.getPrice().doubleValue(), DELTA);
            } else if (c.getId() == 190631052) {
                Assert.assertEquals(190631052, c.getId());
                Assert.assertEquals(1518037080110l, c.getTimestamp());
                Assert.assertEquals(-0.25, c.getAmount().doubleValue(), DELTA);
                Assert.assertEquals(8175.8, c.getPrice().doubleValue(), DELTA);
            } else {
                throw new IllegalArgumentException("Illegal call, expected 2 trades");
            }
            latch.countDown();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    });
    final ExecutedTradeHandler handler = new ExecutedTradeHandler();
    handler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    latch.await();
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) ExecutedTradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with APIException

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

the class ExecutedTradesHandlerTest method testExecutedTradesUpdateAndNotify.

/**
 * Test the parsing of one executed trade
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testExecutedTradesUpdateAndNotify() throws APIException, InterruptedException {
    final String callbackValue = "[190631057,1518037080162,0.007,8175.9]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final QuoteManager quoteManager = new QuoteManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(quoteManager);
    final CountDownLatch latch = new CountDownLatch(1);
    quoteManager.registerExecutedTradeCallback(symbol, (s, c) -> {
        try {
            Assert.assertEquals(symbol, s);
            Assert.assertEquals(190631057, c.getId());
            Assert.assertEquals(1518037080162l, c.getTimestamp());
            Assert.assertEquals(0.007, c.getAmount().doubleValue(), DELTA);
            Assert.assertEquals(8175.9, c.getPrice().doubleValue(), DELTA);
            latch.countDown();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    });
    final ExecutedTradeHandler handler = new ExecutedTradeHandler();
    handler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    latch.await();
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) ExecutedTradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 14 with APIException

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

the class OrderManagerTest method testCancelOrderUnauth.

/**
 * Test the cancelation of an order
 * @throws InterruptedException
 * @throws APIException
 */
@Test(expected = APIException.class)
public void testCancelOrderUnauth() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    Mockito.when(bitfinexApiBroker.getCapabilities()).thenReturn(ConnectionCapabilities.NO_CAPABILITIES);
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    orderManager.cancelOrderAndWaitForCompletion(12);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) Test(org.junit.Test)

Example 15 with APIException

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

the class OrderManagerTest method testOrderChannelHandler2.

/**
 * Test the order channel handler - snapshot
 * @throws APIException
 */
@Test
public void testOrderChannelHandler2() throws APIException {
    final String jsonString = "[0,\"on\",[[6784335053,null,1514956504945000,\"tIOTUSD\",1514956505134,1514956505164,-24.175121,-24.175121,\"EXCHANGE STOP\",null,null,null,0,\"ACTIVE\",null,null,3.84,0,null,null,null,null,null,0,0,0], [67843353243,null,1514956234945000,\"tBTCUSD\",1514956505134,1514956505164,-24.175121,-24.175121,\"EXCHANGE STOP\",null,null,null,0,\"ACTIVE\",null,null,3.84,0,null,null,null,null,null,0,0,0]]]";
    final JSONArray jsonArray = new JSONArray(jsonString);
    final OrderHandler orderHandler = new OrderHandler();
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
    Assert.assertTrue(orderManager.getOrders().isEmpty());
    orderHandler.handleChannelData(bitfinexApiBroker, jsonArray);
    Assert.assertEquals(2, orderManager.getOrders().size());
    orderManager.clear();
    Assert.assertTrue(orderManager.getOrders().isEmpty());
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) OrderManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager) OrderHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler) Test(org.junit.Test)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)33 Test (org.junit.Test)33 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)25 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)20 JSONArray (org.json.JSONArray)20 CountDownLatch (java.util.concurrent.CountDownLatch)14 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)12 HashMap (java.util.HashMap)11 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)10 BitfinexOrder (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder)9 ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)9 Wallet (com.github.jnidzwetzki.bitfinex.v2.entity.Wallet)9 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)8 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)8 CurrencyEntry (com.github.jnidzwetzki.cryptobot.CurrencyEntry)8 BasePortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager)8 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)7 PortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager)7 ArrayList (java.util.ArrayList)7