Code Monkey home page Code Monkey logo

bitfinex-api-rb's Introduction

Bitfinex Trading API for Ruby - Bitcoin, Ethereum, Ripple and more

Code Climate

A Ruby reference implementation of the Bitfinex REST & WebSocket APIs.

This repo is primarily made up of 3 classes: RESTv1, RESTv2, and WSv2, which implement their respective versions of the Bitfinex API. It is recommended that you use the REST APIs for reading data, and the WebSocket API for submitting orders and interacting with the Bitfinex platform.

Check the Bitfinex API documentation for more information.

Features

  • Official implementation
  • REST API v1
  • REST API v2
  • WebSockets API version 2

Installation

Add this line to your application's Gemfile:

gem 'bitfinex-rb', :require => "bitfinex"

And then execute:

bundle

Or install it yourself as:

gem install bitfinex-rb

Quickstart

client = Bitfinex::WSv2.new({
  :api_key => ENV['API_KEY'],
  :api_secret => ENV['API_SECRET'],
  :transform => true, # provide models as event data instead of arrays
})

client.on(:open) do
  client.auth!
end

client.on(:auth) do
  puts 'succesfully authenticated'

  o = Bitfinex::Models::Order.new({
    :type => 'EXCHANGE LIMIT',
    :price => 3.0152235,
    :amount => 2.0235235263262,
    :symbol => 'tEOSUSD'
  })

  client.submit_order(o)
end

Docs

Refer to docs/events.md for a list of available events which can be consumed. Official API docs pending.

For ready to run examples, see the examples/ folder.

Examples

Usage of RESTv1/RESTv2

To use the REST APIs, construct a new API client with your account credentials:

client = Bitfinex::RESTv2.new({
  :api_key => '...',
  :api_secret => '...',
})

Then use it to submit queries, i.e. client.balances

Usage of WSv2

To use version 2 of the WS API, construct a new client with your credentials, bind listeners to react to stream events, and open the connection:

client = Bitfinex::WSv2.new({
  :url => ENV['WS_URL'],
  :api_key => ENV['API_KEY'],
  :api_secret => ENV['API_SECRET'],
  :transform => true, # provide models as event data instead of arrays
  :seq_audit => true, # enable and audit sequence numbers
  :manage_order_books => true, # allows for OB checksum verification
  :checksum_audit => true # enables OB checksum verification (needs manage_order_books)
})

client.on(:open) do
  client.auth!
end

client.on(:auth) do
  puts 'succesfully authenticated'

  o = Bitfinex::Models::Order.new({
    :type => 'EXCHANGE LIMIT',
    :price => 3.0152235,
    :amount => 2.0235235263262,
    :symbol => 'tEOSUSD'
  })

  client.submit_order(o)
end

client.on(:notification) do |n|
  puts 'received notification: %s' % [n]
end

client.on(:order_new) do |msg|
  puts 'recv order new: %s' % [msg]
end

client.open!

Order Manipulation

Three methods are provided for dealing with orders: submit_order, update_order and cancel_order. All methods support callback blocks, which are triggered upon receiving the relevant confirmation notifications. Example:

o = Bitfinex::Models::Order.new({
  :type => 'EXCHANGE LIMIT',
  :price => 3.0152235,
  :amount => 2.0235235263262,
  :symbol => 'tEOSUSD'
})

client.submit_order(o) do |order_packet|
  p "recv order confirmation packet with ID #{order_packet.id}"

  client.update_order({
    :id => order_packet.id,
    :price => '3.0'
  }) do |update_packet|
    p "updated order #{update_packet.id} with price #{update_packet.price}"

    client.cancel_order(order_packet) do |canceled_order|
      p "canceled order with ID #{canceled_order[0]}"
    end
  end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

bitfinex-api-rb's People

Contributors

arvicco avatar clr-xdat avatar erubboli avatar f3rno avatar face avatar moaa avatar prdn avatar stragasevera avatar unrealhoang avatar

Watchers

 avatar

Forkers

bitbd83

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.