Coder Social home page Coder Social logo

bybit-java-api's Introduction

bybit-java-api

Java 11 Contributor Victor License: MIT

Table of Contents

About

The Official Java API connector for Bybit's HTTP and WebSocket APIs.

Dive into a plethora of functionalities:

  • Market Data Retrieval
  • Trade Execution
  • Position Management
  • Account and Asset Info Retrieval
  • User and Upgrade Management
  • Public Websocket Streaming
  • Private Websocket Streaming
  • Lending Institution and Client
  • Broker Earning Data

bybit-java-api provides an official, robust, and high-performance Java connector to Bybit's trading APIs.

Initially conceptualized by Java developer Victor, this module is now maintained by Bybit's in-house Java experts.

Your contributions are most welcome!

Development

bybit-java-api is under active development with the latest features and updates from Bybit's API implemented promptly. The module utilizes minimal external libraries to provide a lightweight and efficient experience. If you've made enhancements or fixed bugs, please submit a pull request.

Installation

Ensure you have Java 11 or higher. You can include bybit-java-api in your project using Maven or Gradle.

Maven Example

<!-- Maven -->
<dependency>
    <groupId>io.github.wuhewuhe</groupId>
    <artifactId>bybit-java-api</artifactId>
    <version>1.2.4</version>
</dependency>

Gradle Example

implementation group: 'io.github.wuhewuhe', name: 'bybit-java-api', version: '1.2.4'

Furthermore, build tool, please check sonar type central repository

Release-Notes

Websockets

  • Add WebSocketMessage handler class to all public and private channels

Improvements

  • public and private websocket channel set message handler

Change Log

  • Spot Margin Trade endpoints are decrypted for classical users

Usage

Note: Replace placeholders (like YOUR_API_KEY, links, or other details) with the actual information. You can also customize this template to better fit the actual state and details of your Java API.

HttP Client Factory & Websocket Client

Client Factory Option

private final String apiKey;
private final String secret;
private final String baseUrl;
private final Boolean debugMode;
private final String logOption;
private final Long recvWindow;

Http Async Examples

  • Place Single Order By Object
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.TESTNET_DOMAIN, true).newAsyncTradeRestClient();
        Map<String, Object> order =Map.of(
        "category", "option",
        "symbol", "BTC-29DEC23-10000-P",
        "side", "Buy",
        "orderType", "Limit",
        "orderIv", "0.1",
        "qty", "0.1",
        "price", "5"
        );
        client.createOrder(order, System.out::println);
  • Place Single Order By Map
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.TESTNET_DOMAIN, true).newAsyncTradeRestClient();
var newOrderRequest = TradeOrderRequest.builder().category(CategoryType.LINEAR).symbol("XRPUSDT")
.side(Side.BUY).orderType(TradeOrderType.MARKET).qty("10").timeInForce(TimeInForce.GOOD_TILL_CANCEL)
.positionIdx(PositionIdx.ONE_WAY_MODE).build();
client.createOrder(newOrderRequest, System.out::println);
  • Place Batch Order
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.TESTNET_DOMAIN).newAsyncTradeRestClient();
var orderRequests = Arrays.asList(TradeOrderRequest.builder().category(CategoryType.OPTION).symbol("BTC-10FEB23-24000-C").side(Side.BUY).orderType(TradeOrderType.LIMIT).qty("0.1")
.price("5").orderIv("0.1").timeInForce(TimeInForce.GOOD_TILL_CANCEL).orderLinkId("9b381bb1-401").mmp(false).reduceOnly(false).build(),
TradeOrderRequest.builder().category(CategoryType.OPTION).symbol("BTC-10FEB23-24000-C").side(Side.BUY).orderType(TradeOrderType.LIMIT).qty("0.1")
.price("5").orderIv("0.1").timeInForce(TimeInForce.GOOD_TILL_CANCEL).orderLinkId("82ee86dd-001").mmp(false).reduceOnly(false).build());
var createBatchOrders = BatchOrderRequest.builder().category(CategoryType.OPTION).request(orderRequests).build();
client.createBatchOrder(createBatchOrders, System.out::println);
  • Position Info
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.TESTNET_DOMAIN).newAsyncPositionRestClient();
var positionListRequest = PositionDataRequest.builder().category(CategoryType.LINEAR).symbol("BTCUSDT").build();
client.getPositionInfo(positionListRequest, System.out::println);
  • Asset Info
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.TESTNET_DOMAIN).newAsyncAssetRestClient();
var coinExchangeRecordsRequest = AssetDataRequest.builder().build();
client.getAssetCoinExchangeRecords(coinExchangeRecordsRequest, System.out::println);

Http Sync Examples

  • Amend Order
var amendOrderRequest = TradeOrderRequest.builder().orderId("1523347543495541248").category(CategoryType.LINEAR).symbol("XRPUSDT")
        .price("0.5")  // setting a new price, for example
        .qty("15")  // and a new quantity
        .build();
var amendedOrder = client.amendOrder(amendOrderRequest);
System.out.println(amendedOrder);
  • Place Batch Order
String jsonRequest = """
{
  "category":"option", 
  "request": [ 
  {  
  "category":"option", 
  "symbol":"BTC-10FEB23-24000-C", 
  "orderType":"Limit", 
  "side":"Buy",   
  "qty":"0.1", 
  "price":"5", 
  "orderIv":"0.1", 
  "timeInForce":"GTC", 
  "orderLinkId":"9b381bb1-401", 
  "mmp":false, 
  "reduceOnly":false  
  },
  {  
  "category":"option", 
  "symbol":"BTC-10FEB23-24000-C", 
  "orderType":"Limit", 
  "side":"Buy",   
  "qty":"0.1", 
  "price":"5", 
  "orderIv":"0.1", 
  "timeInForce":"GTC", 
  "orderLinkId":"82ee86dd-001", 
  "mmp":false, 
  "reduceOnly":false  
  } 
 ]
}
""";
var batchOrderRequest = client.createBathOrder(jsonRequest);
System.out.println(batchOrderRequest);
  • Market Data Info
var client = BybitApiClientFactory.newInstance(BybitApiConfig.TESTNET_DOMAIN,true).newMarketDataRestClient();
var marketKLineRequest = MarketDataRequest.builder().category(CategoryType.LINEAR).symbol("BTCUSDT").marketInterval(MarketInterval.WEEKLY).build();
// Weekly market Kline
var marketKlineResult = client.getMarketLinesData(marketKLineRequest);
System.out.println(marketKlineResult);

// Weekly market price Kline for a symbol
var marketPriceKlineResult = client.getMarketPriceLinesData(marketKLineRequest);
System.out.println(marketPriceKlineResult);

// Weekly index price Kline for a symbol
var indexPriceKlineResult = client.getIndexPriceLinesData(marketKLineRequest);
System.out.println(indexPriceKlineResult);

// Weekly premium index price Kline for a symbol
var indexPremiumPriceKlineResult = client.getPremiumIndexPriceLinesData(marketKLineRequest);
System.out.println(indexPremiumPriceKlineResult);

// Get server time
var serverTime = client.getServerTime();
System.out.println(serverTime);
  • User Management
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.TESTNET_DOMAIN).newUserRestClient();
var subUserRequest = UserDataRequest.builder().username("VictorWuTest3").password("Password123").memberType(MemberType.NORMAL_SUB_ACCOUNT).note("Some note").switchOption(SwitchOption.TURN_OFF).isUta(IsUta.CLASSIC_ACCOUNT).build();
var subUser = client.createSubMember(subUserRequest);
System.out.println(subUser);

Websocket Client

private final Integer pingInterval;
private final WebsocketMessageHandler messageHandler;
private final String maxAliveTime; // Only valid for private channel, timeunit in seconds or minutes

Websocket public channel

  • Order book Subscribe
var client = BybitApiClientFactory.newInstance(BybitApiConfig.STREAM_TESTNET_DOMAIN, true, "okhttp3").newWebsocketClient(20);

// Orderbook
client.getPublicChannelStream(List.of("orderbook.50.BTCUSDT"), BybitApiConfig.V5_PUBLIC_LINEAR);

// Subscribe Orderbook more than one args
client.getPublicChannelStream(List.of("orderbook.50.BTCUSDT","orderbook.1.ETHUSDT"), BybitApiConfig.V5_PUBLIC_LINEAR);
  • Ticker Subscribe
var client = BybitApiClientFactory.newInstance(BybitApiConfig.STREAM_MAINNET_DOMAIN, true).newWebsocketClient();

client.setMessageHandler(message -> {
    var tickerData = (new ObjectMapper()).readValue(message, WebSocketTickerMessage.class);
    // Process message data here
    System.out.println("Websocket Message Data: " + tickerData.getData().toString());
});

// Ticker
client.getPublicChannelStream(List.of("tickers.BTCUSDT"), BybitApiConfig.V5_PUBLIC_LINEAR);

Websocket private channel

  • Position Subscribe
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.STREAM_TESTNET_DOMAIN).newWebsocketClient();

// Position
client.getPrivateChannelStream(List.of("position"), BybitApiConfig.V5_PRIVATE);
  • Order Channel active Message Handler & Max Alive Time & Ping Interval
var client = BybitApiClientFactory.newInstance("YOUR_API_KEY", "YOUR_API_SECRET", BybitApiConfig.STREAM_TESTNET_DOMAIN, true)
        .newWebsocketClient(5, "60s", (message) -> {
        var orderMessage = (new ObjectMapper()).readValue(message, WebSocketOrderMessage.class);
        // Process message data here
        System.out.println("Websocket Message Data: " + orderMessage.getData().toString());
        });

// Order
client.getPrivateChannelStream(List.of("order"), BybitApiConfig.V5_PRIVATE);

Contact

For support, join our Bybit API community on Telegram.

Contributors

List of other contributors


Victor

๐Ÿ’ป ๐Ÿ“–

bybit-java-api's People

Contributors

victorfrwu avatar doom-prince avatar matteoxleggieri avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.