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.