class Itbit::Order
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.
Attributes
amount[RW]
amount_filled[RW]
client_order_identifier[RW]
created_time[RW]
currency[RW]
display_amount[RW]
id[RW]
instrument[RW]
metadata[RW]
price[RW]
side[RW]
status[RW]
type[RW]
volume_weighted_average_price[RW]
wallet_id[RW]
Public Class Methods
all(opts = {})
click to toggle source
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
@return [Array<Itbit::Order>]
# 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 = {})
click to toggle source
Creates a new order @param side [Symbol] :buy or :sell @param instrument [Symbol] :xbtusd, :xbteur or :xbtsgd @param amount [BigDecimal] Quantity to buy or sell @param price [BigDecimal] Maximum price to pay when buying
or minimum price to charge when selling.
@param options [Hash] 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!*
# 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)
click to toggle source
Finds an order by id in a given wallet. @param id [String] The order's uuid @param wallet_id
[String] Optional wallet's uuid, defaults to
Itbit.default_wallet_id
# 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
new(attrs)
click to toggle source
# File lib/itbit/order.rb, line 12 def initialize(attrs) attrs.each{|k, v| send("#{k.underscore}=", v)} end
Public Instance Methods
cancel!()
click to toggle source
Cancel this order
# 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
created_time=(value)
click to toggle source
# File lib/itbit/order.rb, line 29 def created_time=(value) @created_time = value.is_a?(String) ? Time.parse(value).to_i : value end