Search in sources :

Example 41 with APIException

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

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

Example 43 with APIException

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

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

the class TestCapitalAllocation method testCapitalAllocationExchangeMaxPositionLoss2.

@Test
public void testCapitalAllocationExchangeMaxPositionLoss2() throws APIException {
    final PortfolioManager portfolioManager = buildPortfolioManager();
    final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
    final CurrencyEntry entry1 = new CurrencyEntry(BitfinexCurrencyPair.BTC_USD, 1000, 0);
    entries.put(BitfinexCurrencyPair.BTC_USD, entry1);
    final CurrencyEntry entry2 = new CurrencyEntry(BitfinexCurrencyPair.IOT_USD, 1000, 990);
    entries.put(BitfinexCurrencyPair.IOT_USD, entry2);
    portfolioManager.calculatePositionSizes(entries);
    // Max loss = 10, max capital allocation 50%
    Assert.assertEquals(0.045, entry1.getPositionSize(), DELTA);
    Assert.assertEquals(0.45, entry2.getPositionSize(), DELTA);
}
Also used : HashMap(java.util.HashMap) BasePortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager) PortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager) CurrencyEntry(com.github.jnidzwetzki.cryptobot.CurrencyEntry) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) Test(org.junit.Test)

Example 45 with APIException

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

the class TestCapitalAllocation method testCapitalAllocationExchangeMaxPositionLoss3.

@Test
public void testCapitalAllocationExchangeMaxPositionLoss3() throws APIException {
    final PortfolioManager portfolioManager = buildPortfolioManager();
    final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
    final CurrencyEntry entry1 = new CurrencyEntry(BitfinexCurrencyPair.BTC_USD, 1000, 0);
    entries.put(BitfinexCurrencyPair.BTC_USD, entry1);
    final CurrencyEntry entry2 = new CurrencyEntry(BitfinexCurrencyPair.IOT_USD, 1000, 990);
    entries.put(BitfinexCurrencyPair.IOT_USD, entry2);
    final CurrencyEntry entry3 = new CurrencyEntry(BitfinexCurrencyPair.XRP_USD, 1000, 500);
    entries.put(BitfinexCurrencyPair.XRP_USD, entry3);
    portfolioManager.calculatePositionSizes(entries);
    // Max loss = 10, max capital allocation 50%
    Assert.assertEquals(0.045, entry1.getPositionSize(), DELTA);
    Assert.assertEquals(0.45, entry2.getPositionSize(), DELTA);
    Assert.assertEquals(0.09, entry3.getPositionSize(), DELTA);
}
Also used : HashMap(java.util.HashMap) BasePortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager) PortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager) CurrencyEntry(com.github.jnidzwetzki.cryptobot.CurrencyEntry) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) 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