Skip to content

Commit

Permalink
Merge pull request #29 from wilkosz/feature/trade-stream
Browse files Browse the repository at this point in the history
Adds Trades Stream
  • Loading branch information
0xjmp authored Jan 9, 2022
2 parents f9d1510 + f8cc31e commit 21b672a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ EM.run do
end
```

**Trades:**

```ruby
# These callbacks are optional.
on_open = ->(event) do
puts ">> Websocket opened"
end
on_close = ->(event) do
puts ">> Websocket closed (#{event.code}): #{event.reason}"
end
EM.run do
websocket = Binance::WebSocket.new(on_open: on_open, on_close: on_close)

websocket.trades!(['ETHBTC']) do |stream_name, trade|
symbol = trade[:s]
# Do whatever!
end
end
```

**User Data:**

```ruby
Expand Down Expand Up @@ -170,6 +190,7 @@ You can find more info on all `kline_candlestick` attributes & available interva
### Binance::WebSocket instance methods

- [`candlesticks!`](https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#klinecandlestick-streams): Kline/candlestick bars for a symbol.
- [`trades!`](https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#trade-streams): The Trade Streams push raw trade information.
- [`user_data_stream!`](https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md#web-socket-payloads): account updates, balances changes, and order updates.

See the [rubydoc](http://www.rubydoc.info/gems/binance-ruby/0.1.2/Binance) for information about parameters for each method listed above.
Expand Down
2 changes: 1 addition & 1 deletion lib/binance/api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Binance
module Api
VERSION = "1.2.9"
VERSION = "1.2.10"
end
end
22 changes: 22 additions & 0 deletions lib/binance/websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ def user_data_stream!(listen_key, &on_receive)
subscribe([listen_key])
end

# 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
# }
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

private

def process_data(data)
Expand All @@ -75,6 +95,8 @@ def process_data(data)
when :executionReport # order update
listen_key = json[:stream]
@user_stream_handlers[listen_key]&.call(listen_key, json[:data])
when :trade
@trades_handler&.call(json[:stream], json[:data])
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/binance/websocket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@
end
end

describe "#trades" do
let(:stream_name) { "#{symbols.first.downcase}@kline" }

context "error" do
let(:json_string) { '{ "error": {"code": 0, "msg": "Unknown property","id": 123} }' }

subject { websocket.trades!(symbols) }

it { is_expected_block.to raise_error Binance::WebSocket::Error }
end

context "trade" do
let(:json_string) do
{
stream: stream_name,
data: JSON.parse(File.read("spec/fixtures/streams/trade.json"), symbolize_names: true),
}.to_json
end

it "calls on_receive" do
inc = 0
websocket.trades!(symbols) { inc = 1 }
expect(inc).to eq 1
end
end

end

describe "#candlesticks" do
let(:stream_name) { "#{symbols.first.downcase}@kline_#{interval}" }

Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/streams/trade.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"e": "trade",
"E": 123456789,
"s": "BNBBTC",
"t": 12345,
"p": "0.001",
"q": "100",
"b": 88,
"a": 50,
"T": 123456785,
"m": true,
"M": true
}

0 comments on commit 21b672a

Please sign in to comment.