Updating TP/SL of an active position with pybit

huangapple go评论74阅读模式
英文:

Updating TP/SL of an active position with pybit

问题

你好Stack Overflow社区,
希望大家一切都好,过得愉快!

我在Python中使用pybit包遇到了问题,希望这里的某人能够帮助我。

我正在尝试编辑Bybit期货市场上的活动仓位止盈和止损。为此,我正在使用Bybit交易所识别的官方Python包pybit。

我具体使用的是'set_trading_stop'函数,但我一直遇到错误消息。

以下是我用来调用该函数的Python代码:

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

我收到的错误消息如下:

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'}.

我在论坛和Stack Overflow上搜索过,但只找到了关于ccxt包的回答。我更愿意使用pybit包,因为它是Bybit交易所识别的官方包。

pybit包是公共存档,所以我无法在Github上创建问题。因此,我在Stack Overflow上发布了我的问题,希望能找到解决方法。

如果有人有任何想法或建议,我将非常感激!
Greg

英文:

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

答案1

得分: 1

Here is the translated content:

你好,

你可能正在使用较旧版本的pybit库,Bybit已将其REST API更新到版本5,并且方法"https://api.bybit.com/private/linear/position/trading-stop"已被新版本"https://api.bybit.com/v5/position/trading-stop"所替代。

此外,在设置利润目标和止损时,不再传递参数"orderId"到API,而只传递您要调整开仓仓位的交易对。

我对新版本的pybit客户端(实际版本为5.3.0)执行了上述值的测试,止损调整顺利完成。

注意

  • 如果您曾经将止损设置为25,000美元,并再次使用相同的止损值调用该方法,API将返回错误消息"34040 - Set TP/SL/TS not modified"。这是正常的,因为您正在将止损设置为已经设置的相同值。
  • pybit 5.3.0库需要Python 3.9.1及更高版本才能正常运行。

链接

代码示例

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

响应示例

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

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}

huangapple
  • 本文由 发表于 2023年4月19日 15:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定