ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
33.11k stars 7.55k forks source link

Bybit Stop Orders CCXT/API #15869

Open janstrw13 opened 1 year ago

janstrw13 commented 1 year ago

Hello i want to create a Hedge Bot with StopOrders filled when first market order is placed. The Market Order looks good and it works but the Stop Order doesnt.

I want to make one order above actual price going long and one going short under actual price.

e.g. Price= 2000 Now it should create a order that is filled when price is 1800 or 2200

i tried this but it doesnt work HedgePreisShort= 1800 HedgeSize= 0.2 symbol=BTCUSDT HedgeStopLossTrigger= 1600 HedgeTakeProfitTrigger=2000

Hedgeorder=exchange.create_stop_limit_order(symbol=symbol, side='buy', amount=HedgeSize, price=HedgePreisShort, stopPrice=HedgePreisShort, params = {'leverage': 1, 'base_price': HedgePreisShort, 'stopLossPrice': HedgeStopLossTrigger,'takeProfitPrice': HedgeTakeProfitTrigger})

Hope someone can help me!

And maybe you can also tell me how i an cancel this order! With exchange.cancelOrder(Hedgeorder['id'], symbol=symbol, params = {}) ?

Thank you!

carlosmiei commented 1 year ago

Hello @janstrw13, Can you share a verbose log so we can debug it? https://docs.ccxt.com/en/latest/FAQ.html?highlight=get%20help#what-is-required-to-get-help

janstrw13 commented 1 year ago

File "C:\Users\Jan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 2690, in request return self.fetch2(path, api, method, params, headers, body, config, context) File "C:\Users\Jan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 2687, in fetch2 return self.fetch(request['url'], request['method'], request['headers'], request['body']) File "C:\Users\Jan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 662, in fetch self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body) File "C:\Users\Jan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\bybit.py", line 5607, in handle_errors raise ExchangeError(feedback) # unknown message ccxt.base.errors.ExchangeError: bybit {"ret_code":130075,"ret_msg":"expect Falling, but trigger_price[164925000] \u003e= current[164325000]??LastPrice","ext_code":"","ext_info":"","result":null,"time_now":"1669729576.683957","rate_limit_status":98,"rate_limit_reset_ms":1669729576681,"rate_limit":100}

janstrw13 commented 1 year ago

Here is my code for conditional order! cur=exchange.fetch_ticker(symbol) symbolprice=cur['close'] price=cur['close'] HedgeSize=Size SizeMultiplier HedgePreisLong=StopLossTrigger HedgeStopLossTrigger=HedgePreisLong + (StopLoss StopLossMultilplier) HedgeTakeProfitTrigger=HedgePreisLong - (TakeProfit * TakeProfitMultiplier) Hedgeorder=exchange.createOrder(symbol=symbol, type='stop', side='sell', amount=HedgeSize, params = {'stop_px': HedgePreisLong, 'base_price':StopLossTrigger, 'order_type': 'Market', 'close_on_trigger':False, 'stopLossPrice': HedgeStopLossTrigger,'takeProfitPrice': HedgeTakeProfitTrigger}) pprint(Hedgeorder) print(Hedgeorder['id'])

carlosmiei commented 1 year ago

@janstrw13 as you can see in the error message:

r: bybit {"ret_code":130075,"ret_msg":"expect Falling, but trigger_price[164925000] \u003e= current[164325000]??

⬆️ trigger_price < current price when it should be the opposite for this type of order

janstrw13 commented 1 year ago

Okay but that is trigger_price > current price

anyone knows how to fix this? That i can create a stop limit order with ccxt?

janstrw13 commented 1 year ago

fetch Response: bybit GET https://api.bybit.com/public/linear/kline?symbol=BTCUSDT&limit=9&interval=60&from=1669705717 200 ResponseHeaders: {'Content-Type': 'application/json; charset=utf-8', 'Server': 'Openresty', 'Access-Control-Allow-Origin': '', 'Access-Control-Allow-Methods': '', 'Access-Control-Max-Age': '5', 'Access-Control-Expose-Headers': '*', 'Access-Control-Allow-Headers': 'X-BAPI-API-KEY,X-BAPI-TIMESTAMP,X-BAPI-RECV-WINDOW,X-BAPI-SIGN,X-BAPI-MAKER-API-KEY,X-BAPI-MAKER-SIGN,X-Referer,Content-Type', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Date': 'Tue, 29 Nov 2022 16:08:37 GMT', 'Content-Length': '568', 'Connection': 'keep-alive'} ResponseBody: {"ret_code":0,"ret_msg":"OK","ext_code":"","ext_info":"","result":[{"id":156502986,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669708800,"open_time":1669708800,"volume":7510.687,"open":16455,"high":16547,"low":16425.5,"close":16490.5,"turnover":123837719.5095},{"id":156521668,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669712400,"open_time":1669712400,"volume":2484.847,"open":16490.5,"high":16516,"low":16471,"close":16511,"turnover":40967635.556},{"id":156540273,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669716000,"open_time":1669716000,"volume":3876.051,"open":16511,"high":16525,"low":16478,"close":16488,"turnover":63943511.403},{"id":156558793,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669719600,"open_time":1669719600,"volume":3119.837,"open":16488,"high":16533,"low":16476.5,"close":16482.5,"turnover":51470098.3085},{"id":156577508,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669723200,"open_time":1669723200,"volume":2333.91,"open":16482.5,"high":16494,"low":16457.5,"close":16473.5,"turnover":38447352.1445},{"id":156596598,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669726800,"open_time":1669726800,"volume":7965.445,"open":16473.5,"high":16477,"low":16376.5,"close":16403.5,"turnover":130776651.5175},{"id":156615080,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669730400,"open_time":1669730400,"volume":12800.237,"open":16403.5,"high":16450,"low":16320,"close":16355.5,"turnover":209862979.6465},{"id":156633637,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669734000,"open_time":1669734000,"volume":7048.642,"open":16355.5,"high":16448,"low":16333.5,"close":16386.5,"turnover":115517165.2565},{"id":156652303,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669737600,"open_time":1669737600,"volume":724.259,"open":16386.5,"high":16386.5,"low":16361,"close":16372.5,"turnover":11858221.929}],"time_now":"1669738117.173093"}

fetch Request: bybit GET https://api.bybit.com/v2/public/tickers?symbol=BTCUSDT RequestHeaders: {'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'} RequestBody: None

fetch Response: bybit GET https://api.bybit.com/v2/public/tickers?symbol=BTCUSDT 200 ResponseHeaders: {'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '756', 'Ret_code': '0', 'Traceid': '3b76a63aa8ff34d90208674afb6fbe72', 'Timenow': '1669738117510', 'Server': 'Openresty', 'Expires': 'Tue, 29 Nov 2022 16:08:37 GMT', 'Cache-Control': 'max-age=0, no-cache, no-store', 'Pragma': 'no-cache', 'Date': 'Tue, 29 Nov 2022 16:08:37 GMT', 'Connection': 'keep-alive'} ResponseBody: {"ret_code":0,"ret_msg":"OK","result":[{"symbol":"BTCUSDT","bid_price":"16372","ask_price":"16372.5","last_price":"16372.50","last_tick_direction":"ZeroPlusTick","prev_price_24h":"16095.50","price_24h_pcnt":"0.017209","high_price_24h":"16547.00","low_price_24h":"15985.00","prev_price_1h":"16378.50","mark_price":"16372.50","index_price":"16379.99","open_interest":52603.033,"countdown_hour":0,"turnover_24h":"2383453787.0140047","volume_24h":145977.087,"funding_rate":"0.000003","predicted_funding_rate":"","next_funding_time":"2022-11-30T00:00:00Z","predicted_delivery_price":"","total_turnover":"","total_volume":0,"delivery_fee_rate":"","delivery_time":"","price_1h_pcnt":"","open_value":""}],"ext_code":"","ext_info":"","time_now":"1669738117.510928"} Warte auf TP = 16372.5

fetch Request: bybit GET https://api.bybit.com/public/linear/kline?symbol=BTCUSDT&limit=9&interval=60&from=1669705717 RequestHeaders: {'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'} RequestBody: None

fetch Response: bybit GET https://api.bybit.com/public/linear/kline?symbol=BTCUSDT&limit=9&interval=60&from=1669705717 200 ResponseHeaders: {'Content-Type': 'application/json; charset=utf-8', 'Server': 'Openresty', 'Access-Control-Allow-Origin': '', 'Access-Control-Allow-Methods': '', 'Access-Control-Max-Age': '5', 'Access-Control-Expose-Headers': '*', 'Access-Control-Allow-Headers': 'X-BAPI-API-KEY,X-BAPI-TIMESTAMP,X-BAPI-RECV-WINDOW,X-BAPI-SIGN,X-BAPI-MAKER-API-KEY,X-BAPI-MAKER-SIGN,X-Referer,Content-Type', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Date': 'Tue, 29 Nov 2022 16:08:37 GMT', 'Content-Length': '568', 'Connection': 'keep-alive'} ResponseBody: {"ret_code":0,"ret_msg":"OK","ext_code":"","ext_info":"","result":[{"id":156502986,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669708800,"open_time":1669708800,"volume":7510.687,"open":16455,"high":16547,"low":16425.5,"close":16490.5,"turnover":123837719.5095},{"id":156521668,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669712400,"open_time":1669712400,"volume":2484.847,"open":16490.5,"high":16516,"low":16471,"close":16511,"turnover":40967635.556},{"id":156540273,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669716000,"open_time":1669716000,"volume":3876.051,"open":16511,"high":16525,"low":16478,"close":16488,"turnover":63943511.403},{"id":156558793,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669719600,"open_time":1669719600,"volume":3119.837,"open":16488,"high":16533,"low":16476.5,"close":16482.5,"turnover":51470098.3085},{"id":156577508,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669723200,"open_time":1669723200,"volume":2333.91,"open":16482.5,"high":16494,"low":16457.5,"close":16473.5,"turnover":38447352.1445},{"id":156596598,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669726800,"open_time":1669726800,"volume":7965.445,"open":16473.5,"high":16477,"low":16376.5,"close":16403.5,"turnover":130776651.5175},{"id":156615080,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669730400,"open_time":1669730400,"volume":12800.237,"open":16403.5,"high":16450,"low":16320,"close":16355.5,"turnover":209862979.6465},{"id":156633637,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669734000,"open_time":1669734000,"volume":7048.642,"open":16355.5,"high":16448,"low":16333.5,"close":16386.5,"turnover":115517165.2565},{"id":156652303,"symbol":"BTCUSDT","period":"60","interval":"60","start_at":1669737600,"open_time":1669737600,"volume":724.259,"open":16386.5,"high":16386.5,"low":16361,"close":16372.5,"turnover":11858221.929}],"time_now":"1669738117.173093"}

fetch Request: bybit GET https://api.bybit.com/v2/public/tickers?symbol=BTCUSDT RequestHeaders: {'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'} RequestBody: None

fetch Response: bybit GET https://api.bybit.com/v2/public/tickers?symbol=BTCUSDT 200 ResponseHeaders: {'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '753', 'Ret_code': '0', 'Traceid': '50af5dd5d2720b58148206eeb1f4c883', 'Timenow': '1669738117832', 'Server': 'Openresty', 'Expires': 'Tue, 29 Nov 2022 16:08:37 GMT', 'Cache-Control': 'max-age=0, no-cache, no-store', 'Pragma': 'no-cache', 'Date': 'Tue, 29 Nov 2022 16:08:37 GMT', 'Connection': 'keep-alive'} ResponseBody: {"ret_code":0,"ret_msg":"OK","result":[{"symbol":"BTCUSDT","bid_price":"16372","ask_price":"16372.5","last_price":"16372.00","last_tick_direction":"MinusTick","prev_price_24h":"16095.50","price_24h_pcnt":"0.017178","high_price_24h":"16547.00","low_price_24h":"15985.00","prev_price_1h":"16378.50","mark_price":"16372.50","index_price":"16379.99","open_interest":52603.033,"countdown_hour":0,"turnover_24h":"2383458845.9675045","volume_24h":145977.396,"funding_rate":"0.000003","predicted_funding_rate":"","next_funding_time":"2022-11-30T00:00:00Z","predicted_delivery_price":"","total_turnover":"","total_volume":0,"delivery_fee_rate":"","delivery_time":"","price_1h_pcnt":"","open_value":""}],"ext_code":"","ext_info":"","time_now":"1669738117.832089"}

Now it works but i get this error...