This repository has been archived by the owner on Jun 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from weh/feature/binance
Add Binance ticker, and order book
- Loading branch information
Showing
11 changed files
with
403 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
lib/cryptoexchange/exchanges/binance/services/order_book.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
36 changes: 36 additions & 0 deletions
36
spec/cassettes/vcr_cassettes/Binance/integration_specs_fetch_order_book.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
spec/cassettes/vcr_cassettes/Binance/integration_specs_fetch_ticker.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.