Class: Fyb::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/fyb/order.rb

Overview

Handles a Fyb order.

order = Order.new 1, :market, :buy
order.perform

or

order = Order.new 1, 1234, :sell
order.perform

Constant Summary collapse

FEE =
BigDecimal '0.008'
ORDER_TYPES =
{ buy: 'B', sell: 'S', undefined: nil }

Instance Attribute Summary collapse

Instance Method Summary collapse

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_idObject (readonly)

Returns the value of attribute order_id.



12
13
14
# File 'lib/fyb/order.rb', line 12

def order_id
  @order_id
end

#priceObject (readonly)

Returns the value of attribute price.



12
13
14
# File 'lib/fyb/order.rb', line 12

def price
  @price
end

#qtyObject (readonly)

Returns the value of attribute qty.



12
13
14
# File 'lib/fyb/order.rb', line 12

def qty
  @qty
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



12
13
14
# File 'lib/fyb/order.rb', line 12

def timestamp
  @timestamp
end

#typeObject (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_feeObject



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

Returns:

  • (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

#performObject



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_feeObject



27
28
29
# File 'lib/fyb/order.rb', line 27

def qty_after_fee
  @qty * (BigDecimal(1) - FEE)
end