Search in sources :

Example 31 with APIException

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

the class RawOrderbookHandler method handleChannelData.

@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    final RawOrderbookConfiguration configuration = (RawOrderbookConfiguration) channelSymbol;
    // Example: [13182,1,-0.1]
    try {
        // Snapshots contain multiple Orderbook entries, updates only one
        if (jsonArray.get(0) instanceof JSONArray) {
            for (int pos = 0; pos < jsonArray.length(); pos++) {
                final JSONArray parts = jsonArray.getJSONArray(pos);
                handleEntry(bitfinexApiBroker, configuration, parts);
            }
        } else {
            handleEntry(bitfinexApiBroker, configuration, jsonArray);
        }
    } catch (JSONException e) {
        throw new APIException(e);
    }
}
Also used : RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 32 with APIException

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

the class TickHandler method handleChannelData.

/**
 * Handle a tick callback
 * @param channel
 * @param subarray
 */
@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    final BitfinexTickerSymbol currencyPair = (BitfinexTickerSymbol) channelSymbol;
    // 0 = BID
    // 2 = ASK
    // 6 = Price
    final BigDecimal price = jsonArray.getBigDecimal(6);
    // Volume is set to 0, because the ticker contains only the daily volume
    final BitfinexTick tick = new BitfinexTick(System.currentTimeMillis(), price, price, price, price);
    bitfinexApiBroker.getQuoteManager().handleNewTick(currencyPair, tick);
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) BigDecimal(java.math.BigDecimal)

Example 33 with APIException

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

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

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

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