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 #173 from weh/feature/binance
Browse files Browse the repository at this point in the history
Add Binance ticker, and order book
  • Loading branch information
tmlee committed Oct 25, 2017
2 parents 1c81e96 + 04548e7 commit b148488
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Or install it yourself as:
| ----------------- | ------- | ---------- | ------- | ------- | ----------- |
| Allcoin | Y | Y | | | User-Defined|
| ANX | Y | | | | User-Defined|
| Bit-Z | Y | | | | Y
| Binance | | | | | |
| Bit-Z | Y | | | | Y |
| Binance | Y | Y | | | User-Defined|
| Bitbay | Y | | | | User-Defined|
| Bitcoin Indonesia | Y | | | | User-Defined|
| Bitfinex | Y | | | | Y |
Expand Down
147 changes: 147 additions & 0 deletions lib/cryptoexchange/exchanges/binance/binance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
:pairs:
- :base: ETH
:target: BTC
- :base: LTC
:target: BTC
- :base: BNB
:target: BTC
- :base: NEO
:target: BTC
- :base: QTUM
:target: ETH
- :base: EOS
:target: ETH
- :base: SNT
:target: ETH
- :base: BNT
:target: ETH
- :base: BCC
:target: BTC
- :base: GAS
:target: BTC
- :base: BNB
:target: ETH
- :base: BTM
:target: ETH
- :base: HCC
:target: BTC
- :base: BTC
:target: USDT
- :base: ETH
:target: USDT
- :base: HSR
:target: BTC
- :base: OAX
:target: ETH
- :base: DNT
:target: ETH
- :base: MCO
:target: ETH
- :base: ICN
:target: ETH
- :base: ELC
:target: BTC
- :base: MCO
:target: BTC
- :base: WTC
:target: BTC
- :base: WTC
:target: ETH
- :base: LLT
:target: BTC
- :base: LRC
:target: BTC
- :base: LRC
:target: ETH
- :base: QTUM
:target: BTC
- :base: YOYO
:target: BTC
- :base: OMG
:target: BTC
- :base: OMG
:target: ETH
- :base: ZRX
:target: BTC
- :base: ZRX
:target: ETH
- :base: STRAT
:target: BTC
- :base: STRAT
:target: ETH
- :base: SNGLS
:target: BTC
- :base: SNGLS
:target: ETH
- :base: BQX
:target: BTC
- :base: BQX
:target: ETH
- :base: KNC
:target: BTC
- :base: KNC
:target: ETH
- :base: FUN
:target: BTC
- :base: FUN
:target: ETH
- :base: SNM
:target: BTC
- :base: SNM
:target: ETH
- :base: NEO
:target: ETH
- :base: IOTA
:target: BTC
- :base: IOTA
:target: ETH
- :base: LINK
:target: BTC
- :base: LINK
:target: ETH
- :base: XVG
:target: BTC
- :base: XVG
:target: ETH
- :base: CTR
:target: BTC
- :base: CTR
:target: ETH
- :base: SALT
:target: BTC
- :base: SALT
:target: ETH
- :base: MDA
:target: BTC
- :base: MDA
:target: ETH
- :base: MTL
:target: BTC
- :base: MTL
:target: ETH
- :base: SUB
:target: BTC
- :base: SUB
:target: ETH
- :base: EOS
:target: BTC
- :base: SNT
:target: BTC
- :base: ETC
:target: ETH
- :base: ETC
:target: BTC
- :base: MTH
:target: BTC
- :base: MTH
:target: ETH
- :base: ENG
:target: BTC
- :base: ENG
:target: ETH
- :base: DNT
:target: BTC
- :base: ZEC
:target: BTC
- :base: ZEC
:target: ETH
8 changes: 8 additions & 0 deletions lib/cryptoexchange/exchanges/binance/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Cryptoexchange::Exchanges
module Binance
class Market
NAME = 'binance'
API_URL = 'https://www.binance.com/api/v1'
end
end
end
40 changes: 40 additions & 0 deletions lib/cryptoexchange/exchanges/binance/services/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Cryptoexchange::Exchanges
module Binance
module Services
class Market < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
end
end

def fetch(market_pair)
output = super(ticker_url(market_pair))
adapt(output, market_pair)
end

def ticker_url(market_pair)
base = market_pair.base
target = market_pair.target
"#{Cryptoexchange::Exchanges::Binance::Market::API_URL}/ticker/24hr?symbol=#{base}#{target}"
end

def adapt(output, market_pair)
ticker = Cryptoexchange::Models::Ticker.new
ticker.base = market_pair.base
ticker.target = market_pair.target
ticker.market = Binance::Market::NAME
ticker.bid = NumericHelper.to_d(output['bidPrice'])
ticker.ask = NumericHelper.to_d(output['askPrice'])
ticker.last = NumericHelper.to_d(output['lastPrice'])
ticker.high = NumericHelper.to_d(output['highPrice'])
ticker.low = NumericHelper.to_d(output['lowPrice'])
ticker.volume = NumericHelper.to_d(output['volume'])
ticker.timestamp = output['closeTime'].to_i / 1000
ticker.payload = output
ticker
end
end
end
end
end
46 changes: 46 additions & 0 deletions lib/cryptoexchange/exchanges/binance/services/order_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Cryptoexchange::Exchanges
module Binance
module Services
class OrderBook < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
true
end
end

def fetch(market_pair)
output = super(order_book_url(market_pair))
adapt(output, market_pair)
end

def order_book_url(market_pair)
base = market_pair.base
target = market_pair.target
"#{Cryptoexchange::Exchanges::Binance::Market::API_URL}/depth?symbol=#{base}#{target}"
end

def adapt(output, market_pair)
order_book = Cryptoexchange::Models::OrderBook.new
timestamp = Time.now.to_i

order_book.base = market_pair.base
order_book.target = market_pair.target
order_book.market = Binance::Market::NAME
order_book.asks = adapt_orders(output['asks'], timestamp)
order_book.bids = adapt_orders(output['bids'], timestamp)
order_book.timestamp = timestamp
order_book.payload = output
order_book
end

def adapt_orders(orders, timestamp)
orders.collect do |order_entry|
Cryptoexchange::Models::Order.new(price: NumericHelper.to_d(order_entry[0]),
amount: NumericHelper.to_d(order_entry[1]),
timestamp: timestamp)
end
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/cryptoexchange/exchanges/binance/services/pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Cryptoexchange::Exchanges
module Binance
module Services
class Pairs < Cryptoexchange::Services::Pairs

def fetch
output = super
adapt(output)
end

def adapt(output)
market_pairs = []
output.each do |pair|
market_pairs << Cryptoexchange::Models::MarketPair.new(
base: pair[:base],
target: pair[:target],
market: Binance::Market::NAME
)
end
market_pairs
end
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b148488

Please sign in to comment.