Reputation: 2624
I want to test some codes for placing short-open orders using pybit. My code is as follows.
from pybit import *
session = usdt_perpetual.HTTP(endpoint = "https://api-testnet.bybit.com",
api_key = 'yyyyy',
api_secret = 'xxxxxx')
symbol = "HIGHUSDT"
resp = session.place_active_order(
symbol=symbol,
side="Sell",
order_type="Market",
qty= 10,
time_in_force="GoodTillCancel",
reduce_only=False,
close_on_trigger=False
)
It makes the following Error.
InvalidRequestError: Position idx not match position mode (ErrCode: 130001) (ErrTime: 01:35:30).
Request → POST https://api-testnet.bybit.com/private/linear/order/create: {'api_key': 'xxxxxxxx', 'close_on_trigger': False, 'order_type': 'Market', 'qty': 10, 'recv_window': 5000, 'reduce_only': False, 'side': 'Buy', 'symbol': 'HIGHUSDT', 'time_in_force': 'GoodTillCancel', 'timestamp': 1679362530091, 'sign': '64632a4ed529118f8516eb01294deec4036289ae835326eb24abef6b1ab8b813'}.
What is wrong and how can I fix it? I changed qty and order_type, but it does not help.
Upvotes: 0
Views: 2120
Reputation: 56
Apparently, you use hedge mode for the futures contract, so the error is that the "positionIdx" parameter is not set. I also recommend upgrading to the latest version of the pybit library, because the code above shows that you are still using the v3 REST API, but there is a newer version REST API v5.
There are two solutions:
What is Hedge mode?
PositionIdx parameter setting rules
Used to identify positions in different position modes. Under hedge-mode, this param is required
0: one-way mode
1: hedge-mode Buy side
2: hedge-mode Sell side
Code example for hedge mode
from pybit.unified_trading import HTTP
class UpdatingTakeProfitAndStopLossActivePosition(unittest.TestCase):
def test_place_active_order(self):
pybit_client = HTTP(
testnet=True,
api_key="",
api_secret=""
)
response = pybit_client.place_order(
category="linear",
symbol="HIGHUSDT",
side="Sell",
orderType="Market",
qty="10",
timeInForce="GTC",
reduceOnly=False,
closeOnTrigger=False,
positionIdx=2
)
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': {'orderId': '0dab0e8c-7468-405a-9905-4dfc1f0ddf92', 'orderLinkId': ''}, 'retExtInfo': {}, 'time': 1685470503583}
Links
Upvotes: 1