Module: Binance::Spot::Market

Included in:
Binance::Spot
Defined in:
lib/binance/spot/market.rb

Overview

This module includes all spot public endpoints, including:

  • server time

  • kline

  • ticker

  • trades

  • orderbook

  • etc

Instance Method Summary collapse

Instance Method Details

#agg_trades(symbol:, **kwargs) ⇒ Object

Compressed/Aggregate Trades List

Get compressed, aggregate trades. Trades that fill at the time, from the same order, with the same price will have the quantity aggregated.

GET /api/v3/aggTrades

Parameters:

  • symbol (String)

    the symbol

  • kwargs (Hash)

Options Hash (**kwargs):

  • :startTime (Integer)

    Timestamp in ms to get aggregate trades from INCLUSIVE.

  • :endTime (Integer)

    Timestamp in ms to get aggregate trades until INCLUSIVE.

  • :fromId (Integer)

    Trade id to fetch from. Default gets most recent trades.

  • :limit (Integer)

    Default 500; max 1000.

See Also:



122
123
124
125
126
127
128
129
# File 'lib/binance/spot/market.rb', line 122

def agg_trades(symbol:, **kwargs)
  Binance::Utils::Validation.require_param('symbol', symbol)

  @session.public_request(
    path: '/api/v3/aggTrades',
    params: kwargs.merge(symbol: symbol)
  )
end

#avg_price(symbol:) ⇒ Object

Current Average Price

Current average price for a symbol.

GET /api/v3/avgPrice



166
167
168
169
170
171
172
173
# File 'lib/binance/spot/market.rb', line 166

def avg_price(symbol:)
  Binance::Utils::Validation.require_param('symbol', symbol)

  @session.public_request(
    path: '/api/v3/avgPrice',
    params: { symbol: symbol }
  )
end

#book_ticker(symbol: nil) ⇒ Object

Symbol Order Book Ticker

Best price/qty on the order book for a symbol or symbols.

GET /api/v3/ticker/bookTicker

Parameters:

  • symbol (String) (defaults to: nil)

    the symbol

See Also:



213
214
215
216
217
218
# File 'lib/binance/spot/market.rb', line 213

def book_ticker(symbol: nil)
  @session.public_request(
    path: '/api/v3/ticker/bookTicker',
    params: { symbol: symbol }
  )
end

#depth(symbol:, **kwargs) ⇒ Object

Order Book

GET /api/v3/depth

Parameters:

  • symbol (String)

    the symbol

  • kwargs (Hash)

Options Hash (**kwargs):

  • :limit (Integer)

    Default 100; max 1000. Valid limits:[5, 10, 20, 50, 100, 500, 1000, 5000]

See Also:



63
64
65
66
67
68
69
70
# File 'lib/binance/spot/market.rb', line 63

def depth(symbol:, **kwargs)
  Binance::Utils::Validation.require_param('symbol', symbol)

  @session.public_request(
    path: '/api/v3/depth',
    params: kwargs.merge(symbol: symbol)
  )
end

#exchange_info(symbol: nil, symbols: nil, permissions: nil) ⇒ Object

Exchange Information

GET /api/v3/exchangeInfo

Parameters:

  • kwargs (Hash)

    a customizable set of options

See Also:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/binance/spot/market.rb', line 40

def exchange_info(symbol: nil, symbols: nil, permissions: nil)
  if symbols.is_a?(Array)
    symbols = symbols.map { |v| "%22#{v}%22" }.join(',')
    symbols = "%5B#{symbols}%5D"
  end
  if permissions.is_a?(Array)
    permissions = permissions.map { |v| "%22#{v}%22" }.join(',')
    permissions = "%5B#{permissions}%5D"
  end
  @session.public_request(
    path: '/api/v3/exchangeInfo',
    params: { symbol: symbol, symbols: symbols, permissions: permissions }
  )
end

#historical_trades(symbol:, **kwargs) ⇒ Object

Old Trade Lookup

X-MBX-APIKEY required

GET /api/v3/historicalTrades

Parameters:

  • symbol (String)

    the symbol

  • kwargs (Hash)

Options Hash (**kwargs):

  • :limit (Integer)

    Default 500; max 1000.

  • :fromId (Integer)

    Trade id to fetch from. Default gets most recent trades.

See Also:



100
101
102
103
104
105
106
107
# File 'lib/binance/spot/market.rb', line 100

def historical_trades(symbol:, **kwargs)
  Binance::Utils::Validation.require_param('symbol', symbol)

  @session.public_request(
    path: '/api/v3/historicalTrades',
    params: kwargs.merge(symbol: symbol)
  )
end

#klines(symbol:, interval:, **kwargs) ⇒ Object

Kline/Candlestick Data

Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.

GET /api/v3/klines

Parameters:

  • symbol (String)

    the symbol

  • interval (String)

    interval

  • kwargs (Hash)

Options Hash (**kwargs):

  • :startTime (Integer)

    Timestamp in ms to get aggregate trades from INCLUSIVE.

  • :endTime (Integer)

    Timestamp in ms to get aggregate trades until INCLUSIVE.

  • :limit (Integer)

    Default 500; max 1000.

See Also:



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/binance/spot/market.rb', line 145

def klines(symbol:, interval:, **kwargs)
  Binance::Utils::Validation.require_param('symbol', symbol)
  Binance::Utils::Validation.require_param('interval', interval)

  @session.public_request(
    path: '/api/v3/klines',
    params: kwargs.merge(
      symbol: symbol,
      interval: interval
    )
  )
end

#pingObject

Test Connectivity

GET /api/v3/ping



19
20
21
# File 'lib/binance/spot/market.rb', line 19

def ping
  @session.public_request(path: '/api/v3/ping')
end

#ticker(symbol: nil, symbols: nil, windowSize: '1d') ⇒ Object

Symbol Order Book Ticker

Best price/qty on the order book for a symbol or symbols.

GET /api/v3/ticker/bookTicker

Parameters:

  • symbol (String) (defaults to: nil)

    the symbol

Raises:

See Also:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/binance/spot/market.rb', line 228

def ticker(symbol: nil, symbols: nil, windowSize: '1d')
  raise Binance::DuplicatedParametersError.new('symbol', 'symbols') unless symbols.nil? || symbol.nil?

  params = { symbol: symbol.upcase } if symbol

  if symbols
    symbols = symbols.map { |s| "\"#{s}\"" }.join(',')
    params = { symbols: "\[#{symbols}\]".upcase }
  end

  params[:windowSize] = windowSize

  @session.public_request(
    path: '/api/v3/ticker',
    params: params
  )
end

#ticker_24hr(symbol: nil) ⇒ Object

24hr Ticker Price Change Statistics

24 hour rolling window price change statistics. Careful when accessing this with no symbol.

GET /api/v3/ticker/24hr

Parameters:

  • symbol (String) (defaults to: nil)

    the symbol

See Also:



183
184
185
186
187
188
# File 'lib/binance/spot/market.rb', line 183

def ticker_24hr(symbol: nil)
  @session.public_request(
    path: '/api/v3/ticker/24hr',
    params: { symbol: symbol }
  )
end

#ticker_price(symbol: nil) ⇒ Object

Symbol Price Ticker

Latest price for a symbol or symbols.

GET /api/v3/ticker/price

Parameters:

  • symbol (String) (defaults to: nil)

    the symbol

See Also:



198
199
200
201
202
203
# File 'lib/binance/spot/market.rb', line 198

def ticker_price(symbol: nil)
  @session.public_request(
    path: '/api/v3/ticker/price',
    params: { symbol: symbol }
  )
end

#timeObject

Check Server Time

GET /api/v3/time



28
29
30
# File 'lib/binance/spot/market.rb', line 28

def time
  @session.public_request(path: '/api/v3/time')
end

#trades(symbol:, **kwargs) ⇒ Object

Recent Trades List

GET /api/v3/trades

Parameters:

  • symbol (String)

    the symbol

  • kwargs (Hash)

Options Hash (**kwargs):

  • :limit (Integer)

    Default 500; max 1000.

See Also:



80
81
82
83
84
85
86
87
# File 'lib/binance/spot/market.rb', line 80

def trades(symbol:, **kwargs)
  Binance::Utils::Validation.require_param('symbol', symbol)

  @session.public_request(
    path: '/api/v3/trades',
    params: kwargs.merge(symbol: symbol)
  )
end