Class: KrakenOrder
- Inherits:
-
Object
- Object
- KrakenOrder
- Defined in:
- lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb
Overview
Wrapper for Kraken orders.
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#avg_price ⇒ Object
Returns the value of attribute avg_price.
-
#datetime ⇒ Object
Returns the value of attribute datetime.
-
#executed_amount ⇒ Object
Returns the value of attribute executed_amount.
-
#id ⇒ Object
Returns the value of attribute id.
-
#price ⇒ Object
Returns the value of attribute price.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.amount_and_quantity(order_id) ⇒ Object
[BigDecimal, BigDecimal].
- .closed(start: 1.hour.ago.to_i) ⇒ Object
-
.closed_order_by(type, price, quantity) ⇒ Object
description: [type, price, quantity].
-
.create!(type, price, quantity) ⇒ Object
rubocop:disable Metrics/AbcSize.
- .find(id) ⇒ Object
- .find_lost(type, price, quantity) ⇒ Object
- .log_and_return(order, status) ⇒ Object
- .open ⇒ Object
-
.open_order_by(type, price, quantity) ⇒ Object
description: [type, price, quantity].
-
.place_order(type, price, quantity) ⇒ Object
<KrakenOrder:0x007faf255382d0 @id=“OGZ3HI-5I322-OIOV52”, @type=:sell, @datetime=1546971756, @amount=0.248752e-2, @executed_amount=0.248752e-2, @price=0.40025e4, @avg_price=0.40074e4>.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cancel! ⇒ Object
-
#initialize(id, order_data) ⇒ KrakenOrder
constructor
id: ‘O5TDV2-WDYB2-6OGJRD’ order_data: { ‘refid’: nil, ‘userref’: nil, ‘status’: ‘open’, ‘opentm’: 1440292821.4839, ‘starttm’: 0, ‘expiretm’: 0, ‘descr’: { ‘pair’: ‘ETHEUR’, ‘type’: ‘buy’, ‘ordertype’: ‘limit’, ‘price’: ‘1.19000’, ‘price2’: ‘0’, ‘leverage’: ‘none’, ‘order’: ‘buy 1204.00000000 ETHEUR @ limit 1.19000’ }, ‘vol’: ‘1204.00000000’, ‘vol_exec’: ‘0.00000000’, ‘cost’: ‘0.00000’, ‘fee’: ‘0.00000’, ‘price’: ‘0.00000’, ‘misc’: ”, ‘oflags’: ‘fciq’ } }.
Constructor Details
#initialize(id, order_data) ⇒ KrakenOrder
id: ‘O5TDV2-WDYB2-6OGJRD’ order_data: {
'refid': nil, 'userref': nil, 'status': 'open', 'opentm': 1440292821.4839, 'starttm': 0, 'expiretm': 0,
'descr': {
'pair': 'ETHEUR', 'type': 'buy', 'ordertype': 'limit', 'price': '1.19000', 'price2': '0', 'leverage': 'none',
'order': 'buy 1204.00000000 ETHEUR @ limit 1.19000'
},
'vol': '1204.00000000', 'vol_exec': '0.00000000', 'cost': '0.00000', 'fee': '0.00000', 'price': '0.00000',
'misc': '', 'oflags': 'fciq'
}
}
103 104 105 106 107 108 109 110 111 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 103 def initialize(id, order_data) self.id = id self.type = order_data[:descr][:type].to_sym self.datetime = order_data[:opentm].to_i self.amount = order_data[:vol].to_d self.executed_amount = order_data[:vol_exec].to_d self.price = order_data[:descr][:price].to_d self.avg_price = order_data[:price].to_d end |
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
6 7 8 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 6 def amount @amount end |
#avg_price ⇒ Object
Returns the value of attribute avg_price.
6 7 8 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 6 def avg_price @avg_price end |
#datetime ⇒ Object
Returns the value of attribute datetime.
6 7 8 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 6 def datetime @datetime end |
#executed_amount ⇒ Object
Returns the value of attribute executed_amount.
6 7 8 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 6 def executed_amount @executed_amount end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 6 def id @id end |
#price ⇒ Object
Returns the value of attribute price.
6 7 8 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 6 def price @price end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 6 def type @type end |
Class Method Details
.amount_and_quantity(order_id) ⇒ Object
- BigDecimal, BigDecimal
47 48 49 50 51 52 53 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 47 def self.amount_and_quantity(order_id) order = find(order_id) amount = order.avg_price * order.executed_amount quantity = order.executed_amount [amount, quantity] end |
.closed(start: 1.hour.ago.to_i) ⇒ Object
61 62 63 64 65 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 61 def self.closed(start: 1.hour.ago.to_i) api_wrapper.client.private.closed_orders(start: start)[:closed].map { |o| new(*o) } rescue KrakenClient::ErrorResponse retry end |
.closed_order_by(type, price, quantity) ⇒ Object
description: [type, price, quantity]
88 89 90 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 88 def self.closed_order_by(type, price, quantity) closed(start: last_closed_order).detect { |order| order == [type, price, quantity] } end |
.create!(type, price, quantity) ⇒ Object
rubocop:disable Metrics/AbcSize
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 9 def self.create!(type, price, quantity) self.last_closed_order = closed.first.try(:id) || Time.now.to_i order = place_order(type, price.truncate(1), quantity.truncate(8)) order_id = order['txid'].first find(order_id) rescue KrakenClient::ErrorResponse => e # Order could not be placed if e. == 'EService:Unavailable' BitexBot::Robot.log(:debug, 'Captured EService:Unavailable error when placing order on Kraken. Retrying...') retry elsif e..start_with?('EGeneral:Invalid') BitexBot::Robot.log(:debug, "Captured #{e.}: type: #{type}, price: #{price}, quantity: #{quantity}") raise OrderArgumentError, e. elsif e. != 'error' raise end end |
.find(id) ⇒ Object
40 41 42 43 44 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 40 def self.find(id) new(*api_wrapper.client.private.query_orders(txid: id).first) rescue KrakenClient::ErrorResponse retry end |
.find_lost(type, price, quantity) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 67 def self.find_lost(type, price, quantity) BitexBot::Robot.log(:debug, "Looking for #{type} order in open orders...") order = open_order_by(type, price.truncate(2), quantity.truncate(8)) return log_and_return(order, :open) if order.present? BitexBot::Robot.log(:debug, "Looking for #{type} order in closed orders...") order = closed_order_by(type, price.truncate(2), quantity.truncate(8)) return log_and_return(order, :closed) if order.present? && order.id != last_closed_order end |
.log_and_return(order, status) ⇒ Object
77 78 79 80 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 77 def self.log_and_return(order, status) BitexBot::Robot.log(:debug, "Found open #{status} with ID #{order.id}") order end |
.open ⇒ Object
55 56 57 58 59 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 55 def self.open api_wrapper.client.private.open_orders['open'].map { |o| new(*o) } rescue KrakenClient::ErrorResponse retry end |
.open_order_by(type, price, quantity) ⇒ Object
description: [type, price, quantity]
83 84 85 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 83 def self.open_order_by(type, price, quantity) open.detect { |order| order == [type, price, quantity] } end |
.place_order(type, price, quantity) ⇒ Object
<KrakenOrder:0x007faf255382d0 @id=“OGZ3HI-5I322-OIOV52”, @type=:sell, @datetime=1546971756, @amount=0.248752e-2,
@executed_amount=0.248752e-2, @price=0.40025e4, @avg_price=0.40074e4>
30 31 32 33 34 35 36 37 38 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 30 def self.place_order(type, price, quantity) api_wrapper.client.private.add_order( pair: api_wrapper.currency_pair[:altname], type: type, ordertype: 'limit', price: price, volume: quantity ) end |
Instance Method Details
#==(other) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 119 def ==(other) if other.is_a?(self.class) other.id == id elsif other.is_a?(Array) other == [type, price, amount] end end |
#cancel! ⇒ Object
113 114 115 116 117 |
# File 'lib/bitex_bot/models/api_wrappers/kraken/kraken_order.rb', line 113 def cancel! api_wrapper.client.private.cancel_order(txid: id) rescue KrakenClient::ErrorResponse => e e. == 'EService:Unavailable' ? retry : raise end |