Search in sources :

Example 31 with BitfinexCurrencyPair

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

the class BarMegerTest method testBarMerger3.

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

Example 32 with BitfinexCurrencyPair

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

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

Example 34 with BitfinexCurrencyPair

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

the class TestCapitalAllocation method testCapitalAllocationExchange3.

@Test
public void testCapitalAllocationExchange3() 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);
    final CurrencyEntry entry3 = new CurrencyEntry(BitfinexCurrencyPair.XRP_USD, 1000, 990);
    entries.put(BitfinexCurrencyPair.XRP_USD, entry3);
    portfolioManager.calculatePositionSizes(entries);
    // Max loss = 10, max capital allocation 50%
    Assert.assertEquals(0.3, entry1.getPositionSize(), DELTA);
    Assert.assertEquals(0.3, entry2.getPositionSize(), DELTA);
    Assert.assertEquals(0.3, 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)

Example 35 with BitfinexCurrencyPair

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

the class PositionHandler method handlePositionCallback.

/**
 * Handle a position update
 * @param bitfinexApiBroker
 * @param positions
 */
private void handlePositionCallback(final BitfinexApiBroker bitfinexApiBroker, final JSONArray positions) {
    final String currencyString = positions.getString(0);
    BitfinexCurrencyPair currency = BitfinexCurrencyPair.fromSymbolString(currencyString);
    final Position position = new Position(currency);
    position.setStatus(positions.getString(1));
    position.setAmount(positions.getBigDecimal(2));
    position.setBasePrice(positions.getBigDecimal(3));
    position.setMarginFunding(positions.getBigDecimal(4));
    position.setMarginFundingType(positions.getBigDecimal(5));
    position.setPl(positions.optBigDecimal(6, BigDecimal.valueOf(-1)));
    position.setPlPercent(positions.optBigDecimal(7, BigDecimal.valueOf(-1)));
    position.setPriceLiquidation(positions.optBigDecimal(8, BigDecimal.valueOf(-1)));
    position.setLeverage(positions.optBigDecimal(9, BigDecimal.valueOf(-1)));
    bitfinexApiBroker.getPositionManager().updatePosition(position);
}
Also used : Position(com.github.jnidzwetzki.bitfinex.v2.entity.Position) 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