Class: Itbit::Order

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

Overview

A limit buy or sell order, translated from itbit api to a more suitable ruby style. Field names are underscored. Values for side, instrument, currency, type and status are underscored and symbolized.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Order

Returns a new instance of Order.



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

def initialize(attrs)
  attrs.each{|k, v| send("#{k.underscore}=", v)}
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



7
8
9
# File 'lib/itbit/order.rb', line 7

def amount
  @amount
end

#amount_filledObject

Returns the value of attribute amount_filled.



7
8
9
# File 'lib/itbit/order.rb', line 7

def amount_filled
  @amount_filled
end

#client_order_identifierObject

Returns the value of attribute client_order_identifier.



7
8
9
# File 'lib/itbit/order.rb', line 7

def client_order_identifier
  @client_order_identifier
end

#created_timeObject

Returns the value of attribute created_time.



7
8
9
# File 'lib/itbit/order.rb', line 7

def created_time
  @created_time
end

#currencyObject

Returns the value of attribute currency.



7
8
9
# File 'lib/itbit/order.rb', line 7

def currency
  @currency
end

#display_amountObject

Returns the value of attribute display_amount.



7
8
9
# File 'lib/itbit/order.rb', line 7

def display_amount
  @display_amount
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/itbit/order.rb', line 7

def id
  @id
end

#instrumentObject

Returns the value of attribute instrument.



7
8
9
# File 'lib/itbit/order.rb', line 7

def instrument
  @instrument
end

#metadataObject

Returns the value of attribute metadata.



7
8
9
# File 'lib/itbit/order.rb', line 7

def 
  @metadata
end

#priceObject

Returns the value of attribute price.



7
8
9
# File 'lib/itbit/order.rb', line 7

def price
  @price
end

#sideObject

Returns the value of attribute side.



7
8
9
# File 'lib/itbit/order.rb', line 7

def side
  @side
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/itbit/order.rb', line 7

def status
  @status
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/itbit/order.rb', line 7

def type
  @type
end

#volume_weighted_average_priceObject

Returns the value of attribute volume_weighted_average_price.



7
8
9
# File 'lib/itbit/order.rb', line 7

def volume_weighted_average_price
  @volume_weighted_average_price
end

#wallet_idObject

Returns the value of attribute wallet_id.



7
8
9
# File 'lib/itbit/order.rb', line 7

def wallet_id
  @wallet_id
end

Class Method Details

.all(opts = {}) ⇒ Array<Itbit::Order>

Lists all orders for a wallet. Results can be filtered and paginated by passing in these options.

wallet_id: String, defaults to Itbit.default_wallet_id 
instrument: Symbol, either :xbtusd, :xbteur, :xbtsgd
page: Integer, starting page for pagination.
per_page: Integer, how many to show per page.
status: Symbol, either :submitted, :open, :filled, :cancelled, :rejected

Returns:



41
42
43
44
45
46
47
48
49
50
# File 'lib/itbit/order.rb', line 41

def self.all(opts = {})
  wallet_id = opts[:wallet_id] || Itbit.default_wallet_id
  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]
  Api.request(:get, "/wallets/#{wallet_id}/orders", params)
    .collect{|o| Order.new(o) }
end

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

Creates a new order

Parameters:

  • side (Symbol)

    :buy or :sell

  • instrument (Symbol)

    :xbtusd, :xbteur or :xbtsgd

  • amount (BigDecimal)

    Quantity to buy or sell

  • price (BigDecimal)

    Maximum price to pay when buying or minimum price to charge when selling.

  • options (Hash) (defaults to: {})

    Optional arguments wallet_id: [String] The wallet to use for placing this order

    defaults to Itbit.default_wallet_id
    

    wait: [Boolean] Block the process and wait until this

    order moves out of the :submitted state, defaults to false.
    

    type: Order type, only :limit is supported at the moment currency: Always :xbt, but left for compatibility. metadata: Arbitrary JSON data for your use. client_order_identifier: If you have your own ID for this

    order you can pass it in this field, *Make sure it's unique!*
    


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/itbit/order.rb', line 76

def self.create!(side, instrument, amount, price, options = {})
  wallet_id = options[:wallet_id] || Itbit.default_wallet_id
  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 = Order.new Api.request(:post, "/wallets/#{wallet_id}/orders", params)
  retries = 0
  while options[:wait] && order.status == :submitted
    sleep 0.2
    begin 
      order = find(order.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

.find(id, wallet_id = Itbit.default_wallet_id) ⇒ Object

Finds an order by id in a given wallet.

Parameters:

  • id (String)

    The order’s uuid

  • wallet_id (String) (defaults to: Itbit.default_wallet_id)

    Optional wallet’s uuid, defaults to Itbit.default_wallet_id



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

def self.find(id, wallet_id = Itbit.default_wallet_id)
  Order.new Api.request(:get, "/wallets/#{wallet_id}/orders/#{id}")
end

Instance Method Details

#cancel!Object

Cancel this order



108
109
110
111
112
113
114
# File 'lib/itbit/order.rb', line 108

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