diff --git a/xchange-stream-bybit/src/main/java/dto/trade/BybitComplexPositionChanges.java b/xchange-stream-bybit/src/main/java/dto/trade/BybitComplexPositionChanges.java index 2081300805f..7c8d5d08f89 100644 --- a/xchange-stream-bybit/src/main/java/dto/trade/BybitComplexPositionChanges.java +++ b/xchange-stream-bybit/src/main/java/dto/trade/BybitComplexPositionChanges.java @@ -3,35 +3,46 @@ import java.math.BigDecimal; import lombok.Getter; import lombok.Setter; +import lombok.ToString; +import org.knowm.xchange.bybit.dto.BybitCategory; import org.knowm.xchange.dto.account.OpenPosition; import org.knowm.xchange.instrument.Instrument; @Getter @Setter +@ToString public class BybitComplexPositionChanges extends OpenPosition { + + private int positionIdx; + private int tradeMode; + private int riskId; + private String riskLimitValue; + private BigDecimal markPrice; + private BigDecimal positionBalance; + private int autoAddMargin; + private BigDecimal positionMM; + private BigDecimal positionIM; + private BigDecimal bustPrice; private BigDecimal positionValue; private BigDecimal leverage; private BigDecimal takeProfit; private BigDecimal stopLoss; + private BigDecimal trailingStop; private BigDecimal curRealisedPnl; + private BigDecimal sessionAvgPrice; //USDC contract session avg price + private String positionStatus; + private int adlRankIndicator; + private boolean isReduceOnly; + private String mmrSysUpdatedTime; + private String leverageSysUpdatedTime; private long createdTime; private long updatedTime; private long seq; - public BybitComplexPositionChanges( - Instrument instrument, - Type type, - BigDecimal size, - BigDecimal liquidationPrice, - BigDecimal unRealisedPnl, - BigDecimal positionValue, - BigDecimal entryPrice, - BigDecimal leverage, - BigDecimal takeProfit, - BigDecimal stopLoss, - BigDecimal curRealisedPnl, - long createdTime, - long updatedTime, + public BybitComplexPositionChanges(Instrument instrument, Type type, BigDecimal size, + BigDecimal liquidationPrice, BigDecimal unRealisedPnl, + BigDecimal positionValue, BigDecimal entryPrice, BigDecimal leverage, BigDecimal takeProfit, + BigDecimal stopLoss, BigDecimal curRealisedPnl, long createdTime, long updatedTime, long seq) { super(instrument, type, size, entryPrice, liquidationPrice, unRealisedPnl); this.positionValue = positionValue; @@ -44,31 +55,77 @@ public BybitComplexPositionChanges( this.seq = seq; } - public BybitComplexPositionChanges( - Instrument instrument, - Type type, - BigDecimal size, - BigDecimal price, - BigDecimal liquidationPrice, + public BybitComplexPositionChanges(Instrument instrument, Type type, BigDecimal size, + BigDecimal price, BigDecimal liquidationPrice, BigDecimal unRealisedPnl) { super(instrument, type, size, price, liquidationPrice, unRealisedPnl); } public BybitComplexPositionChanges(BybitComplexPositionChanges changes) { - super( - changes.getInstrument(), - changes.getType(), - changes.getSize(), - changes.getPrice(), - changes.getLiquidationPrice(), - changes.getUnRealisedPnl()); + super(changes.getInstrument(), changes.getType(), changes.getSize(), changes.getPrice(), + changes.getLiquidationPrice(), changes.getUnRealisedPnl()); + this.positionIdx = changes.positionIdx; + this.tradeMode = changes.tradeMode; + this.riskId = changes.riskId; + this.riskLimitValue = changes.riskLimitValue; + this.markPrice = changes.markPrice; + this.positionBalance = changes.positionBalance; + this.autoAddMargin = changes.autoAddMargin; + this.positionMM = changes.positionMM; + this.positionIM = changes.positionIM; + this.bustPrice = changes.bustPrice; this.positionValue = changes.positionValue; this.leverage = changes.leverage; this.takeProfit = changes.takeProfit; this.stopLoss = changes.stopLoss; + this.trailingStop = changes.trailingStop; this.curRealisedPnl = changes.curRealisedPnl; + this.sessionAvgPrice = changes.sessionAvgPrice; + this.positionStatus = changes.positionStatus; + this.adlRankIndicator = changes.adlRankIndicator; + this.isReduceOnly = changes.isReduceOnly; + this.mmrSysUpdatedTime = changes.mmrSysUpdatedTime; + this.leverageSysUpdatedTime = changes.leverageSysUpdatedTime; this.createdTime = changes.createdTime; this.updatedTime = changes.updatedTime; this.seq = changes.seq; } + + public BybitComplexPositionChanges(Instrument instrument, Type type, BigDecimal size, + BigDecimal price, BigDecimal liquidationPrice, BigDecimal unRealisedPnl, int positionIdx, + int tradeMode, int riskId, String riskLimitValue, BigDecimal markPrice, + BigDecimal positionBalance, int autoAddMargin, BigDecimal positionMM, BigDecimal positionIM, + BigDecimal bustPrice, BigDecimal positionValue, BigDecimal leverage, + BigDecimal takeProfit, BigDecimal stopLoss, BigDecimal trailingStop, + BigDecimal curRealisedPnl, + BigDecimal sessionAvgPrice, String positionStatus, int adlRankIndicator, boolean isReduceOnly, + String mmrSysUpdatedTime, String leverageSysUpdatedTime, long createdTime, long updatedTime, + long seq) { + super(instrument, type, size, price, liquidationPrice, unRealisedPnl); + this.positionIdx = positionIdx; + this.tradeMode = tradeMode; + this.riskId = riskId; + this.riskLimitValue = riskLimitValue; + this.markPrice = markPrice; + this.positionBalance = positionBalance; + this.autoAddMargin = autoAddMargin; + this.positionMM = positionMM; + this.positionIM = positionIM; + this.bustPrice = bustPrice; + this.positionValue = positionValue; + this.leverage = leverage; + this.takeProfit = takeProfit; + this.stopLoss = stopLoss; + this.trailingStop = trailingStop; + this.curRealisedPnl = curRealisedPnl; + this.sessionAvgPrice = sessionAvgPrice; + this.positionStatus = positionStatus; + this.adlRankIndicator = adlRankIndicator; + this.isReduceOnly = isReduceOnly; + this.mmrSysUpdatedTime = mmrSysUpdatedTime; + this.leverageSysUpdatedTime = leverageSysUpdatedTime; + this.createdTime = createdTime; + this.updatedTime = updatedTime; + this.seq = seq; + } } diff --git a/xchange-stream-bybit/src/main/java/dto/trade/BybitPositionChangesResponse.java b/xchange-stream-bybit/src/main/java/dto/trade/BybitPositionChangesResponse.java index 437cb80149d..ff41479e413 100644 --- a/xchange-stream-bybit/src/main/java/dto/trade/BybitPositionChangesResponse.java +++ b/xchange-stream-bybit/src/main/java/dto/trade/BybitPositionChangesResponse.java @@ -5,57 +5,61 @@ import lombok.Getter; import org.knowm.xchange.bybit.dto.BybitCategory; + @Getter public class BybitPositionChangesResponse { - String id; - String topic; - long creationTime; - List data = new ArrayList<>(); + private String id; + private String topic; + private long creationTime; + private List data = new ArrayList<>(); @Getter public static class BybitPositionChanges { - BybitCategory category; - String symbol; - String side; - String size; - int positionIdx; - String tradeMode; - String positionValue; - int riskId; - String riskLimitValue; - String entryPrice; - String markPrice; - String leverage; - String positionBalance; - int autoAddMargin; - String positionMM; - String positionIM; - String liqPrice; - String bustPrice; - String tpslMode; - String takeProfit; - String stopLoss; - String trailingStop; - String unrealisedPnl; - String curRealisedPnl; - String sessionAvgPrice; - String delta; - String gamma; - String vega; - String theta; - String cumRealisedPnl; - String positionStatus; - int adlRankIndicator; - boolean isReduceOnly; - - String mmrSysUpdatedTime; - - String leverageSysUpdatedTime; - - String createdTime; - String updatedTime; - long seq; + private BybitCategory category; + private String symbol; + private String side; + private String size; + private int positionIdx; + private int tradeMode; + private String positionValue; + private int riskId; + private String riskLimitValue; + private String entryPrice; + private String markPrice; + private String leverage; + private String positionBalance; + private int autoAddMargin; + private String positionMM; + private String positionIM; + private String liqPrice; + private String bustPrice; + private String tpslMode; + private String takeProfit; + private String stopLoss; + private String trailingStop; + private String unrealisedPnl; + private String curRealisedPnl; + private String sessionAvgPrice; + private String delta; + private String gamma; + private String vega; + private String theta; + private String cumRealisedPnl; + private String positionStatus; + private int adlRankIndicator; + private boolean isReduceOnly; + + private String mmrSysUpdatedTime; + + private String leverageSysUpdatedTime; + + private String createdTime; + private String updatedTime; + private long seq; + } } + + diff --git a/xchange-stream-bybit/src/main/java/info/bitrich/xchangestream/bybit/BybitStreamAdapters.java b/xchange-stream-bybit/src/main/java/info/bitrich/xchangestream/bybit/BybitStreamAdapters.java index bd02f94de63..e0e3f894d37 100644 --- a/xchange-stream-bybit/src/main/java/info/bitrich/xchangestream/bybit/BybitStreamAdapters.java +++ b/xchange-stream-bybit/src/main/java/info/bitrich/xchangestream/bybit/BybitStreamAdapters.java @@ -159,22 +159,34 @@ public static List adaptComplexPositionChanges( if (!position.getLiqPrice().isEmpty()) { liqPrice = new BigDecimal(position.getLiqPrice()); } - BybitComplexPositionChanges positionChanges = - new BybitComplexPositionChanges( - guessSymbol(position.getSymbol(), position.getCategory()), - type, - new BigDecimal(position.getSize()), - liqPrice, - new BigDecimal(position.getUnrealisedPnl()), - new BigDecimal(position.getPositionValue()), - new BigDecimal(position.getEntryPrice()), - new BigDecimal(position.getLeverage()), - new BigDecimal(position.getTakeProfit()), - new BigDecimal(position.getStopLoss()), - new BigDecimal(position.getCurRealisedPnl()), - Long.parseLong(position.getCreatedTime()), - Long.parseLong(position.getUpdatedTime()), - position.getSeq()); + BigDecimal bustPrice = null; + if(!position.getBustPrice().isEmpty()) { + bustPrice = new BigDecimal(position.getBustPrice()); + } + BigDecimal sessionAvgPrice = null; + if (!position.getSessionAvgPrice().isEmpty()) { + sessionAvgPrice = new BigDecimal(position.getSessionAvgPrice()); + } + BybitComplexPositionChanges positionChanges = new BybitComplexPositionChanges( + guessSymbol(position.getSymbol(), + position.getCategory()), type, new BigDecimal(position.getSize()), + new BigDecimal(position.getEntryPrice()), liqPrice, + new BigDecimal(position.getUnrealisedPnl()), position.getPositionIdx(), + position.getTradeMode(), position.getRiskId(), position.getRiskLimitValue(), + new BigDecimal(position.getMarkPrice()), + new BigDecimal(position.getPositionBalance()), position.getAutoAddMargin(), + new BigDecimal(position.getPositionMM()), + new BigDecimal(position.getPositionIM()), + bustPrice, new BigDecimal(position.getPositionValue()), + new BigDecimal(position.getLeverage()), + new BigDecimal(position.getTakeProfit()), new BigDecimal(position.getStopLoss()), + new BigDecimal(position.getTrailingStop()), + new BigDecimal(position.getCurRealisedPnl()), + sessionAvgPrice, position.getPositionStatus(), + position.getAdlRankIndicator(), position.isReduceOnly(), + position.getMmrSysUpdatedTime(), position.getLeverageSysUpdatedTime(), + Long.parseLong(position.getCreatedTime()), Long.parseLong(position.getUpdatedTime()), + position.getSeq()); result.add(positionChanges); } return result; @@ -187,29 +199,17 @@ public static List adaptComplexOrdersChanges( Order.OrderType orderType = getOrderType(change.getSide()); BigDecimal avgPrice = change.getAvgPrice().isEmpty() ? null : new BigDecimal(change.getAvgPrice()); - BybitComplexOrderChanges orderChanges = - new BybitComplexOrderChanges( - orderType, - new BigDecimal(change.getQty()), - guessSymbol(change.getSymbol(), change.getCategory()), - change.getOrderId(), - new Date(Long.parseLong(change.getCreatedTime())), - avgPrice, - new BigDecimal(change.getCumExecQty()), - new BigDecimal(change.getCumExecFee()), - adaptBybitOrderStatus(change.getOrderStatus()), - change.getOrderLinkId(), - change.getCategory(), - new BigDecimal(change.getPrice()), - change.getSide(), - new BigDecimal(change.getLeavesQty()), - new BigDecimal(change.getLeavesValue()), - new BigDecimal(change.getCumExecValue()), - change.getFeeCurrency(), - TimeInForce.valueOf(change.getTimeInForce().toUpperCase()), - change.getOrderType(), - change.isReduceOnly(), - new Date(Long.parseLong(change.getUpdatedTime()))); + BybitComplexOrderChanges orderChanges = new BybitComplexOrderChanges(orderType, + new BigDecimal(change.getQty()), guessSymbol(change.getSymbol(), change.getCategory()), + change.getOrderId(), + new Date(Long.parseLong(change.getCreatedTime())), avgPrice, + new BigDecimal(change.getCumExecQty()), new BigDecimal(change.getCumExecFee()), + adaptBybitOrderStatus(change.getOrderStatus()), change.getOrderLinkId(), + change.getCategory(), new BigDecimal(change.getPrice()), change.getSide(), + new BigDecimal(change.getLeavesQty()), new BigDecimal(change.getLeavesValue()), + new BigDecimal(change.getCumExecValue()), change.getFeeCurrency(), + TimeInForce.valueOf(change.getTimeInForce().toUpperCase()), change.getOrderType(), + change.isReduceOnly(), new Date(Long.parseLong(change.getUpdatedTime()))); result.add(orderChanges); } return result; diff --git a/xchange-stream-bybit/src/test/java/info/bitrich/xchangestream/bybit/BybitStreamExample.java b/xchange-stream-bybit/src/test/java/info/bitrich/xchangestream/bybit/BybitStreamExample.java index a6cea09d71d..86ce5da51ce 100644 --- a/xchange-stream-bybit/src/test/java/info/bitrich/xchangestream/bybit/BybitStreamExample.java +++ b/xchange-stream-bybit/src/test/java/info/bitrich/xchangestream/bybit/BybitStreamExample.java @@ -23,6 +23,7 @@ import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.dto.marketdata.Ticker; import org.knowm.xchange.dto.trade.LimitOrder; +import org.knowm.xchange.dto.trade.MarketOrder; import org.knowm.xchange.instrument.Instrument; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,50 +62,50 @@ private static void auth() throws IOException, InterruptedException { StreamingExchangeFactory.INSTANCE.createExchange(exchangeSpecification); exchange.connect().blockingAwait(); Ticker ticker = (exchange.getMarketDataService().getTicker(DOGE_PERP)); - BigDecimal amount = - exchange.getExchangeMetaData().getInstruments().get(DOGE_PERP).getMinimumAmount(); + BigDecimal amount = exchange.getExchangeMetaData().getInstruments().get(DOGE_PERP) + .getMinimumAmount(); if (amount.multiply(ticker.getLast()).compareTo(new BigDecimal("5.0")) <= 0) { - amount = - new BigDecimal("5") - .divide( - ticker.getAsk(), - exchange.getExchangeMetaData().getInstruments().get(DOGE_PERP).getVolumeScale(), - UP); + amount = new BigDecimal("5").divide(ticker.getAsk(), + exchange.getExchangeMetaData().getInstruments().get(DOGE_PERP).getVolumeScale(), UP); } - LimitOrder limitOrder = - new LimitOrder(OrderType.BID, amount, DOGE_PERP, "", new Date(), ticker.getAsk()); + LimitOrder limitOrder = new LimitOrder(OrderType.BID, amount, DOGE_PERP, "", new Date(), + ticker.getAsk()); + MarketOrder marketOrder = new MarketOrder(OrderType.BID, amount, DOGE_PERP); Thread.sleep(2000L); AtomicReference order = new AtomicReference<>(); - Disposable disposableOrderChanges = - ((BybitStreamingTradeService) exchange.getStreamingTradeService()) - .getOrderChanges(BybitCategory.LINEAR) - .doOnError(error -> log.error("OrderChanges error {}", error.getMessage())) - .subscribe( - c -> { - log.info("Order Changes {}", c); - order.set(c); - }, - throwable -> log.error("OrderChanges throwable,{}", throwable.getMessage())); - // Disposable disposablePositionChanges = - // ((BybitStreamingTradeService)exchange.getStreamingTradeService()).getPositionChanges(BybitCategory.LINEAR) - // .doOnError( - // error -> log.error("PositionChanges error {}",error.getMessage())) - // .subscribe( p -> log.info("PositionChanges Changes {}", p), - // throwable -> log.error("Position throwable,{}",throwable.getMessage())); - - Disposable disposableComplexPositionChanges = - ((BybitStreamingTradeService) exchange.getStreamingTradeService()) - .getBybitPositionChanges(BybitCategory.LINEAR) - .doOnError(error -> log.error("ComplexPositionChanges error {}", error.getMessage())) - .subscribe( - p -> log.info("ComplexPositionChanges Changes {}", p), - throwable -> log.error("ComplexPosition throwable,{}", throwable.getMessage())); + Disposable disposableOrderChanges = ((BybitStreamingTradeService) exchange.getStreamingTradeService()).getOrderChanges( + BybitCategory.LINEAR) + .doOnError( + error -> log.error("OrderChanges error {}", error.getMessage())) + .subscribe(c -> { + log.info("Order Changes {}", c); + order.set(c); + }, + throwable -> log.error("OrderChanges throwable,{}", throwable.getMessage())); +// Disposable disposablePositionChanges = ((BybitStreamingTradeService)exchange.getStreamingTradeService()).getPositionChanges(BybitCategory.LINEAR) +// .doOnError( +// error -> log.error("PositionChanges error {}",error.getMessage())) +// .subscribe( p -> log.info("PositionChanges Changes {}", p), +// throwable -> log.error("Position throwable,{}",throwable.getMessage())); + Disposable disposableByBitPositionChanges = ((BybitStreamingTradeService) exchange.getStreamingTradeService()).getBybitPositionChanges( + BybitCategory.LINEAR) + .doOnError( + error -> log.error("PositionChanges error {}",error,error)) + .subscribe(p -> log.info("PositionChanges Changes {}", p), + throwable -> log.error("Position throwable,{}",throwable ,throwable)); + Disposable disposableComplexPositionChanges = ((BybitStreamingTradeService) exchange.getStreamingTradeService()).getBybitPositionChanges( + BybitCategory.LINEAR) + .doOnError( + error -> log.error("ComplexPositionChanges error {}", error.getMessage())) + .subscribe(p -> log.info("ComplexPositionChanges Changes {}", p), + throwable -> log.error("ComplexPosition throwable,{}", throwable.getMessage())); Thread.sleep(3000L); exchange.getTradeService().placeLimitOrder(limitOrder); + exchange.getTradeService().placeMarketOrder(marketOrder); Thread.sleep(30000L); disposableOrderChanges.dispose(); - // disposablePositionChanges.dispose(); + disposableByBitPositionChanges.dispose(); disposableComplexPositionChanges.dispose(); exchange.disconnect().blockingAwait(); } @@ -153,8 +154,14 @@ public static void spot() throws IOException { tradesDisposable.add( exchange .getStreamingMarketDataService() - .getTrades(BTC_SPOT) - .subscribe(trade -> System.out.println("trade: " + trade))); + .getOrderBook(ETH_SPOT) + .subscribe()); + tradesDisposable.add(exchange + .getStreamingMarketDataService().getTrades(ETH_SPOT).subscribe( + trade -> System.out.println("trade: " + trade))); + tradesDisposable.add(exchange + .getStreamingMarketDataService().getTrades(BTC_SPOT).subscribe( + trade -> System.out.println("trade: " + trade))); try { Thread.sleep(2000); } catch (InterruptedException ignored) {