Class: BitstampApiWrapper

Inherits:
ApiWrapper show all
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

ApiWrapper::MIN_AMOUNT

Class Method Summary collapse

Methods inherited from ApiWrapper

enough_order_size?, place_order, user_transacitions

Class Method Details

.amount_and_quantity(order_id, transactions) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 12

def self.amount_and_quantity(order_id, transactions)
  closes = transactions.select { |t| t.order_id.to_s == order_id }
  amount = closes.map { |c| c.usd.to_d }.sum.abs
  quantity = closes.map { |c| c.btc.to_d }.sum.abs

  [amount, quantity]
end

.balanceObject



20
21
22
23
24
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 20

def self.balance
  balance_summary_parser(Bitstamp.balance.symbolize_keys)
rescue StandardError => e
  raise ApiWrapperError, "Bitstamp balance failed: #{e.message}"
end

.balance_parser(balances, currency) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 86

def self.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'



82
83
84
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 82

def self.balance_summary_parser(balances)
  BalanceSummary.new(balance_parser(balances, :btc), balance_parser(balances, :usd), balances[:fee].to_d)
end

.cancel(order) ⇒ Object



26
27
28
29
30
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 26

def self.cancel(order)
  Bitstamp::Order.new(id: order.id).cancel!
rescue StandardError => e
  raise ApiWrapperError, "Bitstamp cancel! failed: #{e.message}"
end

.find_lost(type, price, _quantity) ⇒ Object



32
33
34
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 32

def self.find_lost(type, price, _quantity)
  orders.find { |o| o.type == type && o.price == price && o.timestamp >= 5.minutes.ago.to_i }
end

.order_book(retries = 20) ⇒ Object

rubocop:disable Metrics/AbcSize



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 37

def self.order_book(retries = 20)
  book = Bitstamp.order_book.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']]



99
100
101
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 99

def self.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



103
104
105
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 103

def self.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’>



108
109
110
111
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 108

def self.order_parser(order)
  type = order.type.zero? ? :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



113
114
115
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 113

def self.order_summary_parser(orders)
  orders.map { |order| OrderSummary.new(order[0].to_d, order[1].to_d) }
end

.ordersObject

rubocop:enable Metrics/AbcSize



53
54
55
56
57
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 53

def self.orders
  Bitstamp.orders.all.map { |o| order_parser(o) }
rescue StandardError => e
  raise ApiWrapperError, "Bitstamp orders failed: #{e.message}"
end

.send_order(type, price, quantity) ⇒ Object



59
60
61
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 59

def self.send_order(type, price, quantity)
  Bitstamp.orders.send(type, amount: quantity.round(4), price: price.round(2))
end

.setup(settings) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 4

def self.setup(settings)
  Bitstamp.setup do |config|
    config.key = settings.api_key
    config.secret = settings.secret
    config.client_id = settings.client_id
  end
end

.transaction_parser(transaction) ⇒ Object

<Bitstamp::Transactions: @tid=1469074, @price=‘126.95’, @amount=‘1.10000000’, @date=‘1380648951’>



118
119
120
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 118

def self.transaction_parser(transaction)
  Transaction.new(transaction.tid, transaction.price.to_d, transaction.amount.to_d, transaction.date.to_i)
end

.transactionsObject



63
64
65
66
67
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 63

def self.transactions
  Bitstamp.transactions.map { |t| transaction_parser(t) }
rescue StandardError => e
  raise ApiWrapperError, "Bitstamp transactions failed: #{e.message}"
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'

>



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 126

def self.user_transaction_parser(user_transaction)
  UserTransaction.new(
    user_transaction.order_id,
    user_transaction.usd.to_d,
    user_transaction.btc.to_d,
    user_transaction.btc_usd.to_d,
    user_transaction.fee.to_d,
    user_transaction.type,
    Time.new(user_transaction.datetime).to_i
  )
end

.user_transactionsObject



69
70
71
72
73
# File 'lib/bitex_bot/models/api_wrappers/bitstamp/bitstamp_api_wrapper.rb', line 69

def self.user_transactions
  Bitstamp.user_transactions.all.map { |ut| user_transaction_parser(ut) }
rescue StandardError => e
  raise ApiWrapperError, "Bitstamp user_transactions failed: #{e.message}"
end