Class: ApiWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/bitex_bot/models/api_wrappers/api_wrapper.rb

Overview

This class represents the general behaviour for trading platform wrappers.

Defined Under Namespace

Classes: Balance, BalanceSummary, Order, OrderBook, OrderSummary, Transaction, UserTransaction

Constant Summary collapse

MIN_AMOUNT =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#currency_pairObject

Returns the value of attribute currency_pair.



3
4
5
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 3

def currency_pair
  @currency_pair
end

Instance Method Details

#amount_and_quantity(_order_id) ⇒ Array<Decimal, Decimal>

From an order when you buy or sell, when you place an order and it matches, you can match more than one order.

Parameters:

  • order_id
  • transactions:

    all matches for a purchase or sale order.

Returns:

  • (Array<Decimal, Decimal>)


143
144
145
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 143

def amount_and_quantity(_order_id)
  raise 'self subclass responsibility'
end

#balanceBalanceSummary

Returns:



80
81
82
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 80

def balance
  raise 'self subclass responsibility'
end

#baseObject



155
156
157
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 155

def base
  currency_pair[:base]
end

#base_quoteObject



151
152
153
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 151

def base_quote
  "#{base}_#{quote}"
end

#cancelnil

Returns:

  • (nil)


85
86
87
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 85

def cancel
  raise 'self subclass responsibility'
end

#enough_order_size?(quantity, price) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 147

def enough_order_size?(quantity, price)
  quantity * price > MIN_AMOUNT
end

#find_lost(_type, _price, _quantity) ⇒ Object

Hook Method - arguments could not be used in their entirety by the subclasses

Parameters:

  • order_type (String)

    buy|sell

  • price (Decimal)
  • quantity (Decimal)


134
135
136
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 134

def find_lost(_type, _price, _quantity)
  raise 'self subclass responsibility'
end

#nameObject



65
66
67
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 65

def name
  self.class.name.underscore.split('_').first.capitalize
end

#order_book(_retries = 20) ⇒ OrderBook

Returns:



75
76
77
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 75

def order_book(_retries = 20)
  raise 'self subclass responsibility'
end

#ordersArray<Order>

Returns:



90
91
92
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 90

def orders
  raise 'self subclass responsibility'
end

#place_order(type, price, quantity) ⇒ Object

rubocop:disable Metrics/AbcSize

Parameters:

  • type
  • price
  • quantity

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 103

def place_order(type, price, quantity)
  order = send_order(type, price, quantity)
  return order unless order.nil? || order.id.nil?

  BitexBot::Robot.log(:debug, "Captured error when placing order on #{name}")
  # Order may have gone through and be stuck somewhere in Wrapper's pipeline.
  # We just sleep for a bit and then look for the order.
  5.times do |i|
    BitexBot::Robot.log(
      :info,
      "#{name} cauldn't place #{type} order #{i} times for #{base.upcase} #{quantity} @ #{quote.upcase} #{price}.\n"\
      "Going to sleep 10 seconds.\n"
    )
    BitexBot::Robot.sleep_for(15)
    order = find_lost(type, price, quantity)
    return order if order.present?
  end
  raise OrderNotFound, "Closing: #{type} order not found for #{base.upcase} #{quantity} @ #{quote.upcase} #{price}."
end

#quoteObject



159
160
161
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 159

def quote
  currency_pair[:quote]
end

#send_order(_type, _price, _quantity) ⇒ Object

Hook Method - arguments could not be used in their entirety by the subclasses



125
126
127
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 125

def send_order(_type, _price, _quantity)
  raise 'self subclass responsibility'
end

#transactionsArray<Transaction>

Returns:



70
71
72
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 70

def transactions
  raise 'self subclass responsibility'
end

#user_transactionsUserTransaction

Returns:



95
96
97
# File 'lib/bitex_bot/models/api_wrappers/api_wrapper.rb', line 95

def user_transactions
  raise 'self subclass responsibility'
end