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
32.93k stars 7.51k forks source link

CCXT supports new Bybit API v5 accounts as of version 2.8.61 #17013

Closed kroitor closed 1 year ago

kroitor commented 1 year ago

Hello, dear CCXT users!

We would like to announce that the Bybit v5 integration has been merged in CCXT as of version 2.8.61.

Users with all types of accounts including unified v5 accounts, unified margin accounts, and regular accounts can use Bybit from CCXT out of the box as CCXT can now detect the types of Bybit accounts automatically, so, it should just work.

As always, feel free to report any difficulties and/or issues you might find after the upgrade.

Thank you for using CCXT!

Yours, the CCXT Dev Team

furryboffin commented 1 year ago

Do we still need to set "enableUnifiedMargin" to "true" ?

carlosmiei commented 1 year ago

Do we still need to set "enableUnifiedMargin" to "true" ?

@furryboffin By default CCXT will make one request to figure out your account type.

furryboffin commented 1 year ago

I've noticed that not all markets listed in bybit.markets are available as tickers for the websocket. Is this expected?

carlosmiei commented 1 year ago

I've noticed that not all markets listed in bybit.markets are available as tickers for the websocket. Is this expected?

@furryboffin Which ones are not available? (we didn't update the WS yet so yes we might have some discrepancies)

furryboffin commented 1 year ago

There are a lot... to many to list here easily/quickly. Also, I just tried to place my first trade using this new version and a unified account. I want to trade on BTCUSDT perp... how do I inform CCXT to place the order on futures market and not on spot? It's try to place it on spot with the amount specified in BTC... and fails because the amount is blow the minimum, which I imagine is minimum USDT value. E.G. I used to place futures USDT trades using the base currency amount, in this case BTC. So I want to go long 0.01BTC, I set the amount to 0.01 and it tries to place that on spot and fails due to min amount requirement.

DEBUG DEBUG DEBUG CCXT createUnifiedAccountOrder() (Note I added this debug to know which method was being called. This looks like the correct method, but it seems that it still tries to place the order on spot. )
fetch Request:
 bybit POST https://api.bybit.com/v5/order/create 
RequestHeaders:
 {
  'Content-Type': 'application/json',
  'X-BAPI-API-KEY': 'xxxx',
  'X-BAPI-TIMESTAMP': '1677768871422',
  'X-BAPI-RECV-WINDOW': '5000',
  'X-BAPI-SIGN': 'xxxx',
  Referer: 'CCXT'
} 
RequestBody:
 {"symbol":"BTCUSDT","side":"Buy","orderType":"Market","qty":"0.01567","category":"spot"} 

handleRestResponse:
 bybit POST https://api.bybit.com/v5/order/create 200 OK 
ResponseHeaders:
 {
  'Cache-Control': 'no-store',
  Connection: 'keep-alive',
  'Content-Length': '112',
  'Content-Type': 'application/json; charset=utf-8',
  Date: 'Thu, 02 Mar 2023 14:54:31 GMT',
  Ret_code: '170140',
  Server: 'Openresty',
  Timenow: '1677768871537',
  Traceid: '23fa8978a5866188500a65ec3ffbd581',
  Via: '1.1 87bf84f333bc8ae1d8c723bf1e035c1e.cloudfront.net (CloudFront)',
  'X-Amz-Cf-Id': 'hl1YVT7hmJh6r1nHDiwl_A8gH8NH7eyEUqIyGUY7PBMdMG3kydiwMQ==',
  'X-Amz-Cf-Pop': 'IAD12-P2',
  'X-Bapi-Limit': '20',
  'X-Bapi-Limit-Reset-Timestamp': '1677768871532',
  'X-Bapi-Limit-Status': '19',
  'X-Cache': 'Miss from cloudfront'
} 
ResponseBody:
 {"retCode":170140,"retMsg":"Order value exceeded lower limit.","result":{},"retExtInfo":{},"time":1677768871537} 

here is the market json that I see when I place the order:

mount":0.000001,"price":0.01},"limits":{"amount":{"min":0.000048,"max":46.13},"price":{},"cost":{"min":1,"max":938901},"leverage":{"min":1}},"info":{"symbol":"BTCUSDT","baseCoin":"BTC","quoteCoin":"USDT","innova
tion":"0","status":"1","lotSizeFilter":{"basePrecision":"0.000001","quotePrecision":"0.00000001","minOrderQty":"0.000048","maxOrderQty":"46.13","minOrderAmt":"1","maxOrderAmt":"938901"},"priceFilter":{"tickSize"
:"0.01"}},"tierBased":true,"percentage":true,"taker":0.001,"maker":0.001,"feeSide":"get"}

So one can see it is a spot market, but I've no clue how I"m supposed to inform CCXT that I want to trade on futures with the same ticker.

furryboffin commented 1 year ago

I tried the following:

 bybit = new ccxt.bybit({defaultType: 'future', defaultSubType: 'linear', enableRateLimit:true})

to no avail.

furryboffin commented 1 year ago

After a little more digging, I see that the list of markets that I'm getting is always that of spot. Not one market is future.

furryboffin commented 1 year ago

After even more digging I discovered that you can get a full list of markets by calling fetchMarkets() then filter out those that are linear swap etc. Now I'm able to subscribe to all markets. I still have not yet figured out how to place a trade on linear swap instead of spot yet though.

@carlosmiei could you point me to documentation that explains how to define which market I want to use when placing orders?

furryboffin commented 1 year ago

I think I figured it out. Correct me if I'm wrong, but it seems to just depend on the symbol that is used. So, one can trade on spot or swap(linear/inverse) all with the same ccxt object, just passing the correct ticker i.e. "BTC/USDT:USDT" for linear swap, "BTC/USDT" would be spot and "BTC/USD" would be inverse swap.

kroitor commented 1 year ago

I think I figured it out. Correct me if I'm wrong, but it seems to just depend on the symbol that is used. So, one can trade on spot or swap(linear/inverse) all with the same ccxt object, just passing the correct ticker i.e. "BTC/USDT:USDT" for linear swap, "BTC/USDT" would be spot and "BTC/USD" would be inverse swap.

Yes, you're almost entirely correct, except that the inverse swap would be "BTC/USD:BTC", please, check these contract naming conventions:

wunderb1t commented 1 year ago

Hi, Is there any possible way to load only linear swap markets via loadMarkets method? Because right now loadMarkets method loads all markets (spot and derivatives) and in result we got redundant requests. Will be handy if in bybit class options parameters can be provided 'fetchMarkets' option like it is implemented in binance.

carlosmiei commented 1 year ago

Hi, Is there any possible way to load only linear swap markets via loadMarkets method? Because right now loadMarkets method loads all markets (spot and derivatives) and in result we got redundant requests. Will be handy if in bybit class options parameters can be provided 'fetchMarkets' option like it is implemented in binance.

Hello @wunderb1t, No by default bybit can't opt out market types, but loadMarkets only runs once and caches the result so performance wise it shouldn't make much difference

kjnh10 commented 1 year ago

How can I create_order for margin trading? When I specify "BTC/USDT", it leads to trading spot pair.

frosty00 commented 1 year ago

@kjnh10 you can use bybit.borrowMargin and bybit.repayMargin and then make spot orders as normal using the borrowed margin

kjnh10 commented 1 year ago

@frosty00

With unified accout, I got this error message. ccxt.base.errors.ExchangeError: bybit {"retCode":12634,"retMsg":"Account has been upgraded (upgrading) to UTA","result":{},"retExtInfo":{},"time":1681209532437}