Class: Binance

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/binance_ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, api_secret = nil, options = {}) ⇒ Binance

Returns a new instance of Binance.



11
12
13
14
15
# File 'lib/binance_ruby.rb', line 11

def initialize(api_key=nil, api_secret=nil, options={})
  @api_key      = api_key
  @api_secret   = api_secret
  @base_uri     = options[:base_uri] ||= 'https://www.binance.com/api'
end

Instance Method Details

#aggregate_trade_list(pair) ⇒ Object



31
32
33
34
# File 'lib/binance_ruby.rb', line 31

def aggregate_trade_list(pair)
  opts = {'symbol' => pair}
  response = get_public 'aggTrades', opts
end

#cancel_order(opts = {}) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/binance_ruby.rb', line 126

def cancel_order(opts={})
  required_opts = %w{ symbol }
  leftover = required_opts - opts.keys.map(&:to_s)
  if leftover.length > 0 || (opts["orderId"].nil? && opts["origClientOrderId"].nil?)
    raise ArgumentError.new("Required options not given : symbol and either orderId or origClientOrderId}")
  end
  response = delete_private '/order?', opts
end

#candles(pair, interval) ⇒ Object



36
37
38
39
# File 'lib/binance_ruby.rb', line 36

def candles(pair, interval)
  opts = {'symbol' => pair, 'interval' => interval}
  response = get_public 'klines', opts
end

#get_account(opts = {}) ⇒ Object

Private Data #######



66
67
68
# File 'lib/binance_ruby.rb', line 66

def (opts={})
  response = get_private '/account?', opts
end

#get_all_orders(opts = {}) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/binance_ruby.rb', line 108

def get_all_orders(opts={})
  required_opts = %w{ symbol }
  leftover = required_opts - opts.keys.map(&:to_s)
  if leftover.length > 0
    raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
  end
  response = get_private '/allOrders?', opts
end

#get_open_orders(opts = {}) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/binance_ruby.rb', line 99

def get_open_orders(opts={})
  required_opts = %w{ symbol }
  leftover = required_opts - opts.keys.map(&:to_s)
  if leftover.length > 0
    raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
  end
  response = get_private '/openOrders?', opts
end

#get_order(opts = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/binance_ruby.rb', line 90

def get_order(opts={})
  required_opts = %w{ symbol }
  leftover = required_opts - opts.keys.map(&:to_s)
  if leftover.length > 0 || (opts["orderId"].nil? && opts["origClientOrderId"].nil?)
    raise ArgumentError.new("Required options not given : symbol and either orderId or origClientOrderId}")
  end
  response = get_private '/order?', opts
end

#get_public(method, opts = {}) ⇒ Object



56
57
58
59
60
# File 'lib/binance_ruby.rb', line 56

def get_public(method, opts={})
  url = @base_uri + '/v1/' + method
  r = self.class.get(url, query: opts)
  resp = JSON.parse(r.body)
end

#get_trades(opts = {}) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/binance_ruby.rb', line 117

def get_trades(opts={})
  required_opts = %w{ symbol }
  leftover = required_opts - opts.keys.map(&:to_s)
  if leftover.length > 0
    raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
  end
  response = get_private '/myTrades?', opts
end

#order_book(pair) ⇒ Object



26
27
28
29
# File 'lib/binance_ruby.rb', line 26

def order_book(pair)
  opts = {'symbol' => pair}
  response = get_public 'depth', opts
end

#server_timeObject

Public Data ########



21
22
23
24
# File 'lib/binance_ruby.rb', line 21

def server_time
  response = get_public('time')
  response["serverTime"]
end

#set_order(opts = {}) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/binance_ruby.rb', line 81

def set_order(opts={})
  opts[:type] == "MARKET" ? required_opts = %w{ symbol side type timeInForce quantity price } : required_opts = %w{ symbol side type quantity }
  leftover = required_opts - opts.keys.map(&:to_s)
  if leftover.length > 0
    raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
  end
  response = post_private '/order?', opts
end

#test_order(opts = {}) ⇒ Object

Mandatory parameters for order : symbol side type timeInForce quantity price



72
73
74
75
76
77
78
79
# File 'lib/binance_ruby.rb', line 72

def test_order(opts={})
  opts[:type] == "MARKET" ? required_opts = %w{ symbol side type timeInForce quantity price } : required_opts = %w{ symbol side type quantity }
  leftover = required_opts - opts.keys.map(&:to_s)
  if leftover.length > 0
    raise ArgumentError.new("Required options, not given. Input must include #{leftover}")
  end
  response = post_private '/order/test?', opts
end

#ticker_allObject



48
49
50
# File 'lib/binance_ruby.rb', line 48

def ticker_all
  response = get_public 'ticker/allPrices'
end

#ticker_all_order_bookObject



52
53
54
# File 'lib/binance_ruby.rb', line 52

def ticker_all_order_book
  response = get_public '/ticker/allBookTickers'
end

#ticker_day(pair) ⇒ Object

interval list : 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M



43
44
45
46
# File 'lib/binance_ruby.rb', line 43

def ticker_day(pair)
  opts = {'symbol' => pair}
  response = get_public 'ticker/24hr', opts
end