Skip to content

Commit

Permalink
Merge pull request #4900 from rizer1980/PR/bybit-ws-orders
Browse files Browse the repository at this point in the history
[bybit] ws ComplexOrdersChanges and ComplexPositionChanges
  • Loading branch information
timmolter authored Jun 11, 2024
2 parents 8042003 + 5c09209 commit a6b00bc
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;
import lombok.Getter;
import org.knowm.xchange.instrument.Instrument;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OpenPosition implements Serializable {
/** The instrument */
Expand Down Expand Up @@ -43,29 +45,6 @@ public OpenPosition(
this.unRealisedPnl = unRealisedPnl;
}

public Instrument getInstrument() {
return instrument;
}

public Type getType() {
return type;
}

public BigDecimal getSize() {
return size;
}

public BigDecimal getPrice() {
return price;
}

public BigDecimal getLiquidationPrice() {
return liquidationPrice;
}

public BigDecimal getUnRealisedPnl() {
return unRealisedPnl;
}

@Override
public boolean equals(final Object o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dto.trade;

import com.fasterxml.jackson.annotation.JsonValue;
import java.math.BigDecimal;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.knowm.xchange.bybit.dto.BybitCategory;
import org.knowm.xchange.bybit.dto.trade.BybitOrderStatus;
import org.knowm.xchange.bybit.dto.trade.BybitOrderType;
import org.knowm.xchange.bybit.dto.trade.BybitSide;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.instrument.Instrument;

@Getter
@Setter
public class BybitComplexOrderChanges extends Order {
private BybitCategory category;
private BigDecimal price;
private BybitSide side;
private BigDecimal leavesQty;
private BigDecimal leavesValue;
private BigDecimal cumExecValue;
private String feeCurrency;
private TimeInForce timeInForce;
private BybitOrderType orderType;
private boolean reduceOnly;
private Date updatedTime;

public BybitComplexOrderChanges(OrderType type, BigDecimal originalAmount,
Instrument instrument, String id, Date timestamp,
BigDecimal averagePrice, BigDecimal cumulativeAmount,
BigDecimal fee, OrderStatus status, String userReference) {
super(type, originalAmount, instrument, id, timestamp, averagePrice, cumulativeAmount, fee,
status, userReference);
}

public BybitComplexOrderChanges(OrderType type, BigDecimal originalAmount, Instrument instrument,
String id, Date timestamp, BigDecimal averagePrice, BigDecimal cumulativeAmount,
BigDecimal fee, OrderStatus status, String userReference, BybitCategory category,
BigDecimal price, BybitSide side, BigDecimal leavesQty, BigDecimal leavesValue,
BigDecimal cumExecValue, String feeCurrency, TimeInForce timeInForce,
BybitOrderType orderType, boolean reduceOnly, Date updatedTime) {
super(type, originalAmount, instrument, id, timestamp, averagePrice, cumulativeAmount, fee,
status, userReference);
this.category = category;
this.price = price;
this.side = side;
this.leavesQty = leavesQty;
this.leavesValue = leavesValue;
this.cumExecValue = cumExecValue;
this.feeCurrency = feeCurrency;
this.timeInForce = timeInForce;
this.orderType = orderType;
this.reduceOnly = reduceOnly;
this.updatedTime = updatedTime;
}

@Getter
@AllArgsConstructor
public enum TimeInForce {
GTC("GTC"),
IOC("IOC"),
FOK("FOK"),
POSTONLY("PostOnly");
@JsonValue
private final String value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;
import org.knowm.xchange.bybit.dto.BybitCategory;
import org.knowm.xchange.dto.account.OpenPosition;
import org.knowm.xchange.instrument.Instrument;

@Getter
@Setter
public class BybitComplexPositionChanges extends OpenPosition {
private BigDecimal positionValue;
private BigDecimal leverage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,52 @@ public class BybitOrderChangesResponse {
@Getter
public static class BybitOrderChanges {

BybitCategory category;
String orderId;
String orderLinkId;
String isLeverage;
String blockTradeId;
String symbol;
String price;
String qty;
BybitSide side;
int positionIdx;
BybitOrderStatus orderStatus;
String createType;
String cancelType;
String rejectReason;
String avgPrice;
String leavesQty;
String leavesValue;
String cumExecQty;
String cumExecValue;
String cumExecFee;
String feeCurrency;
String timeInForce;
BybitOrderType orderType;
String stopOrderType;
String ocoTriggerBy;
String orderIv;
String marketUnit;
String triggerPrice;
String takeProfit;
String stopLoss;
String tpslMode;
String tpLimitPrice;
String slLimitPrice;
String tpTriggerBy;
String slTriggerBy;
int triggerDirection;
String triggerBy;
String lastPriceOnCreated;
boolean reduceOnly;
boolean closeOnTrigger;
String placeType;
String smpType;
int smpGroup;
String smpOrderId;
String createdTime;
String updatedTime;
private BybitCategory category;
private String orderId;
private String orderLinkId;
private String isLeverage;
private String blockTradeId;
private String symbol;
private String price;
private String qty;
private BybitSide side;
private int positionIdx;
private BybitOrderStatus orderStatus;
private String createType;
private String cancelType;
private String rejectReason;
private String avgPrice;
private String leavesQty;
private String leavesValue;
private String cumExecQty;
private String cumExecValue;
private String cumExecFee;
private String feeCurrency;
private String timeInForce;
private BybitOrderType orderType;
private String stopOrderType;
private String ocoTriggerBy;
private String orderIv;
private String marketUnit;
private String triggerPrice;
private String takeProfit;
private String stopLoss;
private String tpslMode;
private String tpLimitPrice;
private String slLimitPrice;
private String tpTriggerBy;
private String slTriggerBy;
private int triggerDirection;
private String triggerBy;
private String lastPriceOnCreated;
private boolean reduceOnly;
private boolean closeOnTrigger;
private String placeType;
private String smpType;
private int smpGroup;
private String smpOrderId;
private String createdTime;
private String updatedTime;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import static org.knowm.xchange.bybit.BybitAdapters.guessSymbol;

import dto.marketdata.BybitPublicOrder;
import dto.trade.BybitComplexOrderChanges;
import dto.trade.BybitComplexOrderChanges.TimeInForce;
import dto.trade.BybitComplexPositionChanges;
import dto.trade.BybitOrderChangesResponse;
import dto.trade.BybitOrderChangesResponse.BybitOrderChanges;
import dto.trade.BybitPositionChangesResponse.BybitPositionChanges;
import dto.trade.BybitTrade;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -94,9 +97,10 @@ public static List<Order> adaptOrdersChanges(List<BybitOrderChanges> bybitOrderC
}
builder.fee(new BigDecimal(bybitOrderChange.getCumExecFee())).
leverage(bybitOrderChange.getIsLeverage()).id(bybitOrderChange.getOrderId())
.orderStatus(adaptBybitOrderStatus(bybitOrderChange.getOrderStatus()))
.timestamp(date).cumulativeAmount(new BigDecimal(bybitOrderChange.getCumExecQty()))
.originalAmount(new BigDecimal(bybitOrderChange.getQty())).id(bybitOrderChange.getOrderId())
.orderStatus(adaptBybitOrderStatus(bybitOrderChange.getOrderStatus()))
.timestamp(date).cumulativeAmount(new BigDecimal(bybitOrderChange.getCumExecQty()))
.originalAmount(new BigDecimal(bybitOrderChange.getQty()))
.id(bybitOrderChange.getOrderId())
.userReference(bybitOrderChange.getOrderLinkId());
orders.add(builder.build());
}
Expand All @@ -108,39 +112,68 @@ public static OpenPositions adaptPositionChanges(
OpenPositions openPositions = new OpenPositions(new ArrayList<>());
for (BybitPositionChanges position : bybitPositionChanges) {
OpenPosition.Type type = null;
if(!position.getSide().isEmpty()) {
if (!position.getSide().isEmpty()) {
type = position.getSide().equals("Buy") ? Type.LONG : Type.SHORT;
}
BigDecimal liqPrice = null;
if(!position.getLiqPrice().isEmpty()) {
if (!position.getLiqPrice().isEmpty()) {
liqPrice = new BigDecimal(position.getLiqPrice());
}
OpenPosition openPosition = new OpenPosition(guessSymbol(position.getSymbol(),
position.getCategory()),type,new BigDecimal(position.getSize()),
new BigDecimal(position.getEntryPrice()), liqPrice, new BigDecimal(position.getUnrealisedPnl()));
position.getCategory()), type, new BigDecimal(position.getSize()),
new BigDecimal(position.getEntryPrice()), liqPrice,
new BigDecimal(position.getUnrealisedPnl()));
openPositions.getOpenPositions().add(openPosition);
}
return openPositions;
}

public static List<BybitComplexPositionChanges> adaptComplexPositionChanges(List<BybitPositionChanges> data) {
public static List<BybitComplexPositionChanges> adaptComplexPositionChanges(
List<BybitPositionChanges> data) {
List<BybitComplexPositionChanges> result = new ArrayList<>();
for (BybitPositionChanges position : data) {
OpenPosition.Type type = null;
if(!position.getSide().isEmpty()) {
if (!position.getSide().isEmpty()) {
type = position.getSide().equals("Buy") ? Type.LONG : Type.SHORT;
}
BigDecimal liqPrice = null;
if(!position.getLiqPrice().isEmpty()) {
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());
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());
result.add(positionChanges);
}
return result;
}

public static List<BybitComplexOrderChanges> adaptComplexOrdersChanges(
List<BybitOrderChanges> data) {
List<BybitComplexOrderChanges> result = new ArrayList<>();
for (BybitOrderChanges change : data) {
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())));
result.add(orderChanges);
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.bitrich.xchangestream.bybit;

import com.fasterxml.jackson.databind.ObjectMapper;
import dto.trade.BybitComplexOrderChanges;
import dto.trade.BybitComplexPositionChanges;
import dto.trade.BybitOrderChangesResponse;
import dto.trade.BybitPositionChangesResponse;
Expand Down Expand Up @@ -34,6 +35,20 @@ public Observable<Order> getOrderChanges(BybitCategory category) {
});
}

public Observable<BybitComplexOrderChanges> getComplexOrderChanges(BybitCategory category) {
String channelUniqueId = "order";
if(category != null) {
channelUniqueId += "." + category.getValue();
}
return streamingService
.subscribeChannel(channelUniqueId).flatMap(
node -> {
BybitOrderChangesResponse bybitOrderChangesResponse = mapper.treeToValue(node, BybitOrderChangesResponse.class);
return Observable.fromIterable(
BybitStreamAdapters.adaptComplexOrdersChanges(bybitOrderChangesResponse.getData()));
});
}

public Observable<OpenPosition> getPositionChanges(BybitCategory category) {
String channelUniqueId = "position";
if(category != null) {
Expand Down

0 comments on commit a6b00bc

Please sign in to comment.