Search in sources :

Example 11 with BitfinexCurrencyPair

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

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

the class BarMegerTest method testTickMerger4.

/**
 * Test three tick merge with other timestamps
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 6000)
public void testTickMerger4() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(2);
    final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
        latch.countDown();
    };
    final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
    tickMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:33").getTime(), 2.0, 5.0);
    tickMerger.addNewPrice(parser.parse("02:02:53").getTime(), 2.0, 5.0);
    tickMerger.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)

Example 13 with BitfinexCurrencyPair

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

the class BarMegerTest method testTickMerger3.

/**
 * Test three tick merge
 * @throws InterruptedException
 * @throws IOException
 * @throws ParseException
 */
@Test(timeout = 6000)
public void testTickMerger3() throws InterruptedException, IOException, ParseException {
    final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
    final CountDownLatch latch = new CountDownLatch(2);
    final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
        latch.countDown();
    };
    final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
    tickMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
    tickMerger.addNewPrice(parser.parse("01:01:33").getTime(), 2.0, 5.0);
    tickMerger.addNewPrice(parser.parse("01:02:53").getTime(), 2.0, 5.0);
    tickMerger.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)

Example 14 with BitfinexCurrencyPair

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

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

the class EMABot method createStrategies.

/**
 * Init the trading stretegies
 */
private void createStrategies() {
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        TradeStrategyFactory strategyFactory = new EMAStrategy03(5, 12, 40, timeSeries.get(currency));
        final Strategy strategy = strategyFactory.getStrategy();
        strategies.put(currency, strategy);
    }
}
Also used : EMAStrategy03(com.github.jnidzwetzki.cryptobot.strategy.EMAStrategy03) Strategy(org.ta4j.core.Strategy) TradeStrategyFactory(com.github.jnidzwetzki.cryptobot.strategy.TradeStrategyFactory) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

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