Class: Binance::Spot::WebSocket

Inherits:
WebSocketBase show all
Defined in:
lib/binance/spot/websocket.rb

Overview

Spot Websocket

Constant Summary collapse

BASE_URL =
'wss://stream.binance.com:9443'

Instance Method Summary collapse

Methods inherited from WebSocketBase

#create_connection, #subscribe_to

Constructor Details

#initialize(options = {}) ⇒ WebSocket

Returns a new instance of WebSocket.



11
12
13
14
15
# File 'lib/binance/spot/websocket.rb', line 11

def initialize(options = {})
  @base_url = options[:base_url] || BASE_URL
  options[:base_url] = @base_url
  super(options)
end

Instance Method Details

#agg_trade(symbol:, callbacks:) ⇒ Object

Aggregate Trade Streams The Aggregate Trade Streams push trade information that is aggregated for a single taker order. Stream Name: <symbol>@aggTrade Update Speed: Real-time



24
25
26
27
# File 'lib/binance/spot/websocket.rb', line 24

def agg_trade(symbol:, callbacks:)
  url = "#{@base_url}/ws/#{symbol.downcase}@aggTrade"
  create_connection(url, callbacks)
end

#book_ticker(callbacks:, symbol: nil) ⇒ Object

Individual Symbol Book Ticker Streams or All Book Tickers Stream Pushes any update to the best bid or ask’s price or quantity in real-time for a specified symbol. Stream Name: <symbol>@bookTicker or !bookTicker Update Speed: Real-time

Parameters:

  • symbol (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (symbol:):

  • (String)

See Also:



93
94
95
96
97
98
99
100
# File 'lib/binance/spot/websocket.rb', line 93

def book_ticker(callbacks:, symbol: nil)
  url = if symbol.nil?
          "#{@base_url}/ws/!bookTicker"
        else
          "#{@base_url}/ws/#{symbol.downcase}@bookTicker"
        end
  create_connection(url, callbacks)
end

#diff_book_depth(symbol:, speed:, callbacks:) ⇒ Object

Diff. Depth Stream Top bids and asks, Valid are 5, 10, or 20. Stream Name: <symbol>@depth<levels> OR <symbol>@depth<levels>@100ms. Update Speed: 1000ms or 100ms

Parameters:

  • symbol (String)
  • speed (String)

    1000ms or 100ms

See Also:



124
125
126
127
# File 'lib/binance/spot/websocket.rb', line 124

def diff_book_depth(symbol:, speed:, callbacks:)
  url = "#{@base_url}/ws/#{symbol.downcase}@depth@#{speed}"
  create_connection(url, callbacks)
end

#kline(symbol:, interval:, callbacks:) ⇒ Object

Kline/Candlestick Streams for UTC The Kline/Candlestick Stream push updates to the current klines/candlestick every second. Stream Name: <symbol>@kline_<interval> Update Speed: 2000ms

Parameters:

  • symbol (String)
  • interval (String)

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

See Also:



49
50
51
52
# File 'lib/binance/spot/websocket.rb', line 49

def kline(symbol:, interval:, callbacks:)
  url = "#{@base_url}/ws/#{symbol.downcase}@kline_#{interval}"
  create_connection(url, callbacks)
end

#mini_ticker(callbacks:, symbol: nil) ⇒ Object

Individual Symbol Mini Ticker Stream or All Market Mini Tickers Stream 24hr rolling window mini-ticker statistics. These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs. Stream Name: <symbol>@miniTicker or !miniTicker@arr Update Speed: 1000ms

Parameters:

  • symbol (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (symbol:):

  • (String)

See Also:



61
62
63
64
65
66
67
68
# File 'lib/binance/spot/websocket.rb', line 61

def mini_ticker(callbacks:, symbol: nil)
  url = if symbol.nil?
          "#{@base_url}/ws/!miniTicker@arr"
        else
          "#{@base_url}/ws/#{symbol.downcase}@miniTicker"
        end
  create_connection(url, callbacks)
end

#partial_book_depth(symbol:, levels:, speed:, callbacks:) ⇒ Object

Partial Book Depth Streams Top bids and asks, Valid are 5, 10, or 20. Stream Name: <symbol>@depth<levels> OR <symbol>@depth<levels>@100ms. Update Speed: 1000ms or 100ms

Parameters:

  • symbol (String)
  • levels (Integer)

    5, 10, or 20.

  • speed (String)

    1000ms or 100ms

See Also:



111
112
113
114
# File 'lib/binance/spot/websocket.rb', line 111

def partial_book_depth(symbol:, levels:, speed:, callbacks:)
  url = "#{@base_url}/ws/#{symbol.downcase}@depth#{levels}@#{speed}"
  create_connection(url, callbacks)
end

#rolling_window_ticker(symbol:, windowSize:, callbacks:) ⇒ Object

Individual Symbol Rolling Window Statistics Streams Rolling window ticker statistics for a single symbol, computed over multiple windows. Stream Name: <symbol>@ticker_<window_size> Window Sizes: 1h,4h Update Speed: 1000ms



136
137
138
139
# File 'lib/binance/spot/websocket.rb', line 136

def rolling_window_ticker(symbol:, windowSize:, callbacks:)
  url = "#{@base_url}/ws/#{symbol.downcase}@ticker_#{windowSize}"
  create_connection(url, callbacks)
end

#rolling_window_ticker_all_symbols(windowSize:, callbacks:) ⇒ Object

All Market Rolling Window Statistics Streams Rolling window ticker statistics for all market symbols, computed over multiple windows. Note that only tickers that have changed will be present in the array. Stream Name: !ticker_<window-size>@arr Window Sizes: 1h, 4h Update Speed: 1000ms



148
149
150
151
# File 'lib/binance/spot/websocket.rb', line 148

def rolling_window_ticker_all_symbols(windowSize:, callbacks:)
  url = "#{@base_url}/ws/!ticker_#{windowSize}@arr"
  create_connection(url, callbacks)
end

#subscribe(stream:, callbacks:) ⇒ Object Also known as: user_data

Subscribe to a stream manually subscribe(stream: “btcusdt@miniTicker”) or subscribe(stream: [“btcusdt@miniTicker”, “ethusdt@miniTicker”])

Parameters:

  • stream (String|Array)


158
159
160
161
162
163
164
165
166
# File 'lib/binance/spot/websocket.rb', line 158

def subscribe(stream:, callbacks:)
  url = if stream.is_a?(Array)
          "#{@base_url}/stream?streams=#{stream.join('/')}"
        else
          "#{@base_url}/ws/#{stream}"
        end

  subscribe_to(url, callbacks)
end

#symbol_ticker(callbacks:, symbol: nil) ⇒ Object

Individual Symbol Ticker Streams or All Market Tickers Stream 24hr rollwing window ticker statistics for a single symbol. These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs. Stream Name: <symbol>@ticker or !ticker@arr Update Speed: 1000ms

Parameters:

  • symbol (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (symbol:):

  • (String)

See Also:



77
78
79
80
81
82
83
84
# File 'lib/binance/spot/websocket.rb', line 77

def symbol_ticker(callbacks:, symbol: nil)
  url = if symbol.nil?
          "#{@base_url}/ws/!ticker@arr"
        else
          "#{@base_url}/ws/#{symbol.downcase}@ticker"
        end
  create_connection(url, callbacks)
end

#trade(symbol:, callbacks:) ⇒ Object

Trade Streams The Trade Streams push raw trade information; each trade has a unique buyer and seller. Stream Name: <symbol>@trade Update Speed: Real-time



36
37
38
39
# File 'lib/binance/spot/websocket.rb', line 36

def trade(symbol:, callbacks:)
  url = "#{@base_url}/ws/#{symbol.downcase}@trade"
  create_connection(url, callbacks)
end