Skip to content

hackhowtofaq/binance

Repository files navigation

Binance

Binance Futures API Documentation

Please check CHANGELOG for more info

[2021 - ] Add futures API (WIP)

[NEW] I tried to implement a reconnect mechanism without resetting the instance variables

Refs:

[1] faye ws

[2] reconnect snippet

[3] post_init

This is an unofficial Ruby wrapper for the Binance exchange REST and WebSocket APIs.

Notice

We are now at 1.0 and there are breaking changes, mainly with some method names and the casing of keys. Be sure to check out the code while I work on better documentation.

Installation

Add this line to your application's Gemfile:

gem'binance'

And then execute:

$ bundle 

Or install it yourself as:

$ gem install binance 

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 and multiple streams supported
    • Runs on EventMachine

Planned

  • Futures API (WIP)
  • Exception handling with responses
  • High level abstraction

Getting Started SPOT trading

REST Client

Require Binance:

require'binance'

Create a new instance of the REST Client:

# If you only plan on touching public API endpoints, you can forgo any argumentsclient=Binance::Client::REST.new# Otherwise provide an api_key and secret_key as keyword argumentsclient=Binance::Client::REST.newapi_key: 'x',secret_key: 'y'

Create various requests:

# Ping the serverclient.ping# =>{}# Get kline dataclient.klinessymbol: 'NEOETH',interval: '1m',limit: 1# => [[1511682480000, "0.08230000", "0.08230000", "0.08230000", "0.08230000", "0.00000000", # 1511682539999, "0.00000000", 0, "0.00000000", "0.00000000", "2885926.46000000"]]# Create an orderclient.create_order!symbol: 'XRPETH',side: 'BUY',type: 'LIMIT',time_in_force: 'GTC',quantity: '100.00000000',price: '0.00055000'# =>{"symbol"=>"XRPETH", "orderId"=>918248, "clientOrderId"=>"kmUU0i6cMWzq1NElE6ZTdu", # "transactTime"=>1511685028420, "price"=>"0.00055000", "origQty"=>"100.00000000", # "executedQty"=>"100.00000000", "status"=>"FILLED", "timeInForce"=>"GTC", "type"=>"LIMIT", # "side"=>"BUY"}# Get deposit addressclient.deposit_addressasset: 'NEO'# =>{"address"=>"AHXeTWYv8qZQhQ2WNrBza9LHyzdZtFnbaT", "success"=>true, "addressTag"=>"", "asset"=>"NEO"}

Required and optional parameters, as well as enum values, can currently be found on the Binance GitHub Page. Parameters should always be passed to client methods as keyword arguments in snake_case form.

WebSocket Client

Require Binance and EventMachine:

require'binance'require'eventmachine'

Create a new instance of the WebSocket Client:

client=Binance::Client::WebSocket.new

Create various WebSocket streams, wrapping calls inside EM.run:

EM.rundo# Create event handlersopen=proc{puts'connected'}message=proc{ |e| putse.data}error=proc{ |e| putse}close=proc{puts'closed'}# Bundle our event handlers into Hashmethods={open: open,message: message,error: error,close: close}# Pass a symbol and event handler Hash to connect and process eventsclient.agg_tradesymbol: 'XRPETH',methods: methods# kline takes an additional named parameterclient.klinesymbol: 'XRPETH',interval: '1m',methods: methods# As well as partial_book_depthclient.partial_book_depthsymbol: 'XRPETH',level: '5',methods: methods# Create a custom streamclient.singlestream: {type: 'aggTrade',symbol: 'XRPETH'},methods: methods# Create multiple streams in one callclient.multistreams: [{type: 'aggTrade',symbol: 'XRPETH'},{type: 'ticker',symbol: 'XRPETH'},{type: 'kline',symbol: 'XRPETH',interval: '1m'},{type: 'depth',symbol: 'XRPETH',level: '5'}],methods: methodsend

User Data Stream

User data streams utilize both the REST and WebSocket APIs.

Require Binance and EventMachine:

require'binance'require'eventmachine'

Create a new instance of the REST Client and WebSocket Client:

rest=Binance::Client::REST.newapi_key: 'x'ws=Binance::Client::WebSocket.new

Request a listen key from the REST API, and then create a WebSocket stream using it.

listen_key=rest.listen_key['listenKey']message=proc{ |e| putse.data}EM.rundows.user_datalisten_key: listen_key,methods: {message: message}end

Getting Started Futures trading

Rest Client

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hackhowtofaq/binance.

Buy me a LAMBO

Use my ref code for 10% discount Binance

To the moon? Buy me a LAMBO

License

The gem is available as open source under the terms of the MIT License.

About

API Wrapper for the Binance cryptocurrency exchange written in Ruby.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby97.8%
  • Shell2.2%