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,
  :mts_tif => 10,
  # placeholder
  :flags => 12,
  :status => 13,
  # placeholder
  # placeholder
  :price => 16,
  :price_avg => 17,
  :price_trailing => 18,
  :price_aux_limit => 19,
  # placeholder
  # placeholder
  # placeholder
  :notify => 23,
  :hidden => 24,
  :placed_id => 25,
  # placeholder
  # placeholder
  :routing => 28,
  # placeholder
  # placeholder
  :meta => 31
}
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.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/models/order.rb', line 63

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)
    if data.has_key?(:lev)
      @lev = data[:lev]
    end
  end
end

Instance Attribute Details

#last_amountObject

Returns the value of attribute last_amount.



56
57
58
# File 'lib/models/order.rb', line 56

def last_amount
  @last_amount
end

#levObject

Returns the value of attribute lev.



56
57
58
# File 'lib/models/order.rb', line 56

def lev
  @lev
end

Class Method Details

.gen_cidObject



58
59
60
61
# File 'lib/models/order.rb', line 58

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

.unserialize(data) ⇒ Object



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

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

Instance Method Details

#get_base_currencyObject



148
149
150
# File 'lib/models/order.rb', line 148

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

#get_last_fill_amountObject



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

def get_last_fill_amount
  @last_amount - @amount
end

#get_notional_valueObject



156
157
158
# File 'lib/models/order.rb', line 156

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

#get_quote_currencyObject



152
153
154
# File 'lib/models/order.rb', line 152

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

#includes_variable_ratesObject



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

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

#is_hiddenObject



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

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

#is_ocoObject



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

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

#is_partially_filledObject



160
161
162
163
# File 'lib/models/order.rb', line 160

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

#is_position_closeObject



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

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

#is_post_onlyObject



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

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

#is_reduce_onlyObject



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

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

#modify_flag(flag, active) ⇒ Object



84
85
86
87
88
# File 'lib/models/order.rb', line 84

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

  @flags += active ? flag : -flag
end

#reset_fill_amountObject



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

def reset_fill_amount
  @last_amount = @amount
end

#set_hidden(v) ⇒ Object



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

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

#set_no_variable_rates(v) ⇒ Object



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

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

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



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

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



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

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

#set_post_only(v) ⇒ Object



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

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

#set_reduce_only(v) ⇒ Object



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

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

#to_new_order_packetObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/models/order.rb', line 165

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

  data = {
    :cid => cid,
    :symbol => @symbol,
    :type => @type,
    :amount => BigDecimal.new(@amount, 8).to_s,
    :flags => @flags || 0,
    :meta => @meta
  }
  if !@gid.nil?
    data[:gid] = @gid
  end
  if !@lev.nil?
    data[:lev] = @lev
  end

  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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/models/order.rb', line 201

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