Class: ItBitSDK::Trading::Order

Inherits:
Base
  • Object
show all
Defined in:
lib/it_bit_sdk/trading/order.rb

Constant Summary

Constants inherited from Base

Base::BASE_URL

Instance Method Summary collapse

Methods inherited from Base

#next_nonce, #send_request, #timestamp

Instance Method Details

#cancel!(id, wallet_id) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/it_bit_sdk/trading/order.rb', line 52

def cancel!(id, wallet_id)
  send_request(:delete, "/wallets/#{wallet_id}/orders/#{id}")
  self.status = :cancelling
  return self
rescue RestClient::UnprocessableEntity
  return nil
end

#create!(wallet_id, side, instrument, amount, price, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/it_bit_sdk/trading/order.rb', line 22

def create!(wallet_id, side, instrument, amount, price, options = {})
  params = {
    side: side,
    instrument: instrument.to_s.upcase,
    amount: amount.to_d.to_s('F'),
    price: price.to_d.to_s('F'),
    type: options[:type] || :limit,
    currency: options[:currency].try(:upcase) || 'XBT'
  }
  %w(metadata client_order_identifier).each do |a|
    params[a.camelize(:lower)] = options[a.to_sym] if options[a.to_sym]
  end

  order = ::ItBitSDK::Order.new(params: send_request(:post, "/wallets/#{wallet_id}/orders", params))
  retries = 0
  while options[:wait] && order.status == :submitted
    sleep 0.2
    begin
      order = order(order.id, wallet_id)
    rescue StandardError => e
    end
    retries += 1
    if retries > 5000 # Wait 15 minutes for the order to be accepted.
      raise StandardError.new(
        "Timed out waiting for #{base_path} ##{order.id}")
    end
  end
  return order
end

#order(id, wallet_id) ⇒ Object



18
19
20
# File 'lib/it_bit_sdk/trading/order.rb', line 18

def order(id, wallet_id)
  ::ItBitSDK::Order.new(params: send_request(:get, "/wallets/#{wallet_id}/orders/#{id}"))
end

#orders(wallet_id, opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/it_bit_sdk/trading/order.rb', line 7

def orders(wallet_id, opts = {})
  params = {}
  params[:instrument] = opts[:instrument].upcase if opts[:instrument]
  params[:page] = opts[:page].to_i if opts[:page]
  params[:perPage] = opts[:per_page].to_i if opts[:per_page]
  params[:status] = opts[:status] if opts[:status]

  send_request(:get, "/wallets/#{wallet_id}/orders", params)
    .collect { |o| ::ItBitSDK::Order.new(params: o) }
end