orderbook 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/lib/orderbook.rb +8 -0
- data/lib/orderbook/book_analysis.rb +33 -0
- data/lib/orderbook/book_methods.rb +88 -0
- data/lib/orderbook/real_time_book.rb +38 -0
- data/lib/orderbook/version.rb +3 -0
- data/orderbook.gemspec +25 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4d48c6675dc85211b04b4387110bf495e5636a5f
|
4
|
+
data.tar.gz: 86ac1793072aeed4649702743695292ca281e553
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1fa8666c2db60b162c798ce35caa47d409eda86652d9110912de86ada6f7e8787db5e348c489674909c5907dc2943c57351d7c82fba7b18ed5685b6e685fb17e
|
7
|
+
data.tar.gz: f2fa544dfb48aac647e4d0a1e1314821927c6546c5ed718bf50504ec657c1f1ac7f2b8afe44874c30d23113cdd1cf55660045bb7031d865bbaca862ed952447f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Michael Rodrigues
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Orderbook
|
2
|
+
|
3
|
+
A gem for creating a realtime order book for the Coinbase Exchange.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'orderbook'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install orderbook
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'orderbook'
|
25
|
+
```
|
26
|
+
|
27
|
+
* To create a RealTimeBook:
|
28
|
+
```
|
29
|
+
rtb = Orderbook::RealTimeBook.new
|
30
|
+
|
31
|
+
rtb.bids # Returns an array of bids.
|
32
|
+
|
33
|
+
rtb.asks # Returns an array of asks.
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it ( https://github.com/[my-github-username]/orderbook/fork )
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/orderbook.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Orderbook
|
2
|
+
module BookAnalysis
|
3
|
+
def size
|
4
|
+
puts "Bids: #{@bids.count}"
|
5
|
+
puts "Asks: #{@asks.count}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def volume
|
9
|
+
puts "Bid volume: #{@bids.map {|x| x.fetch(1).to_f}.inject(:+)}"
|
10
|
+
puts "Ask volume: #{@asks.map {|x| x.fetch(1).to_f}.inject(:+)}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def average
|
14
|
+
array = @bids.map do |price, amount, id|
|
15
|
+
price.to_f
|
16
|
+
end
|
17
|
+
avg_bid = array.inject(:+) / array.count
|
18
|
+
puts "Avg. Bid: #{avg_bid}"
|
19
|
+
|
20
|
+
array = @asks.map do |price, amount, id|
|
21
|
+
price.to_f
|
22
|
+
end
|
23
|
+
avg_ask = array.inject(:+) / array.count
|
24
|
+
puts "Avg. Asks: #{avg_ask}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def best
|
28
|
+
puts "Best Bid: #{(@bids.sort_by {|x| x.fetch(0).to_f}).last}"
|
29
|
+
puts "Best Ask: #{@asks.sort_by {|x| x.fetch(0).to_f}.first}"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Orderbook
|
2
|
+
module BookMethods
|
3
|
+
|
4
|
+
def apply(msg)
|
5
|
+
unless msg.fetch('sequence') <= @sequence
|
6
|
+
__send__(msg.fetch('type'), msg)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def open(msg)
|
13
|
+
order = [ msg.fetch("price"), msg.fetch("remaining_size"), msg.fetch("order_id") ]
|
14
|
+
case msg.fetch("side")
|
15
|
+
when "buy"
|
16
|
+
@bids << order
|
17
|
+
when "sell"
|
18
|
+
@asks << order
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def match(msg)
|
23
|
+
match_size = msg.fetch("size").to_f
|
24
|
+
case msg.fetch("side")
|
25
|
+
when "sell"
|
26
|
+
@asks.map do |ask|
|
27
|
+
if ask.include? msg.fetch("maker_order_id")
|
28
|
+
old_size = ask.fetch(1).to_f
|
29
|
+
new_size = old_size - match_size
|
30
|
+
ask[1] = new_size.to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@bids.map do |bid|
|
34
|
+
if bid.include? msg.fetch("taker_order_id")
|
35
|
+
old_size = bid.fetch(1).to_f
|
36
|
+
new_size = old_size - match_size
|
37
|
+
bid[1] = new_size.to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
when "buy"
|
41
|
+
@bids.map do |bid|
|
42
|
+
if bid.include? msg.fetch("maker_order_id")
|
43
|
+
old_size = bid.fetch(1).to_f
|
44
|
+
new_size = old_size - match_size
|
45
|
+
bid[1] = new_size.to_s
|
46
|
+
end
|
47
|
+
end
|
48
|
+
@asks.map do |ask|
|
49
|
+
if ask.include? msg.fetch("taker_order_id")
|
50
|
+
old_size = ask.fetch(1).to_f
|
51
|
+
new_size = old_size - match_size
|
52
|
+
ask[1] = new_size.to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def done(msg)
|
59
|
+
case msg.fetch("side")
|
60
|
+
when "sell"
|
61
|
+
@asks.reject! {|a| a.include? msg.fetch("order_id")}
|
62
|
+
when "buy"
|
63
|
+
@bids.reject! {|a| a.include? msg.fetch("order_id")}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def change(msg)
|
68
|
+
case msg.fetch("side")
|
69
|
+
when "sell"
|
70
|
+
@asks.map do |a|
|
71
|
+
if a.include? msg.fetch("order_id")
|
72
|
+
a[1] = msg.fetch("new_size")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
when "buy"
|
76
|
+
@bids.map do |b|
|
77
|
+
if b.include? msg.fetch("order_id")
|
78
|
+
b[1] = msg.fetch("new_size")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def received(msg)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'orderbook/book_methods'
|
2
|
+
require 'orderbook/book_analysis'
|
3
|
+
|
4
|
+
module Orderbook
|
5
|
+
class RealTimeBook
|
6
|
+
attr_reader :bids
|
7
|
+
attr_reader :asks
|
8
|
+
attr_reader :sequence
|
9
|
+
|
10
|
+
include BookMethods
|
11
|
+
include BookAnalysis
|
12
|
+
def initialize()
|
13
|
+
@cb = ::CoinbaseExchange.new
|
14
|
+
@queue = Queue.new
|
15
|
+
on_msg = lambda {|msg| @queue << msg}
|
16
|
+
on_close = lambda {|close| puts close}
|
17
|
+
on_err = lambda {|err| puts err}
|
18
|
+
@feed = ::CoinbaseExchange::Feed.new(on_msg, on_close, on_err)
|
19
|
+
@snapshot = @cb.orderbook(3, 'BTC-USD')
|
20
|
+
@sequence = @snapshot.fetch('sequence').to_f
|
21
|
+
@bids = @snapshot.fetch('bids')
|
22
|
+
@asks = @snapshot.fetch('asks')
|
23
|
+
@update_thread = Thread.new do
|
24
|
+
loop do
|
25
|
+
msg = @queue.shift
|
26
|
+
unless msg.fetch("sequence").to_f <= @sequence
|
27
|
+
apply(msg)
|
28
|
+
@callback && @callback.call(msg)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_callback(&block)
|
35
|
+
@callback = block
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/orderbook.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'orderbook/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "orderbook"
|
8
|
+
spec.version = Orderbook::VERSION
|
9
|
+
spec.authors = ["Michael Rodrigues"]
|
10
|
+
spec.email = ["mikebrodrigues@gmail.com"]
|
11
|
+
spec.summary = %q{Maintains an real-time copy of the Coinbase Exchange order book.}
|
12
|
+
spec.description = %q{Orderbook uses the Coinbase Exchange Websocket stream
|
13
|
+
to maintain a real-time copy of the order book. Use it
|
14
|
+
in your BTC trading bot.}
|
15
|
+
spec.homepage = "https://github.com/mikerodrigues/orderbook"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: orderbook
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Rodrigues
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: |-
|
42
|
+
Orderbook uses the Coinbase Exchange Websocket stream
|
43
|
+
to maintain a real-time copy of the order book. Use it
|
44
|
+
in your BTC trading bot.
|
45
|
+
email:
|
46
|
+
- mikebrodrigues@gmail.com
|
47
|
+
executables: []
|
48
|
+
extensions: []
|
49
|
+
extra_rdoc_files: []
|
50
|
+
files:
|
51
|
+
- ".gitignore"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/orderbook.rb
|
57
|
+
- lib/orderbook/book_analysis.rb
|
58
|
+
- lib/orderbook/book_methods.rb
|
59
|
+
- lib/orderbook/real_time_book.rb
|
60
|
+
- lib/orderbook/version.rb
|
61
|
+
- orderbook.gemspec
|
62
|
+
homepage: https://github.com/mikerodrigues/orderbook
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Maintains an real-time copy of the Coinbase Exchange order book.
|
86
|
+
test_files: []
|