Search in sources :

Example 31 with BitfinexApiBroker

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

the class SubscribedCallback method handleBookCallback.

/**
 * Handle the book callback
 *
 * @param bitfinexApiBroker
 * @param jsonObject
 * @param channelId
 */
private void handleBookCallback(final BitfinexApiBroker bitfinexApiBroker, final JSONObject jsonObject, final int channelId) {
    if ("R0".equals(jsonObject.getString("prec"))) {
        final RawOrderbookConfiguration configuration = RawOrderbookConfiguration.fromJSON(jsonObject);
        logger.info("Registering raw book {} on channel {}", jsonObject, channelId);
        bitfinexApiBroker.addToChannelSymbolMap(channelId, configuration);
    } else {
        final OrderbookConfiguration configuration = OrderbookConfiguration.fromJSON(jsonObject);
        logger.info("Registering book {} on channel {}", jsonObject, channelId);
        bitfinexApiBroker.addToChannelSymbolMap(channelId, configuration);
    }
}
Also used : RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)

Example 32 with BitfinexApiBroker

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

the class UnsubscribedCallback method handleChannelData.

@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final JSONObject jsonObject) throws APIException {
    final int channelId = jsonObject.getInt("chanId");
    final BitfinexStreamSymbol symbol = bitfinexApiBroker.getFromChannelSymbolMap(channelId);
    logger.info("Channel {} ({}) is unsubscribed", channelId, symbol);
    bitfinexApiBroker.removeChannel(channelId);
}
Also used : BitfinexStreamSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexStreamSymbol)

Example 33 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker 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 34 with BitfinexApiBroker

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

the class TestCapitalAllocation method buildPortfolioManager.

/**
 * Build the portfolio manager
 * @return
 * @throws APIException
 */
private PortfolioManager buildPortfolioManager() throws APIException {
    final Collection<Wallet> wallets = new ArrayList<>();
    wallets.add(new Wallet(Wallet.WALLET_TYPE_EXCHANGE, "USD", 1000, 0, 1000));
    final BitfinexApiBroker apiBroker = Mockito.mock(BitfinexApiBroker.class);
    final WalletManager walletManager = Mockito.mock(WalletManager.class);
    Mockito.when(walletManager.getWallets()).thenReturn(wallets);
    Mockito.when(apiBroker.getWalletManager()).thenReturn(walletManager);
    return new BasePortfolioManager(apiBroker, 0.05);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) WalletManager(com.github.jnidzwetzki.bitfinex.v2.manager.WalletManager) BasePortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) ArrayList(java.util.ArrayList)

Example 35 with BitfinexApiBroker

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

the class CommandsTest method testOrderCommand.

/**
 * Test the order command
 * @throws CommandException
 */
@Test
public void testOrderCommand() throws CommandException {
    final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.EXCHANGE_STOP, 2).setHidden().setPostOnly().withPrice(12).withPriceAuxLimit(23).withPriceTrailing(23).withGroupId(4).build();
    final OrderCommand command = new OrderCommand(order);
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    final String commandValue = command.getCommand(bitfinexApiBroker);
    Assert.assertNotNull(commandValue);
    Assert.assertTrue(commandValue.length() > 10);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) OrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) Test(org.junit.Test)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)46 Test (org.junit.Test)36 JSONArray (org.json.JSONArray)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)15 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)15 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)13 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)12 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)12 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)11 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)11 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)9 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)9 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)9 ExecutorService (java.util.concurrent.ExecutorService)9 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)7 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)7 Assert (org.junit.Assert)7 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)6