Search in sources :

Example 1 with Position

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

the class PortfolioManager method cancelRemovedEntryOrders.

/**
 * Cancel the removed entry orders
 * Position is at the moment not interesting for an entry
 *
 * @param entries
 * @throws APIException
 * @throws InterruptedException
 */
private void cancelRemovedEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
    final List<ExchangeOrder> entryOrders = getAllOpenEntryOrders();
    for (final ExchangeOrder order : entryOrders) {
        final String symbol = order.getSymbol();
        final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
        if (!entries.containsKey(currencyPair)) {
            logger.info("Entry order for {} is not contained, canceling", currencyPair);
            cancelOrder(order);
        }
    }
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 2 with Position

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

the class BitfinexApiBroker method handleChannelDataArray.

/**
 * Handle the channel data with has an array at first position
 * @param jsonArray
 * @param channelSymbol
 * @throws APIException
 */
private void handleChannelDataArray(final JSONArray jsonArray, final BitfinexStreamSymbol channelSymbol) throws APIException {
    final JSONArray subarray = jsonArray.getJSONArray(1);
    if (channelSymbol instanceof BitfinexCandlestickSymbol) {
        final ChannelCallbackHandler handler = new CandlestickHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof RawOrderbookConfiguration) {
        final RawOrderbookHandler handler = new RawOrderbookHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof OrderbookConfiguration) {
        final OrderbookHandler handler = new OrderbookHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof BitfinexTickerSymbol) {
        final ChannelCallbackHandler handler = new TickHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof BitfinexExecutedTradeSymbol) {
        final ChannelCallbackHandler handler = new ExecutedTradeHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else {
        logger.error("Unknown stream type: {}", channelSymbol);
    }
}
Also used : CandlestickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler) RawOrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) ChannelCallbackHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ChannelCallbackHandler) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) JSONArray(org.json.JSONArray) RawOrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler) OrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.OrderbookHandler) ExecutedTradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) TickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)

Example 3 with Position

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

the class MarginPortfolioManager method getOpenPositionSizeForCurrency.

/**
 * Get the position size for the symbol
 * @param symbol
 * @return
 * @throws APIException
 */
@Override
protected double getOpenPositionSizeForCurrency(final String currency) throws APIException {
    final List<Position> positions = bitfinexApiBroker.getPositionManager().getPositions();
    final Position position = positions.stream().filter(p -> p.getCurreny().getCurrency1().equals(currency)).findAny().orElse(null);
    if (position == null) {
        return 0;
    }
    return position.getAmount();
}
Also used : Position(com.github.jnidzwetzki.bitfinex.v2.entity.Position)

Example 4 with Position

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

the class PortfolioManager method calculatePositionSizes.

/**
 * Calculate the position sizes
 * @param entries
 * @throws APIException
 */
@VisibleForTesting
public void calculatePositionSizes(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException {
    final Wallet wallet = getWalletForCurrency("USD");
    // Wallet could be empty
    if (wallet == null) {
        throw new APIException("Unable to find USD wallet");
    }
    final double capitalAvailable = getAvailablePortfolioValueInUSD() * getInvestmentRate();
    double capitalNeeded = 0;
    for (final BitfinexCurrencyPair currency : entries.keySet()) {
        final CurrencyEntry entry = entries.get(currency);
        final double positionSize = calculatePositionSize(entry);
        entry.setPositionSize(positionSize);
        capitalNeeded = capitalNeeded + (positionSize * entry.getEntryPrice());
    }
    // Need the n% risk per position more than the available capital
    if (capitalNeeded > capitalAvailable) {
        final double investmentCorrectionFactor = capitalAvailable / capitalNeeded;
        logger.info("Needed capital {}, available capital {} ({})", capitalNeeded, capitalAvailable, investmentCorrectionFactor);
        capitalNeeded = 0;
        for (final BitfinexCurrencyPair currency : entries.keySet()) {
            final CurrencyEntry entry = entries.get(currency);
            final double newPositionSize = roundPositionSize(entry.getPositionSize() * investmentCorrectionFactor);
            entry.setPositionSize(newPositionSize);
            capitalNeeded = capitalNeeded + (entry.getPositionSize() * entry.getEntryPrice());
        }
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) CurrencyEntry(com.github.jnidzwetzki.cryptobot.CurrencyEntry) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with Position

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

the class PortfolioManager method placeNewEntryOrders.

/**
 * Place the new entry orders
 * @param entries
 * @throws APIException
 * @throws InterruptedException
 */
private void placeNewEntryOrders(final Map<BitfinexCurrencyPair, CurrencyEntry> entries) throws APIException, InterruptedException {
    // Check current limits and position sizes
    for (final BitfinexCurrencyPair currency : entries.keySet()) {
        final ExchangeOrder order = getOpenOrderForSymbol(currency.toBitfinexString());
        final CurrencyEntry entry = entries.get(currency);
        final double entryPrice = entry.getEntryPrice();
        final double positionSize = entry.getPositionSize();
        if (positionSize < currency.getMinimumOrderSize()) {
            logger.info("Not placing order for {}, position size is too small {}", currency, positionSize);
            continue;
        }
        // Old order present
        if (order != null) {
            if (hasEntryOrderChanged(order, entryPrice, positionSize)) {
                logger.info("Entry order for {}, values changed (amount: {} / {}} (price: {} / {})", currency, order.getAmount(), positionSize, order.getPrice(), entryPrice);
                cancelOrder(order);
            } else {
                logger.info("Not placing a new order for {}, old order still active", currency);
                continue;
            }
        }
        final BitfinexOrder newOrder = BitfinexOrderBuilder.create(currency, getOrderType(), positionSize).withPrice(entryPrice).setPostOnly().build();
        placeOrder(newOrder);
    }
}
Also used : BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) CurrencyEntry(com.github.jnidzwetzki.cryptobot.CurrencyEntry) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Aggregations

BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)4 PositionHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.PositionHandler)3 JSONArray (org.json.JSONArray)3 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)2 ExecutedTradeHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler)2 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)2 Position (com.github.jnidzwetzki.bitfinex.v2.entity.Position)2 CurrencyEntry (com.github.jnidzwetzki.cryptobot.CurrencyEntry)2 Test (org.junit.Test)2 DoNothingHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.DoNothingHandler)1 HeartbeatHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.HeartbeatHandler)1 NotificationHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.NotificationHandler)1 OrderHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.OrderHandler)1 TradeHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.TradeHandler)1 WalletHandler (com.github.jnidzwetzki.bitfinex.v2.callback.api.WalletHandler)1 CandlestickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler)1 ChannelCallbackHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.ChannelCallbackHandler)1 OrderbookHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.OrderbookHandler)1 RawOrderbookHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler)1 TickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)1