Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Issue: Pybit/Bybit API failed to configure a series of leverage value when risk ID is highest #76

Open
viper7882 opened this issue Dec 30, 2021 · 0 comments

Comments

@viper7882
Copy link

viper7882 commented Dec 30, 2021

Hi Pybit/Bybit API Admin,

We've noticed a strange behavior in Pybit/Bybit API where certain range of leverage value is constantly rejected by Bybit API despite the new leverage value is different from the existing leverage value. We believe it is an issue Pybit/Bybit API team is interested to look into.

Below is the code to reproduce the issue:

import pybit
from pybit import HTTP
import sys
import time

if __name__ == "__main__":
    mainnet_url = "https://api.bybit.com"
    testnet_url = "https://api-testnet.bybit.com"

    symbol_name = "UNI/USDT"
    symbol_id = symbol_name.replace("/", "")

    # Unauthenticated
    session_unauth = HTTP(endpoint=testnet_url)

    # Authenticated
    session_auth = HTTP(
        endpoint=testnet_url,
        api_key=api_key,
        api_secret=api_secret,
    )

    '''
    Reference: https://help.bybit.com/hc/en-us/articles/360039749753-Risk-Limit-Inverse-Contract-
    Reference: https://help.bybit.com/hc/en-us/articles/900000170023-Risk-Limit-USDT-Contract-
    Reference: https://bybit-exchange.github.io/docs/inverse/?python#t-querysymbol
    Reference: https://bybit-exchange.github.io/docs/linear/#t-querysymbol
    '''
    response = session_auth.query_symbol(
        symbol=symbol_id
    )

    min_leverage = None
    leverage_step = None
    for result in response['result']:
        if result['name'] != symbol_id:
            continue
        min_leverage = float(result['leverage_filter']['min_leverage'])
        leverage_step = float(result['leverage_filter']['leverage_step'])
    assert min_leverage is not None
    assert leverage_step is not None

    print("symbol: {}, min_leverage: {}, leverage_step: {}".format(
        symbol_id, min_leverage, leverage_step,
    ))

    response = session_auth.get_risk_limit(
        symbol=symbol_id
    )

    lowest_risk_id = response['result'][0]['id']
    highest_risk_id = response['result'][-1]['id']
    lowest_risk_max_leverage = response['result'][0]['max_leverage']
    highest_risk_max_leverage = response['result'][-1]['max_leverage']
    print("symbol: {}, lowest_risk_id: {}, lowest_risk_max_leverage: {}, highest_risk_id: {}, highest_risk_max_leverage: {}".format(
        symbol_id, lowest_risk_id, lowest_risk_max_leverage, highest_risk_id, highest_risk_max_leverage,
    ))

    '''
    Reference: https://bybit-exchange.github.io/docs/inverse/#t-myposition
    '''
    position_response = session_auth.my_position(
        symbol=symbol_id
    )
    for result in position_response['result']:
        side = result['side']
        leverage = result['leverage']

        print("symbol: {}, side: {}, leverage: {}, get risk_id: {}".format(
            result['symbol'], side, leverage, result['risk_id']
        ))
        current_risk_id = int(result['risk_id'])
        if current_risk_id != highest_risk_id:
            '''
            Reference: https://bybit-exchange.github.io/docs/inverse/#t-setrisklimit
            '''
            risk_limit_response = session_auth.set_risk_limit(symbol=symbol_id, side=side, risk_id=highest_risk_id)
            print("symbol: {}, side: {}, set risk_id: {}".format(
                symbol_id, side, risk_limit_response['result']['risk_id']
            ))

    # Convert to integer as Python range only accept integer value
    int_min_leverage = int(min_leverage * 100)
    int_highest_risk_max_leverage = int(highest_risk_max_leverage * 100)
    int_leverage_step = int(leverage_step * 100)

    for int_new_leverage in range(int_min_leverage, int_highest_risk_max_leverage, int_leverage_step):
        # Convert from integer back to float
        float_new_leverage = float(int_new_leverage / 100)
        try:
            '''
            Reference: https://bybit-exchange.github.io/docs/linear/#t-setleverage
            '''
            response = session_auth.set_leverage(
                    symbol=symbol_id,
                    buy_leverage=float_new_leverage,
                    sell_leverage=float_new_leverage,
            )
        except pybit.exceptions.InvalidRequestError as e:
            print("symbol: {}, configuring leverage: {} failed with: {}".format(
                symbol_id, float_new_leverage, e.message,
            ))

        time.sleep(1)

Sample output screen:

symbol: UNIUSDT, min_leverage: 1.0, leverage_step: 0.01
symbol: UNIUSDT, lowest_risk_id: 146, lowest_risk_max_leverage: 25, highest_risk_id: 160, highest_risk_max_leverage: 5.56
symbol: UNIUSDT, side: Buy, leverage: 4.01, get risk_id: 160
symbol: UNIUSDT, side: Sell, leverage: 4.01, get risk_id: 160
symbol: UNIUSDT, configuring leverage: 1.13 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.01 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.03 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.05 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.07 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.26 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.28 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.3 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.32 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.51 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.53 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 2.55 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.02 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.06 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.1 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.14 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.27 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.31 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.35 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.39 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.52 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.56 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.6 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.64 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.77 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.81 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.85 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 4.89 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 5.02 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 5.06 failed with: leverage not modified
symbol: UNIUSDT, configuring leverage: 5.1 failed with: leverage not modified

It seems like quite a long list of failure here. Hence, thought process have been considered to first post the issue here prior to Bybit API.

Please allow us to summarize the code logic here:

  1. The risk limit is configured to highest number.
  2. Swept the range of leverage from min_leverage to max_leverage using leverage_step. All of these values are data driven from Pybit API. There is no hardcoded value here.
  3. Noticed the leverage failed to be configured at long list of values above.

Hypothesis
There seems to be some kind of previous leverage versus new leverage checking failed behind Pybit/Bybit API. When the max_leverage is 5.56, the precision on the leverage_step is crucial when comparing previous leverage versus new leverage value. We suspect it is related to precision of the checking performed. It is not clear to us how these values are related to each other but chances are they are related to precision that do not correlate to leverage_step value. Naturally, Bybit API team should be in better position to confirm and comment than us.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant