Search in sources :

Example 6 with APIException

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

the class TestCapitalAllocation method testCapitalAllocationExchange1.

@Test
public void testCapitalAllocationExchange1() throws APIException {
    final PortfolioManager portfolioManager = buildPortfolioManager();
    final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
    final CurrencyEntry entry1 = new CurrencyEntry(BitfinexCurrencyPair.BTC_USD, 1000, 990);
    entries.put(BitfinexCurrencyPair.BTC_USD, entry1);
    portfolioManager.calculatePositionSizes(entries);
    // Max loss = 10, max capital allocation 50%
    Assert.assertEquals(0.45, entry1.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 7 with APIException

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

the class TestCapitalAllocation method testCapitalAllocationExchangeMaxPositionLoss1.

@Test
public void testCapitalAllocationExchangeMaxPositionLoss1() 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);
    portfolioManager.calculatePositionSizes(entries);
    // Max loss = 10, max capital allocation 50%
    Assert.assertEquals(0.045, entry1.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 8 with APIException

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

the class TestCapitalAllocation method testCapitalAllocationExchange2.

@Test
public void testCapitalAllocationExchange2() throws APIException {
    final PortfolioManager portfolioManager = buildPortfolioManager();
    final Map<BitfinexCurrencyPair, CurrencyEntry> entries = new HashMap<>();
    final CurrencyEntry entry1 = new CurrencyEntry(BitfinexCurrencyPair.BTC_USD, 1000, 990);
    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.45, 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 9 with APIException

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

the class EMABot method updateScreen.

public synchronized void updateScreen() {
    if (!UPDATE_SCREEN) {
        return;
    }
    final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
    CliTools.clearScreen();
    System.out.println("");
    System.out.println("==========");
    System.out.println("Last ticks");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
        System.out.println(symbol + " " + tickerManager.getLastTick(tickerSymbol));
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("Last bars");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        System.out.println(currency + " " + timeSeries.get(currency).getLastBar());
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("P/L");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
        final Trade trade = getOpenTrade(currency);
        if (trade != null) {
            final double priceIn = trade.getExpectedPriceOpen();
            final double currentPrice = tickerManager.getLastTick(tickerSymbol).getClose();
            System.out.println(symbol + ": price in " + priceIn + " / " + (currentPrice - priceIn));
        }
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("Trades");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final List<Trade> lastTrades = trades.get(currency);
        if (lastTrades == null) {
            continue;
        }
        lastTrades.sort((t1, t2) -> Long.compare(t2.getTid(), t1.getTid()));
        final List<Trade> lastTwoTrades = lastTrades.subList(Math.max(lastTrades.size() - 2, 0), lastTrades.size());
        for (final Trade trade : lastTwoTrades) {
            System.out.println(symbol + " " + trade);
        }
    }
    try {
        System.out.println("");
        System.out.println("==========");
        System.out.println("Orders");
        System.out.println("==========");
        final List<ExchangeOrder> orders = new ArrayList<>(bitfinexApiBroker.getOrderManager().getOrders());
        orders.sort((o1, o2) -> Long.compare(o2.getCid(), o1.getCid()));
        final List<ExchangeOrder> lastOrders = orders.subList(Math.max(orders.size() - 3, 0), orders.size());
        for (final ExchangeOrder order : lastOrders) {
            System.out.println(order);
        }
    } catch (APIException e) {
        logger.error("Got eception while reading wallets", e);
    }
}
Also used : Trade(com.github.jnidzwetzki.cryptobot.entity.Trade) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) ArrayList(java.util.ArrayList) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 10 with APIException

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

the class EMABot method registerTicker.

protected void registerTicker(final BitfinexApiBroker bitfinexApiBroker) throws InterruptedException, APIException {
    logger.info("Register ticker");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        tickMerger.put(currency, new BarMerger(currency, TIMEFRAME, (s, t) -> barDoneCallback(s, t)));
        final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
        bitfinexApiBroker.getQuoteManager().subscribeTicker(symbol);
        logger.info("Wait for ticker");
        while (!bitfinexApiBroker.isTickerActive(symbol)) {
            Thread.sleep(100);
        }
        bitfinexApiBroker.getQuoteManager().registerTickCallback(symbol, (s, c) -> handleTickCallback(s, c));
    }
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) Arrays(java.util.Arrays) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) Decimal(org.ta4j.core.Decimal) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) OrderType(org.ta4j.core.Order.OrderType) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) EMAStrategy03(com.github.jnidzwetzki.cryptobot.strategy.EMAStrategy03) ArrayList(java.util.ArrayList) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) Map(java.util.Map) BitfinexClientFactory(com.github.jnidzwetzki.cryptobot.util.BitfinexClientFactory) Strategy(org.ta4j.core.Strategy) TradeState(com.github.jnidzwetzki.cryptobot.entity.TradeState) TradeStrategyFactory(com.github.jnidzwetzki.cryptobot.strategy.TradeStrategyFactory) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) Logger(org.slf4j.Logger) TradeDirection(com.github.jnidzwetzki.cryptobot.entity.TradeDirection) Collectors(java.util.stream.Collectors) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) TimeUnit(java.util.concurrent.TimeUnit) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) List(java.util.List) Trade(com.github.jnidzwetzki.cryptobot.entity.Trade) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)

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