Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #175 from tmlee/feature/order_abstraction
Browse files Browse the repository at this point in the history
Add Order class to abstract formatting into [price, amount, timestamp]
  • Loading branch information
tmlee committed Oct 20, 2017
2 parents 5a29d74 + 2b7ba13 commit 5990d51
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 12 deletions.
13 changes: 11 additions & 2 deletions lib/cryptoexchange/exchanges/allcoin/services/order_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ def adapt(output, market_pair)
order_book.base = market_pair.base
order_book.target = market_pair.target
order_book.market = Allcoin::Market::NAME
order_book.asks = output['asks']
order_book.bids = output['bids']
order_book.asks = adapt_orders output['asks']
order_book.bids = adapt_orders output['bids']
order_book.timestamp = timestamp
order_book.payload = output
order_book
end

def adapt_orders(orders)
orders.collect do |order_entry|
price, amount = order_entry
Cryptoexchange::Models::Order.new(price: price,
amount: amount,
timestamp: nil)
end
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/cryptoexchange/exchanges/coinmate/services/order_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ def adapt(output, market_pair)
end

def adapt_orders(orders, timestamp)
# Format as: [price, amount, timestamp]
orders.collect { |order_entry| order_entry.values << timestamp }
orders.collect do |order_entry|
Cryptoexchange::Models::Order.new(price: order_entry['price'],
amount: order_entry['amount'],
timestamp: timestamp)
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/cryptoexchange/exchanges/gemini/services/order_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ def adapt(output, market_pair)
end

def adapt_orders(orders)
# Format as: [price, amount, timestamp]
orders.collect { |order_entry| order_entry.values }
orders.collect do |order_entry|
price, amount, timestamp = order_entry.values
Cryptoexchange::Models::Order.new(price: price,
amount: amount,
timestamp: timestamp)
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/cryptoexchange/models/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Cryptoexchange
module Models
class Order
attr_accessor :price, :amount, :timestamp

def initialize(args={})
@price = args[:price]
@amount = args[:amount]
@timestamp = args[:timestamp] || nil
end
end
end
end
10 changes: 10 additions & 0 deletions lib/cryptoexchange/models/order_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ class OrderBook
attr_accessor :base, :target, :market, :bids,
:asks, :timestamp, :payload

def initialize(args = {})
@base = args[:base]
@target = args[:target]
@market = args[:market]
@bids = args[:bids] || []
@asks = args[:asks] || []
@timestamp = args[:timestamp]
@payload = args[:payload]
end

def base
@base.upcase
end
Expand Down
5 changes: 3 additions & 2 deletions spec/exchanges/allcoin/integration/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
expect(order_book.market).to eq 'allcoin'
expect(order_book.asks).to_not be_empty
expect(order_book.bids).to_not be_empty
expect(order_book.asks.first.size).to eq 2
expect(order_book.bids.first.size).to eq 2
expect(order_book.asks.first.price).to_not be_nil
expect(order_book.bids.first.amount).to_not be_nil
expect(order_book.bids.first.timestamp).to be_nil
expect(order_book.asks.count).to be > 10
expect(order_book.bids.count).to be > 10
expect(order_book.timestamp).to be_a Numeric
Expand Down
5 changes: 3 additions & 2 deletions spec/exchanges/coinmate/integration/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
expect(order_book.market).to eq market
expect(order_book.asks).to_not be_empty
expect(order_book.bids).to_not be_empty
expect(order_book.asks.first.size).to eq 3
expect(order_book.bids.first.size).to eq 3
expect(order_book.asks.first.price).to_not be_nil
expect(order_book.bids.first.amount).to_not be_nil
expect(order_book.bids.first.timestamp).to_not be_nil
expect(order_book.asks.count).to be > 10
expect(order_book.bids.count).to be > 10
expect(order_book.timestamp).to be_a Numeric
Expand Down
5 changes: 3 additions & 2 deletions spec/exchanges/gemini/integration/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
expect(order_book.market).to eq 'gemini'
expect(order_book.asks).to_not be_empty
expect(order_book.bids).to_not be_empty
expect(order_book.asks.first.size).to eq 3
expect(order_book.bids.first.size).to eq 3
expect(order_book.asks.first.price).to_not be_nil
expect(order_book.bids.first.amount).to_not be_nil
expect(order_book.bids.first.timestamp).to_not be_nil
expect(order_book.timestamp).to be_a Numeric
expect(order_book.payload).to_not be nil
end
Expand Down
14 changes: 14 additions & 0 deletions spec/models/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

RSpec.describe Cryptoexchange::Models::Order do
let(:time_now) { Time.now.to_i }
let(:order) { described_class.new(price: 1000, amount: 9999) }
let(:order_with_timestamp) do
described_class.new(price: 1000, amount: 9999, timestamp: time_now)
end

it { expect(order.price).to eq 1000 }
it { expect(order.amount).to eq 9999 }
it { expect(order.timestamp).to be_nil }
it { expect(order_with_timestamp.timestamp).to eq time_now }
end
7 changes: 7 additions & 0 deletions spec/models/order_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

RSpec.describe Cryptoexchange::Models::OrderBook do
let(:new_order_book) { described_class.new }
it { expect(new_order_book.bids).to be_empty }
it { expect(new_order_book.asks).to be_empty }
end
4 changes: 4 additions & 0 deletions spec/models/trade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'spec_helper'

RSpec.describe Cryptoexchange::Models::Trade do
end

0 comments on commit 5990d51

Please sign in to comment.