Search in sources :

Example 26 with BitfinexCurrencyPair

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

the class EMABot method addTradeToOpenTradeList.

/**
 * Open the trade to the open trades list
 * @param currency
 * @param trade
 */
private void addTradeToOpenTradeList(final Trade trade) {
    final BitfinexCurrencyPair currency = trade.getSymbol();
    if (!trades.containsKey(currency)) {
        trades.put(currency, new ArrayList<>());
    }
    trades.get(currency).add(trade);
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

Example 27 with BitfinexCurrencyPair

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

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

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

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

the class BarMegerTest method testBarMergerMinMax.

/**
 * Test Bar merger min max
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 6000)
public void testBarMergerMinMax() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(1);
    final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
        Assert.assertEquals(1.0, t.getMinPrice().doubleValue(), DELTA);
        Assert.assertEquals(8.0, t.getMaxPrice().doubleValue(), DELTA);
        Assert.assertEquals(3.0, t.getOpenPrice().doubleValue(), DELTA);
        Assert.assertEquals(4.5, t.getClosePrice().doubleValue(), DELTA);
        latch.countDown();
    };
    final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
    BarMerger.addNewPrice(parser.parse("01:01:01").getTime(), 3.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:02").getTime(), 2.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:03").getTime(), 8.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:04").getTime(), 1.5, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:05").getTime(), 2.5, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:06").getTime(), 1.0, 5.0);
    BarMerger.addNewPrice(parser.parse("01:01:07").getTime(), 4.5, 5.0);
    BarMerger.close();
    latch.await();
}
Also used : BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BiConsumer(java.util.function.BiConsumer) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Test(org.junit.Test) ParseException(java.text.ParseException) Assert(org.junit.Assert) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) Bar(org.ta4j.core.Bar) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)35 Test (org.junit.Test)21 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 Bar (org.ta4j.core.Bar)17 BarMerger (com.github.jnidzwetzki.cryptobot.util.BarMerger)16 BiConsumer (java.util.function.BiConsumer)15 IOException (java.io.IOException)14 ParseException (java.text.ParseException)14 SimpleDateFormat (java.text.SimpleDateFormat)14 Assert (org.junit.Assert)14 HashMap (java.util.HashMap)10 CurrencyEntry (com.github.jnidzwetzki.cryptobot.CurrencyEntry)8 BasePortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager)7 PortfolioManager (com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager)7 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)6 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)6 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)5 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)4 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)4