Skip to content

Commit

Permalink
Merge pull request #100 from bybit-exchange/v5-only
Browse files Browse the repository at this point in the history
 Upgrade to Bybit's v5 APIs (5.0.0)
  • Loading branch information
dextertd authored Apr 3, 2023
2 parents df32cc4 + b9b2d3d commit 1b7162b
Show file tree
Hide file tree
Showing 51 changed files with 4,499 additions and 6,579 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,7 @@ dmypy.json
.pyre/

# PyCharm idea files
*.idea
*.idea

# VSCode files
*.vscode
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ python:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
cache: pip
before_install:
- "pip install -U pip"
- "export PYTHONPATH=$PYTHONPATH:$(pwd)"
install:
- pip install -r requirements.txt
script:
- python tests/test_pybit.py
- python -m unittest
28 changes: 23 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.0.0] - 2023-04-03
This version upgrades pybit to Bybit's [version 5 (v5) APIs](https://bybit-exchange.github.io/docs/v5/intro). It supports both [Unified Trading Accounts](https://www.bybit.com/en-US/help-center/s/article/Introduction-to-Bybit-Unified-Trading-Account) (UTA) and non-UTA accounts. Bybit is not expected to develop any more major API versions in the future, so Bybit's v5 API (and subsequently, pybit's 5.0.0) is expected to be supported in the long-term.

See the [examples folder](https://github.com/bybit-exchange/pybit/tree/master/examples) for examples on how to interact with the latest modules.

### Added
- Bybit's v5 HTTP and WebSocket APIs in the `unified_trading` module. See what markets All-In-One V5 API supports in the [upgrade guide](https://bybit-exchange.github.io/docs/v5/upgrade-guide).

### Modified
- Non-v5 modules like `copy_trading`, `usdc_options`, and `usdc_perpetuals` to continue to work from a `legacy` subpackage. Import like so: `from pybit.legacy.copy_trading import HTTP
`. These modules are maintained because they are currently not supported by the v5 API; see the [upgrade guide](https://bybit-exchange.github.io/docs/v5/upgrade-guide).

### Removed
- Various legacy modules which have been superseded by the v5 API via the `unified_trading` module

### Fixed
- Tests for V5 endpoints


## [3.0.0rc5] - 2023-02-02
### Added
Expand Down Expand Up @@ -49,28 +67,28 @@ Future modules will be removed as Bybit's APIs are further unified so that they

This is a pre-release, as indicated by the `rc` (release candidate) in the version number. Future versions may have breaking changes. An imminent major version of the Bybit API will introduce major changes before these v3 APIs make it to the production version.

## Added
### Added
- Bybit's main v3 HTTP and WebSocket APIs:
- `contract` – inverse perpetuals, inverse futures, USDT perpetuals, and USDC options
- `unified_margin` – USDT perpetuals and USDC options

## Modified
### Modified
- `spot` to use v3 HTTP API and WebSocket APIs
- `account_asset` to use v3 HTTP API

## Removed
### Removed
- `usdt_perpetual` because it is now accessible via `contract` and `unified_margin`


## [2.4.1rc0] - 2022-10-07
## Modified
### Modified
- `is_connected()` and the WebSocket reconnection logic.


## [2.4.1] - 2022-10-07
- See below release candidates for further changes.

## Fixed
### Fixed
- Wrong endpoint path in `usdc_perpetual.py`
- Wrong endpoint path in `account_asset.py`

Expand Down
250 changes: 22 additions & 228 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,7 @@ Official Python3 API connector for Bybit's HTTP and WebSockets APIs.
- [About](#about)
- [Development](#development)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
* [Market Data Endpoints](#market-data-endpoints)
+ [Advanced Data](#advanced-data)
* [Account Data Endpoints](#account-data-endpoints)
+ [Active Orders](#active-orders)
+ [Conditional Orders](#conditional-orders)
+ [Position](#position)
+ [Market Maker Protection](#market-maker-protection)
+ [Risk Limit](#risk-limit)
+ [Funding](#funding)
+ [API Key Info](#api-key-info)
+ [LCP Info](#lcp-info)
* [Wallet Data Endpoints](#wallet-data-endpoints)
* [API Data Endpoints](#api-data-endpoints)
* [Account Asset Endpoints](#account-asset-endpoints)
* [WebSocket](#websocket)
* [Futures](#futures)
+ [Public Topics](#public-topics)
+ [Private Topics](#private-topics)
+ [Spot](#spot)
+ [Public Topics V1](#public-topics-v1)
+ [Public Topics V2](#public-topics-v2)
+ [Private Topics](#private-topics-spot)
- [Usage](#usage)
- [Contact](#contact)
- [Contributors](#contributors)
- [Donations](#donations)
Expand All @@ -53,233 +31,49 @@ It was designed with the following vision in mind:
`pybit` is being actively developed, and new Bybit API changes should arrive on `pybit` very quickly. `pybit` uses `requests` and `websocket-client` for its methods, alongside other built-in modules. Anyone is welcome to branch/fork the repository and add their own upgrades. If you think you've made substantial improvements to the module, submit a pull request and we'll gladly take a look.

## Installation
`pybit` requires Python 3.6.1 or higher. The module can be installed manually or via [PyPI](https://pypi.org/project/pybit/) with `pip`:
`pybit` requires Python 3.9.1 or higher. The module can be installed manually or via [PyPI](https://pypi.org/project/pybit/) with `pip`:
```
pip install pybit
```

## Basic Usage
## Usage
You can retrieve a specific market like so:
```python
from pybit import inverse_perpetual
from pybit.unified_trading import HTTP
```
Create an HTTP session and connect via WebSocket for Inverse on mainnet:
```python
session = inverse_perpetual.HTTP(
endpoint='https://api.bybit.com',
api_key='...',
api_secret='...'
)
ws = inverse_perpetual.WebSocket(
test=False,
session = HTTP(
testnet=False,
api_key="...",
api_secret="..."
api_secret="...",
)
```
Information can be sent to, or retrieved from, the Bybit APIs:

```python
# Get orderbook.
session.orderbook(symbol='BTCUSD')
# Get the orderbook of the USDT Perpetual, BTCUSDT
session.get_orderbook(category="linear", symbol="BTCUSDT")

# Create five long orders.
# Create five long USDC Options orders.
# (Currently, only USDC Options support sending orders in bulk.)
payload = {"category": "option"}
orders = [{
"symbol": "BTCUSD",
"order_type": "Limit",
"side": "Buy",
"qty": 100,
"price": i,
"time_in_force": "GoodTillCancel"
} for i in [5000, 5500, 6000, 6500, 7000]]

"symbol": "BTC-30JUN23-20000-C",
"side": "Buy",
"orderType": "Limit",
"qty": "0.1",
"price": i,
} for i in [15000, 15500, 16000, 16500, 16600]]

payload["request"] = orders
# Submit the orders in bulk.
session.place_active_order_bulk(orders)


# Check on your order and position through WebSocket.
def handle_orderbook(message):
print(message)


def handle_position(message):
print(message)


ws.orderbook_stream(handle_orderbook, "BTCUSD")
ws.position_stream(handle_position)

while True:
# Run your main trading strategy here
pass # To avoid CPU utilisation, use time.sleep(1)
session.place_batch_order(payload)
```
Check out the example python files or the list of endpoints below for more information on available
endpoints and methods. Usage examples on the `HTTP` methods can
be found at:
- https://github.com/bybit-exchange/pybit/blob/master/examples/http_example.py

Usage examples on the `WebSocket` methods can be found at:
- https://github.com/bybit-exchange/pybit/blob/master/examples/websocket_example.py

### Market Data Endpoints

| Endpoint | Method |
| ------------- | ------------- |
| Orderbook | `orderbook()` |
| Query Kline | `query_kline()` |
| Latest Information for Symbol | `latest_information_for_symbol()` |
| Public Trading Records | `public_trading_records()` |
| Query Symbol | `query_symbol()` |
| Liquidated Orders | `liquidated_orders()` |
| Query Mark Price Kline | `query_mark_price_kline()` |
| Open Interest | `open_interest()` |
| Delivery Price (USDC) | `delivery_price()` |
| Last 500 Trades (USDC) | `last_500_trades()` |

#### Advanced Data

| Endpoint | Method |
| ------------- | ------------- |
| Query Kline | `query_kline()` |
| Latest Big Deal | `latest_big_deal()` |
| Long Short Ratio | `long_short_ratio()` |

### Account Data Endpoints

#### Active Orders

| Endpoint | Method |
| --------------------------------------- | ------------------------------------ |
| Place Active Order | `place_active_order()` |
| Get Active Order | `get_active_order()` |
| Cancel Active Order | `cancel_active_order()` |
| Cancel All Active Orders | `cancel_all_active_orders()` |
| Replace Active Order | `replace_active_order()` |
| Query Active Order | `query_active_order()` |
| Batch Place Active Orders (USDC) | `batch_place_active_orders()` |
| Fast Cancel Active Order (Spot) | `fast_cancel_active_order()` |
| Batch Cancel Active Order (Spot, USDC) | `batch_cancel_active_order()` |
| Batch Fast Cancel Active Order (Spot) | `batch_fast_cancel_active_order()` |
| Batch Cancel Active Order By IDs (Spot) | `batch_cancel_active_order_by_ids()` |
| Batch Replace Active Orders (USDC) | `batch_replace_active_orders()` |

#### Conditional Orders

| Endpoint | Method |
| ------------- | ------------- |
| Place Conditional Order | `place_conditional_order()` |
| Get Conditional Order | `get_conditional_order()` |
| Cancel Conditional Order | `cancel_conditional_order()` |
| Cancel All Conditional Orders | `cancel_all_conditional_orders()` |
| Replace Conditional Order | `replace_conditional_order()` |
| Query Conditional Order | `query_conditional_order()` |

#### Position

| Endpoint | Method |
| ------------- | ------------- |
| My Position | `my_position()` |
| Set Auto Add Margin (Linear) | `set_auto_add_margin()` |
| Cross/Isolated Margin Switch (Linear) | `cross_isolated_margin_switch()` |
| Full/Partial Position SL/TP Switch | `full_partial_position_tp_sl_switch` |
| Add/Reduce Margin (Linear) | `add_reduce_margin()` |
| Set Trading-Stop | `set_trading_stop()` |
| Set Leverage | `set_leverage()` |
| User Leverage (deprecated) | `user_leverage()` |
| User Trade Records | `user_trade_records()` |
| Closed Profit and Loss | `closed_profit_and_loss()` |
| Query Trading Fee Rate | `query_trading_fee_rate()` |
| Query Delivery History (USDC) | `query_delivery_history()` |
| Query Position Expiration Date (USDC) | `query_position_expiration_date()` |

#### Market Maker Protection

Only available for the USDC API.

| Endpoint | Method |
| ---------- | -------------- |
| Query MMP | `query_mmp`() |
| Modify MMP | `modify_mmp`() |
| Reset MMP | `reset_mmp`() |

#### Risk Limit

| Endpoint | Method |
| ------------- | ------------- |
| Get Risk Limit | `my_position()` |
| Set Risk Limit (Inverse) | `set_auto_add_margin()` |

#### Funding

| Endpoint | Method |
| ------------- | ------------- |
| Get the Last Funding Rate | `get_the_last_funding_rate()` |
| My Last Funding Fee | `my_last_funding_fee()` |
| Predicted Funding Rate and My Funding Fee | `predicted_funding_rate()` |

#### API Key Info

| Endpoint | Method |
| ------------- | ------------- |
| API Key Info | `api_key_info()` |

#### LCP Info

| Endpoint | Method |
| ------------- | ------------- |
| LCP Info | `lcp_info()` |

### Wallet Data Endpoints

| Endpoint | Method |
| ------------- | ------------- |
| Get Wallet Balance | `get_wallet_balance()` |
| Wallet Fund Records | `wallet_fund_records()` |
| Withdraw Records | `withdraw_records()` |
| Asset Exchange Records | `asset_exchange_records()` |
| Get Asset Info (USDC) | `get_asset_info()` |
| Get Margin Mode (USDC) | `get_margin_mode()` |

### API Data Endpoints

| Endpoint | Method |
| ------------- | ------------- |
| Server Time | `server_time()` |
| Announcement | `announcement()` |

### Account Asset Endpoints

| Endpoint | Method |
| ------------------------------ | ---------------------------------- |
| Create Internal Transfer | `create_internal_transfer()` |
| Create Subaccount Transfer | `create_subaccount_transfer()` |
| Query Transfer List | `query_transfer_list()` |
| Query Subaccount Transfer List | `query_subaccount_transfer_list()` |
| Query Subaccount List | `query_subaccount_list()` |
| Enable Universal Transfer | `enable_universal_transfer()` |
| Create Universal Transfer | `create_universal_transfer()` |
| Query Universal Transfer List | `query_universal_transfer_list()` |
| Query Supported Deposit List | `query_supported_deposit_list()` |
| Query Deposit Records | `query_deposit_records()` |
| Query Withdraw Records | `query_withdraw_records()` |
| Query Coin Info | `query_coin_info()` |
| Query Asset Info | `query_asset_info()` |
| Withdraw | `withdraw()` |
| Cancel Withdrawal | `cancel_withdrawal()` |
| Query Deposit Address | `query_deposit_address()` |

### pybit Custom Endpoints

| Endpoint | Method |
| ------------- | ------------- |
| Place Active Order (Bulk) | `place_active_order_bulk()` |
| Cancel Active Order (Bulk) | `cancel_active_order_bulk()` |
| Place Conditional Order (Bulk) | `place_conditional_order_bulk()` |
| Cancel Conditional Order (Bulk) | `cancel_conditional_order_bulk()` |
| Close Position | `close_position()` |

### WebSocket
be found in the [examples folder](https://github.com/bybit-exchange/pybit/tree/master/examples).

To see comprehensive examples of how to subscribe to the futures and spot websockets, check the [examples file](https://github.com/bybit-exchange/pybit/blob/master/examples/websocket_example.py).

## Contact
You can reach out for support on the [BybitAPI Telegram](https://t.me/BybitAPI) group chat.
Expand Down
Loading

0 comments on commit 1b7162b

Please sign in to comment.