From 2ae5ca0e4730c57b96e216987a0b2abc318c1ce7 Mon Sep 17 00:00:00 2001 From: elvis Date: Tue, 1 Oct 2019 13:20:25 +0800 Subject: [PATCH] WIP: add xbtprime --- .byebug_history | 92 ------------------- .../exchanges/xbt_prime/market.rb | 8 ++ .../exchanges/xbt_prime/services/market.rb | 50 ++++++++++ .../xbt_prime/services/order_book.rb | 42 +++++++++ .../exchanges/xbt_prime/services/pairs.rb | 25 +++++ .../exchanges/xbt_prime/services/trades.rb | 31 +++++++ 6 files changed, 156 insertions(+), 92 deletions(-) delete mode 100644 .byebug_history create mode 100644 lib/cryptoexchange/exchanges/xbt_prime/market.rb create mode 100644 lib/cryptoexchange/exchanges/xbt_prime/services/market.rb create mode 100644 lib/cryptoexchange/exchanges/xbt_prime/services/order_book.rb create mode 100644 lib/cryptoexchange/exchanges/xbt_prime/services/pairs.rb create mode 100644 lib/cryptoexchange/exchanges/xbt_prime/services/trades.rb diff --git a/.byebug_history b/.byebug_history deleted file mode 100644 index 704e0e8c6..000000000 --- a/.byebug_history +++ /dev/null @@ -1,92 +0,0 @@ -c -xc -x -c -market_pair.inst_id.nil? -market_pair.inst_id.present? -market_pair.inst_id -market_pair -market_paid -t.inst_id -s = tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) || (t.base.casecmp(market_pair.base) == 0 && t.target.casecmp(market_pair.target) == 0) } -s = tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) } -c -s = tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) } -market_paoir -market_paooir -market_paoir -s = tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) } -tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) }.first -tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) } -tickers -exit -c -market_pair -c -market_pair -c -market_pair -c -market_pair -c -market_pair -c -market_pair -c -market_pair -c -(expire-start)/86400 -expire-start -c -pair -market_pair -pairs -(expire_timestamp - start_timestamp)/86400 -expire_timestamp - start_timestamp -expire_timestamp = DateTime.parse(expire).to_time -start_timestamp = DateTime.parse(start).to_time -expire -start -DateTime.parse("").to_time -DateTime.parse(nil) -DateTime.parse(nil).to_time -DateTime.parse(nil).to_time.to_i -DateTime.parse(expire).to_time.to_i -DateTime.parse(expire).to_time -DateTime.parse(expire) -DateTime.parse(expire).to_i -start -expire -expire - start -start -c -pp output -output -c -tt = tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) || (t.base.casecmp(market_pair.base) == 0 && t.target.casecmp(market_pair.target) == 0)} -tt.size -tt = tickers.select { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) || (t.base.casecmp(market_pair.base) == 0 && t.target.casecmp(market_pair.target) == 0)} -tt -tt.size -tt = tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) || (t.base.casecmp(market_pair.base) == 0 && t.target.casecmp(market_pair.target) == 0)} -tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) || (t.base.casecmp(market_pair.base) == 0 && t.target.casecmp(market_pair.target) == 0)} -tickers.find { |t| (t.inst_id.casecmp(market_pair.inst_id) == 0 if t.inst_id) } -tickers.find do |t| -market_pair.inst_id -tickers -tickers = market.fetch -tickers -c -ss.size -ss = swaps + futures -swaps + futures -futures -swaps -c -args -c -btc_usd_pair.inst_id -c -btc_usd_pair.inst_id -btc_usd_pair -inst_id diff --git a/lib/cryptoexchange/exchanges/xbt_prime/market.rb b/lib/cryptoexchange/exchanges/xbt_prime/market.rb new file mode 100644 index 000000000..7960c9805 --- /dev/null +++ b/lib/cryptoexchange/exchanges/xbt_prime/market.rb @@ -0,0 +1,8 @@ +module Cryptoexchange::Exchanges + module XbtPrime + class Market < Cryptoexchange::Models::Market + NAME = 'xbt_prime' + API_URL = 'https://api.primexbt.com/v1' + end + end +end diff --git a/lib/cryptoexchange/exchanges/xbt_prime/services/market.rb b/lib/cryptoexchange/exchanges/xbt_prime/services/market.rb new file mode 100644 index 000000000..1e7064640 --- /dev/null +++ b/lib/cryptoexchange/exchanges/xbt_prime/services/market.rb @@ -0,0 +1,50 @@ +module Cryptoexchange::Exchanges + module XbtPrime + 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::XbtPrime::Market::API_URL}/tickers" + end + + def adapt_all(output) + output['ticker'].map do |pair| + base, target = pair['symbol'].split('_') + market_pair = Cryptoexchange::Models::MarketPair.new( + base: base, + target: target, + market: XbtPrime::Market::NAME + ) + adapt(market_pair, pair) + end + end + + def adapt(market_pair, output) + ticker = Cryptoexchange::Models::Ticker.new + ticker.base = market_pair.base + ticker.target = market_pair.target + ticker.market = XbtPrime::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 diff --git a/lib/cryptoexchange/exchanges/xbt_prime/services/order_book.rb b/lib/cryptoexchange/exchanges/xbt_prime/services/order_book.rb new file mode 100644 index 000000000..ffa397950 --- /dev/null +++ b/lib/cryptoexchange/exchanges/xbt_prime/services/order_book.rb @@ -0,0 +1,42 @@ +module Cryptoexchange::Exchanges + module XbtPrime + 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, market_pair) + end + + def ticker_url(market_pair) + "#{Cryptoexchange::Exchanges::XbtPrime::Market::API_URL}/depth?symbol=#{market_pair.base.downcase}_#{market_pair.target.downcase}" + 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 = XbtPrime::Market::NAME + order_book.asks = adapt_orders(output['asks']) + order_book.bids = adapt_orders(output['bids']) + order_book.timestamp = Time.now.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[0], + amount: order_entry[1]) + end + end + end + end + end +end diff --git a/lib/cryptoexchange/exchanges/xbt_prime/services/pairs.rb b/lib/cryptoexchange/exchanges/xbt_prime/services/pairs.rb new file mode 100644 index 000000000..b198dfd96 --- /dev/null +++ b/lib/cryptoexchange/exchanges/xbt_prime/services/pairs.rb @@ -0,0 +1,25 @@ +module Cryptoexchange::Exchanges + module XbtPrime + module Services + class Pairs < Cryptoexchange::Services::Pairs + PAIRS_URL = "#{Cryptoexchange::Exchanges::XbtPrime::Market::API_URL}/markets" + + def fetch + output = super + market_pairs = [] + output['data'].each do |pair| + next unless pair['name'].include?('/') + + base, target = pair['name'].split('/') + market_pairs << Cryptoexchange::Models::MarketPair.new( + base: base, + target: target, + market: XbtPrime::Market::NAME + ) + end.compact + market_pairs + end + end + end + end +end diff --git a/lib/cryptoexchange/exchanges/xbt_prime/services/trades.rb b/lib/cryptoexchange/exchanges/xbt_prime/services/trades.rb new file mode 100644 index 000000000..082a49ad1 --- /dev/null +++ b/lib/cryptoexchange/exchanges/xbt_prime/services/trades.rb @@ -0,0 +1,31 @@ +module Cryptoexchange::Exchanges + module XbtPrime + 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::XbtPrime::Market::API_URL}/trades?symbol=#{market_pair.base.downcase}_#{market_pair.target.downcase}" + end + + def adapt(output, market_pair) + output.collect do |trade| + tr = Cryptoexchange::Models::Trade.new + tr.base = market_pair.base + tr.target = market_pair.target + tr.market = XbtPrime::Market::NAME + tr.type = trade['side'] + tr.price = trade['price'] + tr.amount = trade['amount'] + tr.timestamp = trade['timestamp']/1000 + tr.payload = trade + tr + end + end + end + end + end +end