Class: Fyb::Order
- Inherits:
-
Object
- Object
- Fyb::Order
- Defined in:
- lib/fyb/order.rb
Overview
Constant Summary collapse
- FEE =
BigDecimal '0.008'
- ORDER_TYPES =
{ buy: 'B', sell: 'S', undefined: nil }
Instance Attribute Summary collapse
-
#order_id ⇒ Object
readonly
Returns the value of attribute order_id.
-
#price ⇒ Object
readonly
Returns the value of attribute price.
-
#qty ⇒ Object
readonly
Returns the value of attribute qty.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #cancel! ⇒ Object
-
#initialize(qty, price, type, order_id = nil) ⇒ Order
constructor
A new instance of Order.
- #money_after_fee ⇒ Object
- #pending? ⇒ Boolean
- #perform ⇒ Object
- #qty_after_fee ⇒ Object
Constructor Details
#initialize(qty, price, type, order_id = nil) ⇒ Order
Returns a new instance of Order.
17 18 19 20 21 22 23 24 25 |
# File 'lib/fyb/order.rb', line 17 def initialize(qty, price, type, order_id = nil) fail ArgumentError, 'type must be :buy or :sell' unless ORDER_TYPES.keys.member? type @qty = BigDecimal(qty, 8) @price = BigDecimal(price, 2) unless price == :market @price ||= price @type = ORDER_TYPES[type] @order_id = order_id end |
Instance Attribute Details
#order_id ⇒ Object (readonly)
Returns the value of attribute order_id.
12 13 14 |
# File 'lib/fyb/order.rb', line 12 def order_id @order_id end |
#price ⇒ Object (readonly)
Returns the value of attribute price.
12 13 14 |
# File 'lib/fyb/order.rb', line 12 def price @price end |
#qty ⇒ Object (readonly)
Returns the value of attribute qty.
12 13 14 |
# File 'lib/fyb/order.rb', line 12 def qty @qty end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
12 13 14 |
# File 'lib/fyb/order.rb', line 12 def @timestamp end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
12 13 14 |
# File 'lib/fyb/order.rb', line 12 def type @type end |
Instance Method Details
#cancel! ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/fyb/order.rb', line 54 def cancel! return false if @order_id.nil? body = Fyb.private.cancelpendingorder(orderNo: @order_id).perform.parse error = body['error'] fail Exception, error unless error == 0 true end |
#money_after_fee ⇒ Object
31 32 33 34 35 36 |
# File 'lib/fyb/order.rb', line 31 def money_after_fee price = @price price = @type == 'B' ? Fyb.ask : Fyb.bid if price == :market qty_after_fee.in_money(price) end |
#pending? ⇒ Boolean
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fyb/order.rb', line 64 def pending? return false if @order_id.nil? body = Fyb.private.getpendingorders.perform.parse error = body['error'] fail Exception, error unless error == 0 return false if body['orders'].nil? body['orders'].each do |order| return true if order['ticket'] == @order_id end false end |
#perform ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fyb/order.rb', line 38 def perform return self unless @order_id.nil? || @type.nil? future do @price = @type == 'B' ? Fyb.ask : Fyb.bid if @price == :market body = Fyb.private.placeorder(qty: @qty.btc, price: @price.money, type: @type).perform.parse error = body['error'] fail Exception, error unless error == 0 @order_id = body['pending_oid'].to_i self end end |
#qty_after_fee ⇒ Object
27 28 29 |
# File 'lib/fyb/order.rb', line 27 def qty_after_fee @qty * (BigDecimal(1) - FEE) end |