5

I try creating a sample trading bot using python with Bybit API. it is working when I creating position but when I trying close the position it sending me error because when i closing position I cannot using "Market" order. I am searching on Bybit docs but I cannot see this point.

let me explain:

it my opening position code

open_position = client.place_active_order(symbol="BTCUSDT",
                side="Buy",
                order_type="Market",
                qty= 0.001,
                time_in_force="GoodTillCancel",
                reduce_only=False,
                close_on_trigger=False,)

And I try closing position with this code:

last_price = client.latest_information_for_symbol(symbol="BTCUSDT")["result"][0]["last_price"]

# close position
close_position = client.set_trading_stop(
                 symbol="BTCUSDT",
                 side="Buy",
                 take_profit=last_price)

this code is working in some cases but generally I getting this error:

InvalidRequestError(
pybit.exceptions.InvalidRequestError: Takeprofit:445890000 set for buy position should be higher 
than base_price:445895000??lastprice (ErrCode: 130027)

I think, the last_price is not equal to BTCUSDT parity price when I running order to take_profit = last_price

for example, my last_price code getting BTCUSDT= 40000 but BTCUSDT went up 40100 when my code until place an order.

So, How can I closing position with using "Market" order?

The Bybit documantation is here Bybit Api documantation

Thanks.

Turing
  • 124
  • 1
  • 7

1 Answers1

5

I've been wrestling with the same issue until I realized that the actual way to close trades on ByBit is to use the reduce_only option with the opposite side to with which the trade was opened:

client.place_active_order(symbol="BTCUSDT",
                side="Sell",
                order_type="Market",
                qty= 0.001,
                time_in_force="GoodTillCancel",
                reduce_only=True
                close_on_trigger=False,)
Zathrus Writer
  • 4,311
  • 5
  • 27
  • 50