Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bybit-stream] getOrderChanges, getPositionChanges #4885

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.knowm.xchange.binance.service;

import static org.knowm.xchange.binance.BinanceExchange.EXCHANGE_TYPE;
import static org.knowm.xchange.binance.dto.ExchangeType.FUTURES;
import static org.knowm.xchange.binance.dto.ExchangeType.SPOT;

import java.io.IOException;
import java.math.BigDecimal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ public static CurrencyPair guessSymbol(String symbol) {
return new CurrencyPair(symbol.substring(0, splitIndex), symbol.substring(splitIndex));
}

public static Instrument guessSymbol(String symbol, BybitCategory category) {
switch (category) {
case SPOT: {
return guessSymbol(symbol);
}
case LINEAR: {
if (symbol.endsWith("USDT")) {
int splitIndex = symbol.lastIndexOf("USDT");
return new FuturesContract((symbol.substring(0, splitIndex)+"/"+ symbol.substring(splitIndex)+"/PERP"));
} else if (symbol.endsWith("PERP")) {
int splitIndex = symbol.lastIndexOf("PERP");
return new FuturesContract((symbol.substring(0, splitIndex)+"/"+ "USDC/PERP"));
}
}
}
return null;
}

public static Instrument adaptInstrumentInfo(BybitInstrumentInfo instrumentInfo) {
if (instrumentInfo instanceof BybitSpotInstrumentInfo) {
return new CurrencyPair(instrumentInfo.getBaseCoin(), instrumentInfo.getQuoteCoin());
Expand Down Expand Up @@ -213,6 +231,8 @@ public static InstrumentMetaData symbolToCurrencyPairMetaData(
.priceScale(instrumentInfo.getPriceFilter().getTickSize().scale())
.priceStepSize(instrumentInfo.getPriceFilter().getTickSize())
.tradingFee(instrumentInfo.getDeliveryFeeRate())
.volumeScale(instrumentInfo.getLotSizeFilter().getQtyStep().scale())
.amountStepSize(instrumentInfo.getLotSizeFilter().getQtyStep())
.build();
}

Expand Down Expand Up @@ -269,7 +289,7 @@ private static OrderType adaptOrderType(BybitOrderDetail bybitOrderResult) {
}
}

private static OrderStatus adaptBybitOrderStatus(BybitOrderStatus orderStatus) {
public static OrderStatus adaptBybitOrderStatus(BybitOrderStatus orderStatus) {
switch (orderStatus) {
case CREATED:
return OrderStatus.OPEN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class BybitExchange extends BaseExchange {

public static final String SPECIFIC_PARAM_ACCOUNT_TYPE = "accountType";
private static final String BASE_URL = "https://api.bybit.com";
private static final String DEMO_URL = "https://api-demo.bybit.com";
// private static final String DEMO_URL = "https://api-demo.bybit.com";
private static final String TESTNET_URL = "https://api-testnet.bybit.com";


@Override
protected void initServices() {
Expand Down Expand Up @@ -108,7 +110,7 @@ public void applySpecification(ExchangeSpecification exchangeSpecification) {
if (exchangeSpecification
.getExchangeSpecificParametersItem(Exchange.USE_SANDBOX)
.equals(true)) {
exchangeSpecification.setSslUri(DEMO_URL);
exchangeSpecification.setSslUri(TESTNET_URL);
}
super.applySpecification(exchangeSpecification);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public void testGuessSymbol() {
assertThat(guessSymbol("LTCBTC")).isEqualTo(new CurrencyPair("LTC", "BTC"));
assertThat(guessSymbol("BTCDAI")).isEqualTo(new CurrencyPair("BTC", "DAI"));
assertThat(guessSymbol("ABCDEFG")).isEqualTo(new CurrencyPair("ABCD", "EFG"));

assertThat(guessSymbol("BTCUSDT", BybitCategory.LINEAR)).isEqualTo(new FuturesContract("BTC/USDT/PERP"));
assertThat(guessSymbol("ETHPERP", BybitCategory.LINEAR)).isEqualTo(new FuturesContract("ETH/USDC/PERP"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dto.trade;

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

@Getter
public class BybitComplexPositionChanges extends OpenPosition {
private BigDecimal positionValue;
private BigDecimal leverage;
private BigDecimal takeProfit;
private BigDecimal stopLoss;
private BigDecimal curRealisedPnl;
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,
long seq) {
super(instrument, type, size, entryPrice, liquidationPrice, unRealisedPnl);
this.positionValue = positionValue;
this.leverage = leverage;
this.takeProfit = takeProfit;
this.stopLoss = stopLoss;
this.curRealisedPnl = curRealisedPnl;
this.createdTime = createdTime;
this.updatedTime = updatedTime;
this.seq = seq;
}

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());
this.positionValue = changes.positionValue;
this.leverage = changes.leverage;
this.takeProfit = changes.takeProfit;
this.stopLoss = changes.stopLoss;
this.curRealisedPnl = changes.curRealisedPnl;
this.createdTime = changes.createdTime;
this.updatedTime = changes.updatedTime;
this.seq = changes.seq;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dto.trade;

import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
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;

@Getter
public class BybitOrderChangesResponse {

String id;
String topic;
long creationTime;
List<BybitOrderChanges> data = new ArrayList<>();

@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;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dto.trade;

import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import org.knowm.xchange.bybit.dto.BybitCategory;
import org.knowm.xchange.bybit.dto.trade.BybitSide;


@Getter
public class BybitPositionChangesResponse {

String id;
String topic;
long creationTime;
List<BybitPositionChanges> 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;

}
}


Loading
Loading