I am trying to build a bot that buys btc/usdt for 50 usdt on bybit using ccxt and python. I also have a stoploss at 0.33% and a take profit at 0.45%. But when I try to run it, I get this message:
"Market order failed with error: bybit {"retCode":170003,"retMsg":"An unknown parameter was sent.","result":{},"retExtInfo":{},"time":1698675395747}"
This is my code:
# Define your Bybit API credentials
api_key = '<Here is my api key>'
api_secret = '<Here is my screct api key>'
# Initialize the Bybit exchange
exchange = ccxt.bybit({
'apiKey': api_key,
'secret': api_secret,
'enableRateLimit': True,
})
# Set the option to allow market buy orders without price
exchange.options["createMarketBuyOrderRequiresPrice"] = False
# Define trading parameters
symbol = 'BTC/USDT'
cost_in_usdt = 50 # The cost of the buy order in USDT
entry_price = None # Market order, so entry price is not specified
stop_loss_percent = 0.0033 # 0.33% stop loss as a float (0.33% = 0.0033)
take_profit = 0.0045 # 0.45% take profit as a float (0.45% = 0.0045)
# Get the current market price
ticker = exchange.fetch_ticker(symbol)
market_price = ticker['last']
# Calculate the quantity of BTC to buy
quantity = cost_in_usdt / market_price
# Place a market order to buy BTC/USDT
try:
market_order = exchange.create_order(
symbol,
'market',
'buy',
quantity
)
print(f"Market order to buy {quantity} BTC placed at market price.")
# Update the entry price based on the market order response
entry_price = market_order['price']
# Calculate stop loss and take profit prices
stop_loss_price = entry_price * (1 - stop_loss_percent)
take_profit_price = entry_price * (1 + take_profit)
# Place a stop market sell order (stop loss)
stop_loss_order = exchange.create_order(
symbol,
'limit',
'sell',
quantity,
stop_loss_price,
{'stopPrice': stop_loss_price}
)
# Place a take profit market sell order
take_profit_order = exchange.create_order(
symbol,
'market',
'sell',
quantity
)
print(f"Stop loss order placed at {stop_loss_price}.")
print(f"Take profit order placed at {take_profit_price}.")
except Exception as e:
print(f"Market order failed with error: {str(e)}")`
I've tried fix it with some other lines of code. I tried to add different parameters, or switch to a limit/market order. But that all can't seem it fix it.
I got the same error code (170003) using pybit module when submitting a limit order with stop loss (I did not use take profit).
What helped me was to provide two additional stoploss arguments:
slTriggerBy="LastPrice"
andslOrderType="Market"
as described in Bybit V5 API documentation.