forked from knowm/XChange
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request knowm#4833 from rizer1980/bybit-fix
[bybit] fix futures error "symbol invalid"
- Loading branch information
Showing
5 changed files
with
149 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
xchange-bybit/src/main/java/org/knowm/xchange/bybit/dto/trade/BybitPlaceOrderPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.knowm.xchange.bybit.dto.trade; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BybitPlaceOrderPayload { | ||
|
||
private String category; | ||
private String symbol; | ||
private String side; | ||
private String orderType; | ||
private String qty; | ||
private String orderLinkId; | ||
private String price; | ||
|
||
public BybitPlaceOrderPayload(String category, String symbol, String side, String orderType, | ||
BigDecimal qty, | ||
String orderLinkId) { | ||
this.category = category; | ||
this.symbol = symbol; | ||
this.side = side; | ||
this.orderType = orderType; | ||
this.qty = qty.toString(); | ||
this.orderLinkId = orderLinkId; | ||
} | ||
public BybitPlaceOrderPayload(String category, String symbol, String side, String orderType, | ||
BigDecimal qty, String orderLinkId, BigDecimal price) { | ||
this.category = category; | ||
this.symbol = symbol; | ||
this.side = side; | ||
this.orderType = orderType; | ||
this.qty = qty.toString(); | ||
this.orderLinkId = orderLinkId; | ||
this.price = price.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
xchange-bybit/src/test/java/org/knowm/xchange/bybit/TradeExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.knowm.xchange.bybit; | ||
|
||
import static org.knowm.xchange.bybit.BybitExchange.SPECIFIC_PARAM_ACCOUNT_TYPE; | ||
|
||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import org.knowm.xchange.Exchange; | ||
import org.knowm.xchange.ExchangeFactory; | ||
import org.knowm.xchange.ExchangeSpecification; | ||
import org.knowm.xchange.bybit.dto.account.walletbalance.BybitAccountType; | ||
import org.knowm.xchange.currency.CurrencyPair; | ||
import org.knowm.xchange.derivative.FuturesContract; | ||
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; | ||
|
||
public class TradeExample { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
try { | ||
testTrade(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static void testTrade() throws IOException { | ||
ExchangeSpecification exchangeSpecification = | ||
new BybitExchange().getDefaultExchangeSpecification(); | ||
exchangeSpecification.setApiKey(System.getProperty("test_api_key")); | ||
exchangeSpecification.setSecretKey(System.getProperty("test_secret_key")); | ||
exchangeSpecification.setExchangeSpecificParametersItem( | ||
SPECIFIC_PARAM_ACCOUNT_TYPE, BybitAccountType.UNIFIED); // or FUND | ||
Exchange exchange = ExchangeFactory.INSTANCE.createExchange( | ||
exchangeSpecification); | ||
Instrument ETH_USDT = new CurrencyPair("ETH/USDT"); | ||
Instrument BTC_USDT_PERP = new FuturesContract(new CurrencyPair("BTC/USDT"), "PERP"); | ||
Instrument ETH_USDT_PERP = new FuturesContract(new CurrencyPair("ETH/USDT"), "PERP"); | ||
|
||
System.out.printf("Wallets: %n%s%n", | ||
exchange.getAccountService().getAccountInfo().getWallets()); | ||
Ticker ticker = exchange | ||
.getMarketDataService() | ||
.getTicker(ETH_USDT_PERP); | ||
System.out.println(ticker.toString()); | ||
|
||
System.out.printf("Instrument %s:%n %s", ETH_USDT, exchange.getExchangeMetaData() | ||
.getInstruments().get(ETH_USDT)); | ||
System.out.printf("Instrument %s:%n %s", ETH_USDT_PERP, exchange.getExchangeMetaData() | ||
.getInstruments().get(ETH_USDT_PERP)); | ||
|
||
BigDecimal minAmountSpot = exchange.getExchangeMetaData().getInstruments().get(ETH_USDT) | ||
.getMinimumAmount(); | ||
BigDecimal minUSDTSpot = minAmountSpot.multiply(ticker.getLast()); | ||
//buy | ||
String marketSpotOrderId = | ||
exchange | ||
.getTradeService() | ||
.placeMarketOrder( | ||
new MarketOrder(OrderType.BID, minUSDTSpot, ETH_USDT)); | ||
System.out.println("Market Spot order id: " + marketSpotOrderId); | ||
//sell | ||
marketSpotOrderId = | ||
exchange | ||
.getTradeService() | ||
.placeMarketOrder( | ||
new MarketOrder(OrderType.ASK, minAmountSpot, ETH_USDT)); | ||
|
||
System.out.println("Market Spot order id: " + marketSpotOrderId); | ||
|
||
BigDecimal minAmountFuture = exchange.getExchangeMetaData().getInstruments().get(ETH_USDT_PERP) | ||
.getMinimumAmount(); | ||
|
||
//long | ||
String marketFutureOrderId = | ||
exchange | ||
.getTradeService() | ||
.placeMarketOrder( | ||
new MarketOrder(OrderType.BID, minAmountFuture, ETH_USDT_PERP)); | ||
System.out.println("Market Future order id: " + marketFutureOrderId); | ||
|
||
//short | ||
String limitFutureOrderId = | ||
exchange | ||
.getTradeService() | ||
.placeLimitOrder( | ||
new LimitOrder(OrderType.ASK, minAmountFuture, ETH_USDT_PERP, "123213", null, | ||
ticker.getLast())); | ||
System.out.println("Limit Future order id: " + limitFutureOrderId); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters