Kucoin API
This is an unofficial Ruby wrapper for the Kucoin exchange REST and WebSocket APIs.
Notice
- This is Alpha software. All issues should be aggressively reported for quick resolution!
- RESTful interface is fully implemented.
- Websocket is fully implemented.
- Pull Requests are very welcome!
- RSPEC tests depend on Ruby >= 2.4 features, specifically
#match?
The gem itself should work find with any Ruby 2.x and higher and perhaps 1.9.x, but has certainly not been tested. YMMV!
Installation
Add this line to your application's Gemfile:
gem 'kucoin-api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install kucoin-api
Features
Current
-
Basic implementation of REST API
- Easy to use authentication
- Methods return parsed JSON
- No need to generate timestamps
- No need to generate signatures
-
Basic implementation of WebSocket API
- Pass procs or lambdas to event handlers
- Single streams supported
- Runs on EventMachine
-
Exception handling with responses
-
High level abstraction
TODO
- Websockets currently uses the first InstanceServer returned in the "Apply connect token" RESTful calls. It is currently not clear what Kucoin intends with multiple InstanceServers, so until a use-case arises or feature/bug request comes along that sheds some light on how to select and use specific servers, the websocket interface is hard-wired to the first instance server returned.
Getting Started
REST Client
Require Kucoin API:
require 'kucoin/api'
Create a new instance of the REST Client:
client = Kucoin::Api::REST.new
client = Kucoin::Api::REST.new api_key: 'your.api_key', api_secret: 'your.api_secret', api_passphrase: 'your.api_passphrase'
client = Kucoin::Api::REST.new sandbox: true
ALTERNATIVELY, set your API key in exported environment variable:
export KUCOIN_API_KEY=your.api_key
export KUCOIN_API_SECRET=your.api_secret
export KUCOIN_API_PASSPHRASE=your.api_passphrase
Then you can instantiate client without parameters as in first variation above.
Create various requests:
client.other.timestamp
client.currency.all
client.market.tick(symbol: 'KCS-BTC')
client.order.create 'KCS-BTC', type: 'BUY', price: 0.000127, amount: 22
client.account.wallet_address('KCS')
Required and optional parameters, as well as enum values, can currently be found on the Kucoin Apiary Page. Parameters should always be passed to client methods as keyword arguments in snake_case form. symbol, when a required parameter is simply passed as first parameter for most API calls.
REST Endpoints
REST endpoints are in order as documented on the Kucoin Apiary page (linked above).
Endpoints are accessible by following resourceful structure given in Kucoin API documentation. For example - user.accounts
is for endpoints given in Kucoin API documentation under "User/Accounts".
The following lists only the method names, aliases (if any) and parameters of the methods to access endpoints.
For the most part, method names follow RESTful action names and alias method follows the title/name given in Kucoin API documentation.
There were some deviations where there would otherwise be name clashes/overloading.
User
Accounts
user.accounts.list options={}
user.accounts.get account_id
- required params: account_id
user.accounts.create currency, type
- required params: currency, type
user.accounts.ledgers account_id, options={}
- required params: account_id
user.accounts.holds account_id
- required params: account_id
user.accounts.inner_transfer client_oid, pay_account_id, rec_account_id, amount
- required params: client_oid, pay_account_id, rec_account_id, amount
Deposits
user.deposits.create currency
- required params: currency
user.deposits.get currency
- required params: currency
user.deposits.list options={}
Withdrawals
user.withdrawals.list options={}
user.withdrawals.quotas currency
- required params: currency
user.withdrawals.apply currency, address, amount, options={}
- required params: currency, address, amount
user.withdrawals.cancel withdrawal_id
- required params: withdrawal_id
Trade
Orders
trade.orders.place client_oid, side, symbol, options={}
- required params: client_oid, side, symbol
trade.orders.cancel order_id
- required params: order_id
trade.orders.cancel_all options={}
trade.orders.list options={}
trade.orders.recent
trade.orders.get order_id
- required params: order_id
Fills
trade.fills.list
trade.fills.recent
Market Data
Symbols & Ticker
markets.all
markets.stats symbol
markets.tickers.all
markets.tickers.inside symbol
markets.symbols.all options={}
Order Book
markets.order_book.part symbol, depth
- required params: symbol, depth(20, 100)
markets.order_book.full_aggregated symbol
markets.order_book.full_atomic symbol
Histories
markets.histories.trade symbol
markets.histories.klines symbol, type, options={}
- required params: symbol, type
Currencies
markets.currencies.all
markets.currencies.detail currency
- required params: currency
markets.currencies.fiat options= {}
Other
Time
other.timestamp
WebSocket Client
Create a new instance of the WebSocket Client:
client = Kucoin::Api::Websocket.new
client = Kucoin::Api::Websocket.new rest_client: Kucoin::Api::REST.new(sendbox: true)
Subscribe various topics:
methods = { message: proc { |event| puts event.data } }
client.ticker symbols: 'ETH-BTC', methods: methods
client.stop_order_received_event symbols: 'BTC-USDT', methods: methods
Multiplex:
channel = nil
message = proc do |event|
puts event.data
data = JSON.parse(event.data)
if data['type'] == 'ack' && data['id'] == '1222'
Kucoin::Api::Websocket.subscribe(channel: channel, params: { topic: "/market/ticker:ETH-BTC", tunnelId: 'bt1' })
end
end
methods = { message: message }
channel = client.multiplex stream: { newTunnelId: 'bt1', id: '1222' }, methods: methods
All subscription topic method will expect "methods" in argument(As shown above).
It's The Hash which contains the event handler methods to pass to the WebSocket client methods.
Proc is the expected value of each event handler key. Following are list of expected event handler keys.
- :open - The Proc called when a stream is opened (optional)
- :message - The Proc called when a stream receives a message
- :error - The Proc called when a stream receives an error (optional)
- :close - The Proc called when a stream is closed (optional)
Websocket Feed
Subscribe topics are in order as documented on the Kucoin Apiary page (linked above).
Public Channels
ticker symbols:, methods:
- required params: symbols(Array/String), methods
all_ticker methods:
snapshot symbol:, methods:
- required params: symbol, methods
- alias methods: symbol_snapshot, market_snapshot
level_2_market_data symbols:, methods:
- required params: symbols(Array/String), methods
match_execution_data symbols:, methods:, private_channel: false
- required params: symbols(Array/String), methods
full_match_engine_data symbols:, methods:, private_channel: false
- required params: symbols(Array/String), methods
Private Channels
stop_order_received_event symbols:, methods:
- required params: symbols(Array/String), methods
- alias methods: stop_order_activate_event
balance methods:
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mwlang/kucoin-api.
Inspiration
The inspiration for architectural layout of this gem comes nearly one-for-one from the Binance gem by craysiii.
License
The gem is available as open source under the terms of the MIT License.