A Ruby API wrapper for the Kraken cryptocurrency exchange, now featured as one of the libraries in Kraken's API documentation.
Emphasis on speed, simplicity, no meta-programming, and few dependencies.
Kraken Ruby Client:
-
follows X-ISO4217-A3 Internet Standards Draft Registry Contents, an API standards protocol for the open identification of currencies and currency-like commodities on the internet by the Internet Financial EXchange (IFEX) organisation.
-
has only one runtime dependency: The fast Curb (CUrl-RuBy) gem, which provides Ruby bindings for libcurl, a fully-featured client-side URL transfer library written in C.
-
does not use Hashie, to avoid the pain and performance costs of subclassing Hash.
-
uses fast, straightforward, assertions-style Minitest for its test suite.
Currently developed with Ruby 2.7. Compatible with Ruby 2.4 and up.
-
Clone the repository from the command line or add it to your Gemfile.
clone with SSH
git clone [email protected]:jonatack/kraken_ruby_client.git
or clone with HTTPS
git clone https://github.com/jonatack/kraken_ruby_client.git
or add it to your Gemfile
gem 'kraken_ruby_client', github: 'jonatack/kraken_ruby_client'
-
Install from the command line in your local directory.
gem install curb ; bundle install
-
Launch the interactive Ruby shell from the terminal.
$ irb -I lib
or with the rake task provided by this library
$ rake console
Inside the interactive Ruby console, ensure that
require 'curb'
does not raise any errors. If it does, Curb may not be properly installed or your system may be missing necessary dependencies. See the Curb documentation for more info.
require 'kraken_ruby_client'
client = Kraken::Client.new
client.server_time
client.assets
client.assets('USD')
client.assets('xbt,zec,dash,xmr,eth,etc,usd,eur,gbp,jpy')
client.asset_pairs
client.asset_pairs(:zecusd)
client.asset_pairs('xbtusd,etheur')
client.ticker('xbtusd,xmreur,ethgbp,ethjpy')
client.ohlc(:xbtusd)
client.order_book('etheur')
client.trades('DASHXBT')
client.spread(:XMREUR)
client.spread('XBTJPY')
require 'kraken_ruby_client'
client = Kraken::Client.new(api_key: 'YOUR_API_KEY', api_secret: 'YOUR_API_SECRET')
client.balance
client.add_order(pair: 'XBTEUR', type: 'buy', ordertype: 'market', volume: 0.5)
client.open_orders.dig('result', 'open')
# Get OHLC (Open, High, Low, Close) data
# URL: https://api.kraken.com/0/public/OHLC
# Input:
# +pair+ = required asset pair for which to query OHLC data
# +interval+ = optional time frame interval in minutes. Defaults to 1.
# Permitted values: 1, 5, 15, 30, 60, 240, 1440, 10080, 21600
# Returns an Invalid Arguments error for other values.
# +since+ = optional Unix Time from when to return committed OHLC data
#
# Returns a hash with keys `error' and `result'.
# +result+ is an array containing pair name, OHLC data, and last Unixtime.
# The OHLC data array contains:
# time, open, high, low, close, VWAP, price, volume, count.
# The last entry in the OHLC data array is for the current, not-yet-
# committed frame and is always present, regardless of the value of since.
# +last+ is to be used as `since' when getting new committed OHLC data.
client.ohlc('XBTUSD')
client.ohlc('xbtjpy', interval: 5)
client.ohlc('ETHGBP', since: 1548525720)
client.ohlc('xmrusd', interval: 15, since: 1548525720)
client.balance
# Fetch all closed orders and a display a readable summary.
closed_orders = client.closed_orders.dig('result', 'closed') # All closed orders
closed_orders.first # Show the most recent closed order
# Show a readable list of the last 10 closed orders, including canceled ones.
closed_orders.first(10).each { |order| puts "#{order[0]} - #{order[1].dig('descr', 'order')} - #{order[1]['status']}" }
# More elaborate version, with order date/time and improved readability for the
# last 10 closed orders (that were not canceled) of an asset pair.
pair = 'XBTEUR'
closed_orders.select { |_, v| v.dig('descr', 'pair') == pair && v['status'] != 'canceled' }.first(10).each do |order|
action, price, *rest = order[1].dig('descr', 'order').split
puts "#{order[0]} #{Time.at(order[1]['opentm']).utc} " \
"#{action}#{' ' if action.size == 3} #{price[0..4]} #{rest.join(' ')} #{order[1]['status']}"
end
# Fetch all open orders, the most recent open order, and total open order count.
open_orders = client.open_orders.dig('result', 'open') # All open orders
open_orders.first # Most recent open order
open_orders.count # Number of open orders
# List the open orders for an asset pair and the most recent order for the pair.
pair = 'ETCUSD'
open_orders.select { |_, v| v.dig('descr', 'pair') == pair } # All open orders
open_orders.detect { |_, v| v.dig('descr', 'pair') == pair } # Most recent open order
# More elaborate version, with order date and improved readability for the
# last 10 open orders of an asset pair.
open_orders.select { |_, v| v.dig('descr', 'pair') == pair }.first(10).each do |order|
action, price, *rest = order[1].dig('descr', 'order').split
puts "#{order[0]} #{Time.at(order[1]['opentm'])} " \
"#{action}#{' ' if action.size == 3} #{price[0..4]} #{rest.join(' ')}"
end
client.add_order(pair: 'LTCEUR', type: 'buy', ordertype: 'limit', volume: 1, price: 50.5)
client.add_order(pair: 'XBTUSD', type: 'buy', ordertype: 'market', volume: 0.012)
client.add_order(pair: 'BTCUSD, type: 'buy, ordertype: 'market', volume: 0.072, leverage: 2)
client.add_order(pair: 'ZECEUR', type: 'sell', ordertype: 'limit', volume: 1.23, price: 199.43)
client.add_order(pair: 'XMRUSD, type: 'sell', ordertype: 'limit', volume: 256.62)
client.add_order(pair: 'DASHEUR', type: 'sell', ordertype: 'market', volume: 1.53, leverage: 2)
client.cancel_order('TRANSACTION_ID')
client.trade_volume
client.trade_volume(pair: 'XBTEUR, etcusd, xbteth')
client.deposit_methods(asset: 'XXBT')
To run all tests: rake test
or just rake
To run one test file: rake TEST=test/public_api_test
To run an individual test in a test file:
rake TEST=test/public_api_test TESTOPTS=--name=test_get_server_time
To run the linters: rubocop
This will run the linters in the Gemfile: rubocop, rubocop-performance, and rubocop-minitest.
To support the project:
- Use Kraken Ruby Client in your apps, and please file an issue if you encounter anything that's broken or missing. A failing test to demonstrate the issue is awesome. A pull request with passing tests is even better!
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/.
The author may be contacted by email at [email protected].
Copyright © 2016-2021 Jon Atack (@jonatack)