Skip to content

Commit

Permalink
Added Pairs, Tickers, OrderBook, Trades, and updated README for HPX (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
estherleongym authored and wongy91 committed Oct 26, 2018
1 parent 5074862 commit 47e4303
Show file tree
Hide file tree
Showing 12 changed files with 400 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Or install it yourself as:
| Hikenex | Y | N | N | | Y | N | hikenex |
| HitBTC | Y | | | | Y | | hitbtc |
| Hotbit | Y | N | N | | Y | Y | hotbit |
| HPX | Y | Y | Y | | Y | | hpx |
| Huobi | Y | | | | Y | Y | huobi |
| Ice3x | Y | Y | Y | | Y | | ice3x |
| Idax | Y | N | N | | Y | Y | idax |
Expand Down
8 changes: 8 additions & 0 deletions lib/cryptoexchange/exchanges/hpx/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Cryptoexchange::Exchanges
module Hpx
class Market < Cryptoexchange::Models::Market
NAME = 'hpx'
API_URL = 'https://api.hpx.com/data/v2'
end
end
end
50 changes: 50 additions & 0 deletions lib/cryptoexchange/exchanges/hpx/services/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Cryptoexchange::Exchanges
module Hpx
module Services
class Market < Cryptoexchange::Services::Market
class << self
def supports_individual_ticker_query?
false
end
end

def fetch
output = super(ticker_url)
adapt_all(output)
end

def ticker_url
"#{Cryptoexchange::Exchanges::Hpx::Market::API_URL}/tickers"
end

def adapt_all(output)
output['data'].map do |pair|
base, target = pair['symbol'].split('_')
market_pair = Cryptoexchange::Models::MarketPair.new(
base: base.upcase,
target: target.upcase,
market: Hpx::Market::NAME
)
adapt(market_pair, pair['ticker'])
end
end

def adapt(market_pair, output)
ticker = Cryptoexchange::Models::Ticker.new
ticker.base = market_pair.base
ticker.target = market_pair.target
ticker.market = Hpx::Market::NAME
ticker.last = NumericHelper.to_d(output['last'])
ticker.high = NumericHelper.to_d(output['high'])
ticker.low = NumericHelper.to_d(output['low'])
ticker.bid = NumericHelper.to_d(output['buy'])
ticker.ask = NumericHelper.to_d(output['sell'])
ticker.volume = NumericHelper.to_d(output['vol'])
ticker.timestamp = nil
ticker.payload = output
ticker
end
end
end
end
end
42 changes: 42 additions & 0 deletions lib/cryptoexchange/exchanges/hpx/services/order_book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Cryptoexchange::Exchanges
module Hpx
module Services
class OrderBook < 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['data'], market_pair)
end

def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::Hpx::Market::API_URL}/depth?currency=#{market_pair.base.downcase}_#{market_pair.target.upcase}"
end

def adapt(output, market_pair)
order_book = Cryptoexchange::Models::OrderBook.new

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

def adapt_orders(orders)
orders.collect do |order_entry|
Cryptoexchange::Models::Order.new(price: order_entry['price'],
amount: order_entry['amount'])
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/cryptoexchange/exchanges/hpx/services/pairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Cryptoexchange::Exchanges
module Hpx
module Services
class Pairs < Cryptoexchange::Services::Pairs
PAIRS_URL = "#{Cryptoexchange::Exchanges::Hpx::Market::API_URL}/tickers"

def fetch
output = super
market_pairs = []
output['data'].each do |pair|
base, target = pair['symbol'].split('_')
market_pairs << Cryptoexchange::Models::MarketPair.new(
base: base.upcase,
target: target.upcase,
market: Hpx::Market::NAME
)
end
market_pairs
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/cryptoexchange/exchanges/hpx/services/trades.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Cryptoexchange::Exchanges
module Hpx
module Services
class Trades < Cryptoexchange::Services::Market
def fetch(market_pair)
output = super(ticker_url(market_pair))
adapt(output, market_pair)
end

def ticker_url(market_pair)
"#{Cryptoexchange::Exchanges::Hpx::Market::API_URL}/trades?currency=#{market_pair.base.downcase}_#{market_pair.target.upcase}"
end

def adapt(output, market_pair)
output['data'].collect do |trade|
tr = Cryptoexchange::Models::Trade.new
tr.trade_id = trade['id']
tr.base = market_pair.base
tr.target = market_pair.target
tr.market = Hpx::Market::NAME
tr.type = trade['en_type'] == 0 ? 'buy' : 'sell'
tr.price = trade['price']
tr.amount = trade['amount']
tr.timestamp = nil
tr.payload = trade
tr
end
end
end
end
end
end

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

43 changes: 43 additions & 0 deletions spec/cassettes/vcr_cassettes/HPX/integration_specs_fetch_pairs.yml

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 47e4303

Please sign in to comment.