Class: Bitfinex::Models::Order

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

Constant Summary collapse

BOOL_FIELDS =
[]
FIELDS =
{
  :id => 0,
  :gid => 1,
  :cid => 2,
  :symbol => 3,
  :mts_create => 4,
  :mts_update => 5,
  :amount => 6,
  :amount_orig => 7,
  :type => 8,
  :type_prev => 9,
  :flags => 12,
  :status => 13,
  :price => 16,
  :price_avg => 17,
  :price_trailing => 18,
  :price_aux_limit => 19,
  :notify => 23,
  :placed_id => 25
}
FLAG_OCO =

16384

2 ** 14
FLAG_POSTONLY =

4096

2 ** 12
FLAG_HIDDEN =

64

2 ** 6
FLAG_NO_VR =

524288

2 ** 19
FLAG_POS_CLOSE =

512

2 ** 9
FLAG_REDUCE_ONLY =

1024

2 ** 10
@@last_cid =
Time.now.to_i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#apply, #serialize

Constructor Details

#initialize(data) ⇒ Order

Returns a new instance of Order.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/models/order.rb', line 49

def initialize (data)
  super(data, FIELDS, BOOL_FIELDS)

  @flags = 0 unless @flags.is_a?(Numeric)
  @amount_orig = @amount if @amount_orig.nil? && !@amount.nil?
  @last_amount = @amount

  if data.kind_of?(Hash)
    set_oco(data[:oco]) if data.has_key?(:oco)
    set_hidden(data[:hidden]) if data.has_key?(:hidden)
    set_post_only(data[:post_only]) if data.has_key?(:post_only)
  end
end

Instance Attribute Details

#last_amountObject

Returns the value of attribute last_amount.



42
43
44
# File 'lib/models/order.rb', line 42

def last_amount
  @last_amount
end

#metaObject

Returns the value of attribute meta.



42
43
44
# File 'lib/models/order.rb', line 42

def meta
  @meta
end

Class Method Details

.gen_cidObject



44
45
46
47
# File 'lib/models/order.rb', line 44

def self.gen_cid
  @@last_cid += 1
  @@last_cid
end

.unserialize(data) ⇒ Object



63
64
65
# File 'lib/models/order.rb', line 63

def self.unserialize (data)
  return Model.unserialize(data, FIELDS, BOOL_FIELDS)
end

Instance Method Details

#get_base_currencyObject



131
132
133
# File 'lib/models/order.rb', line 131

def get_base_currency
  @symbol[1...4]
end

#get_last_fill_amountObject



123
124
125
# File 'lib/models/order.rb', line 123

def get_last_fill_amount
  @last_amount - @amount
end

#get_notional_valueObject



139
140
141
# File 'lib/models/order.rb', line 139

def get_notional_value
  (@amount * @price).abs
end

#get_quote_currencyObject



135
136
137
# File 'lib/models/order.rb', line 135

def get_quote_currency
  @symbol[4..-1]
end

#includes_variable_ratesObject



111
112
113
# File 'lib/models/order.rb', line 111

def includes_variable_rates
  return !!(@flags & FLAG_NO_VR)
end

#is_hiddenObject



103
104
105
# File 'lib/models/order.rb', line 103

def is_hidden
  return !!(@flags & FLAG_HIDDEN)
end

#is_ocoObject



99
100
101
# File 'lib/models/order.rb', line 99

def is_oco
  return !!(@flags & FLAG_OCO)
end

#is_partially_filledObject



143
144
145
146
# File 'lib/models/order.rb', line 143

def is_partially_filled
  a = @amount.abs
  a > 0 && a < @amount_orig.abs
end

#is_position_closeObject



115
116
117
# File 'lib/models/order.rb', line 115

def is_position_close
  return !!(@flags & FLAG_POS_CLOSE)
end

#is_post_onlyObject



107
108
109
# File 'lib/models/order.rb', line 107

def is_post_only
  return !!(@flags & FLAG_POSTONLY)
end

#is_reduce_onlyObject



119
120
121
# File 'lib/models/order.rb', line 119

def is_reduce_only
  return !!(@flags & FLAG_REDUCE_ONLY)
end

#modify_flag(flag, active) ⇒ Object



67
68
69
70
71
# File 'lib/models/order.rb', line 67

def modify_flag (flag, active)
  return if (@flags & flag != 0) == active

  @flags += active ? flag : -flag
end

#reset_fill_amountObject



127
128
129
# File 'lib/models/order.rb', line 127

def reset_fill_amount
  @last_amount = @amount
end

#set_hidden(v) ⇒ Object



79
80
81
# File 'lib/models/order.rb', line 79

def set_hidden (v)
  modify_flag(FLAG_HIDDEN, v)
end

#set_no_variable_rates(v) ⇒ Object



87
88
89
# File 'lib/models/order.rb', line 87

def set_no_variable_rates (v)
  modify_flag(FLAG_NO_VR, v)
end

#set_oco(v, stop_price = @price_aux_limit) ⇒ Object



73
74
75
76
77
# File 'lib/models/order.rb', line 73

def set_oco (v, stop_price = @price_aux_limit)
  @price_aux_limit = stop_price if v

  modify_flag(FLAG_OCO, v)
end

#set_position_close(v) ⇒ Object



91
92
93
# File 'lib/models/order.rb', line 91

def set_position_close (v)
  modify_flag(FLAG_POS_CLOSE, v)
end

#set_post_only(v) ⇒ Object



83
84
85
# File 'lib/models/order.rb', line 83

def set_post_only (v)
  modify_flag(FLAG_POSTONLY, v)
end

#set_reduce_only(v) ⇒ Object



95
96
97
# File 'lib/models/order.rb', line 95

def set_reduce_only (v)
  modify_flag(FLAG_REDUCE_ONLY, v)
end

#to_new_order_packetObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/models/order.rb', line 148

def to_new_order_packet
  if !@cid.nil?
    cid = @cid
  else
    cid = Order.gen_cid
  end

  data = {
    :gid => @gid,
    :cid => cid,
    :symbol => @symbol,
    :type => @type,
    :amount => BigDecimal.new(@amount, 8).to_s,
    :flags => @flags || 0,
    :meta => @meta
  }

  data[:price] = BigDecimal.new(@price, 5).to_s if !@price.nil?
  data[:price_trailing] = BigDecimal.new(@price_trailing, 5).to_s if !@price_trailing.nil?

  if !@price_aux_limit.nil?
    if is_oco
      data[:price_oco_stop] = BigDecimal.new(@price_aux_limit, 5).to_s
    else
      data[:price_aux_limit] = BigDecimal.new(@price_aux_limit, 5).to_s
    end
  end

  data
end

#update(changes = {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/models/order.rb', line 179

def update (changes = {})
  changes.each do |k, v|
    return if k == 'id'

    if FIELDS.has_key?(k)
      instance_variable_set(k, v)
    elsif k == 'price_trailing'
      @price_trailing = v.to_f
    elsif k == 'price_oco_stop' || k == 'price_aux_limit'
      @price_aux_limit = v.to_f
    elsif k == 'delta' && v.is_a?(Numeric) && @amount.is_a?(Numeric)
      @amount += v.to_f
      @last_amount = @amount
    end
  end
end