Search in sources :

Example 11 with BitfinexTickerSymbol

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

the class BasePortfolioManager method getTotalPortfolioValueInUSD.

@Override
protected double getTotalPortfolioValueInUSD() throws APIException {
    final List<Wallet> wallets = getAllWallets();
    double totalValue = 0;
    for (final Wallet wallet : wallets) {
        final String curreny = wallet.getCurreny();
        if (curreny.equals("USD")) {
            totalValue = totalValue + wallet.getBalance();
        } else {
            final String symbol = "t" + curreny + "USD";
            try {
                final BitfinexCurrencyPair bitfinexCurrencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
                final BitfinexTickerSymbol bitfinexSymbol = new BitfinexTickerSymbol(bitfinexCurrencyPair);
                final BitfinexTick lastTick = bitfinexApiBroker.getQuoteManager().getLastTick(bitfinexSymbol);
                if (lastTick != null) {
                    final double rate = lastTick.getClose();
                    final double value = rate * wallet.getBalance();
                    totalValue = totalValue + value;
                } else {
                    logger.debug("Unable to find tick for {}, appraise wallet with 0 USD", symbol);
                }
            } catch (IllegalArgumentException e) {
                logger.debug("Unkown symbol {}, ignoring wallet", symbol);
            }
        }
    }
    return totalValue;
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)

Example 12 with BitfinexTickerSymbol

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

the class CommandsTest method testCommandsJSON.

/**
 * Call all commands and check for excepion
 * @throws CommandException
 */
@Test
public void testCommandsJSON() throws CommandException {
    final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.EXCHANGE_STOP, 2).build();
    final BitfinexCandlestickSymbol candleSymbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BCH_USD, Timeframe.HOUR_1);
    final OrderbookConfiguration orderbookConfiguration = new OrderbookConfiguration(BitfinexCurrencyPair.BCH_USD, OrderBookPrecision.P0, OrderBookFrequency.F0, 50);
    final RawOrderbookConfiguration rawOrderbookConfiguration = new RawOrderbookConfiguration(BitfinexCurrencyPair.BAT_BTC);
    final List<AbstractAPICommand> commands = Arrays.asList(new AuthCommand(), new CancelOrderCommand(123), new CancelOrderGroupCommand(1), new OrderCommand(order), new PingCommand(), new SubscribeCandlesCommand(candleSymbol), new SubscribeTickerCommand(new BitfinexTickerSymbol(BitfinexCurrencyPair.BCH_USD)), new SubscribeTradesCommand(new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BAT_BTC)), new SubscribeOrderbookCommand(orderbookConfiguration), new SubscribeRawOrderbookCommand(rawOrderbookConfiguration), new UnsubscribeChannelCommand(12));
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    for (final AbstractAPICommand command : commands) {
        final String commandValue = command.getCommand(bitfinexApiBroker);
        Assert.assertNotNull(commandValue);
        Assert.assertTrue(commandValue.length() > 10);
    }
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) OrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand) SubscribeOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand) CancelOrderGroupCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) UnsubscribeChannelCommand(com.github.jnidzwetzki.bitfinex.v2.commands.UnsubscribeChannelCommand) SubscribeTradesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) AbstractAPICommand(com.github.jnidzwetzki.bitfinex.v2.commands.AbstractAPICommand) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) PingCommand(com.github.jnidzwetzki.bitfinex.v2.commands.PingCommand) SubscribeCandlesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) SubscribeTickerCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) AuthCommand(com.github.jnidzwetzki.bitfinex.v2.commands.AuthCommand) SubscribeRawOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand) Test(org.junit.Test)

Example 13 with BitfinexTickerSymbol

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

the class IntegrationTest method testReconnect.

/**
 * Test the session reconnect
 * @throws APIException
 * @throws InterruptedException
 */
@Test
public void testReconnect() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    bitfinexClient.connect();
    final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
    final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
    orderbookManager.subscribeTicker(symbol);
    Thread.sleep(1000);
    bitfinexClient.reconnect();
    // Await at least 2 callbacks
    final CountDownLatch latch = new CountDownLatch(2);
    final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
        latch.countDown();
    };
    orderbookManager.registerTickCallback(symbol, callback);
    latch.await();
    Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
    orderbookManager.unsubscribeTicker(symbol);
    Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
    bitfinexClient.close();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) OrderBookPrecision(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision) Test(org.junit.Test) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) OrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager) OrderBookFrequency(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BiConsumer(java.util.function.BiConsumer) ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) Assert(org.junit.Assert) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 14 with BitfinexTickerSymbol

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

the class QuoteManager method subscribeTicker.

/**
 * Subscribe a ticker
 * @param tickerSymbol
 */
public void subscribeTicker(final BitfinexTickerSymbol tickerSymbol) {
    final SubscribeTickerCommand command = new SubscribeTickerCommand(tickerSymbol);
    bitfinexApiBroker.sendCommand(command);
}
Also used : SubscribeTickerCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand)

Example 15 with BitfinexTickerSymbol

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

the class CommandsCallbackTest method testSubscribeAndUnsubscribeCallback.

/**
 * Test the subscribed callback
 * @throws APIException
 */
@Test
public void testSubscribeAndUnsubscribeCallback() throws APIException {
    final String jsonString = "{\"event\":\"subscribed\",\"channel\":\"ticker\",\"chanId\":30,\"symbol\":\"tNEOUSD\",\"pair\":\"NEOUSD\"}";
    final JSONObject jsonObject = new JSONObject(jsonString);
    final BitfinexApiBroker bitfinexApiBroker = new BitfinexApiBroker();
    Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) == null);
    final SubscribedCallback subscribedCallback = new SubscribedCallback();
    subscribedCallback.handleChannelData(bitfinexApiBroker, jsonObject);
    Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) instanceof BitfinexTickerSymbol);
    final String unsubscribedJsonString = "{\"event\":\"unsubscribed\",\"status\":\"OK\",\"chanId\":30}";
    final JSONObject jsonUnsubscribedObject = new JSONObject(unsubscribedJsonString);
    final UnsubscribedCallback unsubscribedCallback = new UnsubscribedCallback();
    unsubscribedCallback.handleChannelData(bitfinexApiBroker, jsonUnsubscribedObject);
    Assert.assertTrue(bitfinexApiBroker.getFromChannelSymbolMap(30) == null);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONObject(org.json.JSONObject) SubscribedCallback(com.github.jnidzwetzki.bitfinex.v2.callback.command.SubscribedCallback) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) UnsubscribedCallback(com.github.jnidzwetzki.bitfinex.v2.callback.command.UnsubscribedCallback) Test(org.junit.Test)

Aggregations

BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)13 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)7 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)7 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)7 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)6 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)5 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)5 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)5 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)5 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)5 Test (org.junit.Test)5 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 SubscribeTickerCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand)3 TickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)2 SubscribeCandlesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand)2 SubscribeOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand)2 SubscribeRawOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)2 SubscribeTradesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand)2 UnsubscribeChannelCommand (com.github.jnidzwetzki.bitfinex.v2.commands.UnsubscribeChannelCommand)2