class Bitex::BaseOrder

Base class for Bids and Asks

Attributes

created_at[RW]

@!attribute created_at

@return [Time] Time when this Bid was created.
id[RW]

@!attribute id

@return [Integer] This Bid's unique ID.
issuer[RW]

@!attribute issuer

@return [String] The issuer of this order, helps you tell apart orders created from the web UI and the API.
order_book[RW]

@!attribute order_book

@return [Symbol] :btc_usd or :btc_ars
price[RW]

@!attribute price

@return [BigDecimal] Maximum price to pay per unit.
reason[RW]

@!attribute reason

The cancellation reason for your Ask/Bid subclass, if any.
* :not_cancelled Has not been cancelled.
* :not_enough_funds Not enough funds to place this order.
* :user_cancelled Cancelled per user's request
* :system_cancelled Bitex cancelled this order, for a good reason.
status[RW]

@!attribute status

The status in its lifecycle, was defined into Ask/Bid subclass.

Public Class Methods

active() click to toggle source

Returns an array with all your active orders of this type. Uses {Order.active} under the hood.

# File lib/bitex/base_order.rb, line 44
def self.active
  Order.active.select { |o| o.is_a?(self) }
end
all() click to toggle source

Returns an array with all your active orders of this type and another order of this type that was active the last 2 hours. Uses {Order.all} under the hood.

# File lib/bitex/base_order.rb, line 38
def self.all
  Order.all.select { |o| o.is_a?(self) }
end
create!(order_book, amount, price, wait = false) click to toggle source

@visibility private

# File lib/bitex/base_order.rb, line 55
def self.create!(order_book, amount, price, wait = false)
  order_book_id = ORDER_BOOKS[order_book]
  raise UnknownOrderBook, "Could not find order book #{order_book}" unless order_book_id

  params = { amount: amount, price: price, orderbook: order_book_id }

  order = from_json(Api.private(:post, "/private#{base_path}", params))
  retries = 0
  while wait && order.status == :received
    sleep(0.2)
    order = find_order(order)
    retries += 1
    # Wait 15 minutes for the order to be accepted.
    raise StandardError, "Timed out waiting for #{base_path} ##{order.id}" if retries > 5000
  end
  order
end
find(id) click to toggle source

Find an order in your list of active orders. Uses {Order.active} under the hood.

# File lib/bitex/base_order.rb, line 50
def self.find(id)
  from_json(Api.private(:get, "/private#{base_path}/#{id}"))
end
from_json(json, order = nil) click to toggle source

@visibility private

# File lib/bitex/base_order.rb, line 79
def self.from_json(json, order = nil)
  Api.from_json(order || new, json) do |thing|
    thing.order_book = order_books[json[3]]
    thing.price = json[6].to_s.to_d
    thing.status = statuses[json[7]]
    thing.reason = reasons[json[8]]
    thing.issuer = json[10]
  end
end

Private Class Methods

find_order(order) click to toggle source
# File lib/bitex/base_order.rb, line 89
def self.find_order(order)
  find(order.id)
rescue StandardError
  order
end
order_books() click to toggle source
# File lib/bitex/base_order.rb, line 95
def self.order_books
  ORDER_BOOKS.invert
end
reasons() click to toggle source
# File lib/bitex/base_order.rb, line 99
def self.reasons
  { 0 => :not_cancelled, 1 => :not_enough_funds, 2 => :user_cancelled, 3 => :system_cancelled }
end
statuses() click to toggle source
# File lib/bitex/base_order.rb, line 103
def self.statuses
  { 1 => :received, 2 => :executing, 3 => :cancelling, 4 => :cancelled, 5 => :completed }
end

Public Instance Methods

cancel!() click to toggle source
# File lib/bitex/base_order.rb, line 73
def cancel!
  path = "/private#{self.class.base_path}/#{id}/cancel"
  self.class.from_json(Api.private(:post, path), self)
end