Beeflight31
Beeflight31

Reputation: 249

Updating TP/SL of an active position with pybit

Hello Stack Overflow community, I hope you're all doing well and having a great day!

I'm having an issue with the pybit package in Python, and I was hoping that someone here might be able to help me out.

I am trying to edit the take profit and stop loss of an active position on Bybit futures markets. To do so, I'm using the official Python package identified by the Bybit exchange, pybit.

I'm specifically using the 'set_trading_stop' function, but I keep encountering an error message.

Here is the Python code that I am using to call the function:

session_auth.set_trading_stop(orderId=myorderid, symbol=crypto, stopLoss='25000', category='linear', positionIdx=0, slTriggerBy='MarkPrice')

The error message that I get is the following:

InvalidRequestError: Not modified (ErrCode: 130127) (ErrTime: 06:43:33).
Request → POST https://api.bybit.com/private/linear/position/trading-stop: {'api_key': 'xxx', 'category': 'linear', 'orderId': '67bf2151-1f30-4b33-b2b8-332023a33f2c', 'positionIdx': 0, 'recv_window': 5000, 'slTriggerBy': 'MarkPrice', 'stopLoss': '25000', 'symbol': 'BTCUSDT', 'timestamp': 1681886613451, 'sign': 'xxx'}.

I have searched on forums and Stack Overflow, but I have only found responses for the ccxt package. I would prefer to use the pybit package, as it is the official package identified by the Bybit exchange.

The pybit package is a public archive, so I am unable to create an issue on Github. Therefore, I am posting my issue here on Stack Overflow in the hopes of finding a solution.

If anyone has any ideas or suggestions, I would greatly appreciate it! Greg

Upvotes: 0

Views: 1819

Answers (1)

Jiří Cága
Jiří Cága

Reputation: 56

Good day,

You are probably using an older version of the pybit library, Bybit has updated its rest api to version 5 and the method "https://api.bybit.com/private/linear/position/trading-stop" has been replaced by the newer "https://api.bybit.com/v5/position/trading-stop".

Further, when setting the profit target and stop loss, the parameter "orderId" is not pass on the api, but only the symbol on which you want to adjust the open position.

I performed testing on new version pybit client (actually version 5.3.0) for the above values and the stop loss adjustment went smoothly.

Notes:

  • If you once set the stop loss to the value of 25,000 usd, and call the method again with the same stop loss value, the api returns the error message "34040 - Set TP/SL/TS not modified". This is fine, because you are setting the stop loss to the same value that is already set.
  • The pybit 5.3.0 library requires Python 3.9.1 and higher to function properly.

Links

Code example

from pybit.unified_trading import HTTP

class UpdatingTakeProfitAndStopLossActivePosition(unittest.TestCase):

    def test_update_stop_loss_active_position(self):
        pybit_client = HTTP(
            testnet=True,
            api_key="",
            api_secret=""
        )

        response = pybit_client.set_trading_stop(
            category="linear",
            symbol="BTCUSDT",
            stopLoss="25000",
            slTriggerBy="MarkPrice",
            positionIdx=0
        )

        print("Response: {}".format(response))

        self.assertEqual(response["retCode"], 0)
        self.assertEqual(response["retMsg"], "OK")


requirements.txt

pybit==5.3.0

Response example

{'retCode': 0, 'retMsg': 'OK', 'result': {}, 'retExtInfo': {}, 'time': 1685467952890}

Upvotes: 2

Related Questions