Class: Binance::WebSocket

Inherits:
Faye::WebSocket::Client
  • Object
show all
Defined in:
lib/binance/websocket.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(on_open: nil, on_close: nil) ⇒ WebSocket

Returns a new instance of WebSocket.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/binance/websocket.rb', line 5

def initialize(on_open: nil, on_close: nil)
  super "wss://stream.binance.com:9443/stream", nil, ping: 180

  @request_id_inc = 0
  @user_stream_handlers = {}

  on :open do |event|
    on_open&.call(event)
  end

  on :message do |event|
    process_data(event.data)
  end

  on :close do |event|
    on_close&.call(event)
  end
end

Instance Method Details

#candlesticks!(symbols, interval, &on_receive) ⇒ Object

stream name: <symbol>@kline_<interval>

{

"e": "kline",     // Event type
"E": 123456789,   // Event time
"s": "BNBBTC",    // Symbol
"k": {
  "t": 123400000, // Kline start time
  "T": 123460000, // Kline close time
  "s": "BNBBTC",  // Symbol
  "i": "1m",      // Interval
  "f": 100,       // First trade ID
  "L": 200,       // Last trade ID
  "o": "0.0010",  // Open price
  "c": "0.0020",  // Close price
  "h": "0.0025",  // High price
  "l": "0.0015",  // Low price
  "v": "1000",    // Base asset volume
  "n": 100,       // Number of trades
  "x": false,     // Is this kline closed?
  "q": "1.0000",  // Quote asset volume
  "V": "500",     // Taker buy base asset volume
  "Q": "0.500",   // Taker buy quote asset volume
  "B": "123456"   // Ignore
}

}



50
51
52
53
54
# File 'lib/binance/websocket.rb', line 50

def candlesticks!(symbols, interval, &on_receive)
  symbols_fmt = symbols.is_a?(String) ? [symbols] : symbols
  @candlesticks_handler = on_receive
  subscribe(symbols_fmt.map { |s| "#{s.downcase}@kline_#{interval}" })
end

#trades!(symbols, &on_receive) ⇒ Object

stream name: <symbol>@trade

"e": "trade",     // Event type
"E": 123456789,   // Event time
"s": "BNBBTC",    // Symbol
"t": 12345,       // Trade ID
"p": "0.001",     // Price
"q": "100",       // Quantity
"b": 88,          // Buyer order ID
"a": 50,          // Seller order ID
"T": 123456785,   // Trade time
"m": true,        // Is the buyer the market maker?
"M": true         // Ignore



75
76
77
78
79
# File 'lib/binance/websocket.rb', line 75

def trades!(symbols, &on_receive)
  symbols_fmt = symbols.is_a?(String) ? [symbols] : symbols
  @trades_handler = on_receive
  subscribe(symbols_fmt.map { |s| "#{s.downcase}@trade" })
end

#user_data_stream!(listen_key, &on_receive) ⇒ Object



56
57
58
59
# File 'lib/binance/websocket.rb', line 56

def user_data_stream!(listen_key, &on_receive)
  @user_stream_handlers[listen_key] = on_receive
  subscribe([listen_key])
end