Class: BitstampApiWrapper
- Inherits:
-
ApiWrapper
- Object
- ApiWrapper
- BitstampApiWrapper
- Defined in:
- lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb
Overview
Wrapper implementation for Bitstamp API. www.bitstamp.net/api/
Constant Summary
Constants inherited from ApiWrapper
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#key ⇒ Object
Returns the value of attribute key.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #amount_and_quantity(order_id) ⇒ Object
- #balance ⇒ Object
- #balance_parser(balances, currency) ⇒ Object
-
#balance_summary_parser(balances) ⇒ Object
{ btc_reserved: ‘0’, btc_available: ‘0’, btc_balance: ‘0’, usd_reserved: ‘1.02, usd_available: ’6952.05’, usd_balance: ‘6953.07’, fee: ‘0.4000’ }.
- #currency_pair(order_book = '') ⇒ Object
- #find_lost(type, price, _quantity) ⇒ Object
-
#initialize(settings) ⇒ BitstampApiWrapper
constructor
A new instance of BitstampApiWrapper.
-
#order_book(retries = 20) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#order_book_parser(book) ⇒ Object
{ timestamp: ‘1380237884’, bids: [[‘124.55’, ‘1.58057006’], [‘124.40’, ‘14.91779125’]], asks: [[‘124.56’, ‘0.81888247’], [‘124.57’, ‘0.81078911’]] }.
- #order_is_done?(order) ⇒ Boolean
-
#order_parser(order) ⇒ Object
<Bitstamp::Order @id=‘76’, @type=0, @price=‘1.1’, @amount=‘1.0’, @datetime=‘2013-09-26 23:15:04’>.
- #order_summary_parser(orders) ⇒ Object
-
#orders ⇒ Object
rubocop:enable Metrics/AbcSize.
- #send_order(type, price, quantity) ⇒ Object
- #setup ⇒ Object
-
#transaction_parser(transaction) ⇒ Object
<Bitstamp::Transactions: @tid=‘1469074’, @price=‘126.95’, @amount=‘1.10000000’, @date=‘1380648951’>.
- #transactions ⇒ Object
-
#user_transaction_parser(user_transaction) ⇒ Object
<Bitstamp::UserTransaction: @usd=‘-373.51’, @btc=‘3.00781124’, @btc_usd=‘124.18’, @order_id=7623942, @fee=‘1.50’, @type=2, @id=1444404, @datetime=‘2013-09-26 13:28:55’ >.
- #user_transactions ⇒ Object
Methods inherited from ApiWrapper
#base, #base_quote, #cancel, #enough_order_size?, #name, #place_order, #quote
Constructor Details
#initialize(settings) ⇒ BitstampApiWrapper
Returns a new instance of BitstampApiWrapper.
6 7 8 9 10 11 12 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 6 def initialize(settings) self.key = settings.api_key self.secret = settings.secret self.client_id = settings.client_id currency_pair(settings.order_book) setup end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
4 5 6 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 4 def client_id @client_id end |
#key ⇒ Object
Returns the value of attribute key.
4 5 6 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 4 def key @key end |
#secret ⇒ Object
Returns the value of attribute secret.
4 5 6 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 4 def secret @secret end |
Instance Method Details
#amount_and_quantity(order_id) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 22 def amount_and_quantity(order_id) closes = user_transactions.select { |t| t.order_id.to_s == order_id } amount = closes.sum(&:fiat).abs quantity = closes.sum(&:crypto).abs [amount, quantity] end |
#balance ⇒ Object
30 31 32 33 34 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 30 def balance balance_summary_parser(Bitstamp.balance(currency_pair[:name]).symbolize_keys) rescue StandardError => e raise ApiWrapperError, "Bitstamp balance failed: #{e.}" end |
#balance_parser(balances, currency) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 93 def balance_parser(balances, currency) Balance.new( balances["#{currency}_balance".to_sym].to_d, balances["#{currency}_reserved".to_sym].to_d, balances["#{currency}_available".to_sym].to_d ) end |
#balance_summary_parser(balances) ⇒ Object
btc_reserved: '0', btc_available: '0', btc_balance: '0',
usd_reserved: '1.02, usd_available: '6952.05', usd_balance: '6953.07',
fee: '0.4000'
85 86 87 88 89 90 91 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 85 def balance_summary_parser(balances) BalanceSummary.new( balance_parser(balances, currency_pair[:base]), balance_parser(balances, currency_pair[:quote]), balances[:fee].to_d ) end |
#currency_pair(order_book = '') ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 145 def currency_pair(order_book = '') @currency_pair ||= { name: order_book, base: order_book.slice(0..2), quote: order_book.slice(3..5) } end |
#find_lost(type, price, _quantity) ⇒ Object
36 37 38 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 36 def find_lost(type, price, _quantity) orders.find { |o| o.type == type && o.price == price && o. >= 5.minutes.ago.to_i } end |
#order_book(retries = 20) ⇒ Object
rubocop:disable Metrics/AbcSize
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 41 def order_book(retries = 20) book = Bitstamp.order_book(currency_pair[:name]).deep_symbolize_keys age = Time.now.to_i - book[:timestamp].to_i return order_book_parser(book) if age <= 300 BitexBot::Robot.log(:info, "Refusing to continue as orderbook is #{age} seconds old") order_book(retries) rescue StandardError raise if retries.zero? BitexBot::Robot.log(:info, "Bitstamp order book failed, retrying #{retries} more times") BitexBot::Robot.sleep_for 1 order_book(retries - 1) end |
#order_book_parser(book) ⇒ Object
timestamp: '1380237884',
bids: [['124.55', '1.58057006'], ['124.40', '14.91779125']],
asks: [['124.56', '0.81888247'], ['124.57', '0.81078911']]
106 107 108 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 106 def order_book_parser(book) OrderBook.new(book[:timestamp].to_i, order_summary_parser(book[:bids]), order_summary_parser(book[:asks])) end |
#order_is_done?(order) ⇒ Boolean
110 111 112 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 110 def order_is_done?(order) order.nil? end |
#order_parser(order) ⇒ Object
<Bitstamp::Order @id=‘76’, @type=0, @price=‘1.1’, @amount=‘1.0’, @datetime=‘2013-09-26 23:15:04’>
115 116 117 118 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 115 def order_parser(order) type = order.type == '0' ? :buy : :sell Order.new(order.id.to_s, type, order.price.to_d, order.amount.to_d, order.datetime.to_datetime.to_i, order) end |
#order_summary_parser(orders) ⇒ Object
120 121 122 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 120 def order_summary_parser(orders) orders.map { |order| OrderSummary.new(order[0].to_d, order[1].to_d) } end |
#orders ⇒ Object
rubocop:enable Metrics/AbcSize
57 58 59 60 61 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 57 def orders Bitstamp.orders.all(currency_pair: currency_pair[:name]).map { |o| order_parser(o) } rescue StandardError => e raise ApiWrapperError, "Bitstamp orders failed: #{e.}" end |
#send_order(type, price, quantity) ⇒ Object
63 64 65 66 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 63 def send_order(type, price, quantity) order = Bitstamp.orders.send(type, currency_pair: currency_pair[:name], amount: quantity.round(4), price: price.round(2)) order_parser(order) unless order.error.present? end |
#setup ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 14 def setup Bitstamp.setup do |config| config.key = key config.secret = secret config.client_id = client_id end end |
#transaction_parser(transaction) ⇒ Object
<Bitstamp::Transactions: @tid=‘1469074’, @price=‘126.95’, @amount=‘1.10000000’, @date=‘1380648951’>
125 126 127 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 125 def transaction_parser(transaction) Transaction.new(transaction.tid, transaction.price.to_d, transaction.amount.to_d, transaction.date.to_i, transaction) end |
#transactions ⇒ Object
68 69 70 71 72 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 68 def transactions Bitstamp.transactions(currency_pair[:name]).map { |t| transaction_parser(t) } rescue StandardError => e raise ApiWrapperError, "Bitstamp transactions failed: #{e.}" end |
#user_transaction_parser(user_transaction) ⇒ Object
<Bitstamp::UserTransaction:
@usd='-373.51', @btc='3.00781124', @btc_usd='124.18', @order_id=7623942, @fee='1.50', @type=2, @id=1444404,
@datetime='2013-09-26 13:28:55'
>
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 133 def user_transaction_parser(user_transaction) UserTransaction.new( user_transaction.order_id, user_transaction.send(quote).to_d, user_transaction.send(base).to_d, user_transaction.send(base_quote).to_d, user_transaction.fee.to_d, user_transaction.type.to_i, Time.parse(user_transaction.datetime).to_i ) end |
#user_transactions ⇒ Object
74 75 76 77 78 |
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 74 def user_transactions Bitstamp.user_transactions.all(currency_pair: currency_pair[:name]).map { |ut| user_transaction_parser(ut) } rescue StandardError => e raise ApiWrapperError, "Bitstamp user_transactions failed: #{e.}" end |