Class: Coinbase::Orders

Inherits:
Client
  • Object
show all
Defined in:
lib/coinbase/orders.rb

Overview

Create, Delete, Lists orders

Constant Summary collapse

ORDER_URL =
'/orders'

Instance Method Summary collapse

Methods inherited from Client

configuration, configure, #delete, #get, #headers, #http_request, #initialize, #post, #put, #signature

Methods included from Util

#base_uri, #build_query_params, #format_response, #pagination_params, #send_request

Constructor Details

This class inherits a constructor from Coinbase::Client

Instance Method Details

#cancel(options = {}) ⇒ Hash

Cancel a previously placed order. Order must belong to the profile that the API key belongs to.

product_id [String] The product ID of the order (optional). id [String] The ID of the order (optional). product_id [String] The product ID of the order (optional). client_oid [String] Order ID selected by you to identify your order (optional) One of client_oid or id is required. of the canceled orders.

Parameters:

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

    an hash with the following parameters

Returns:

  • (Hash)

    a hash with status code and list of ids



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/coinbase/orders.rb', line 63

def cancel(options = {})
  if options.key?(:id)
    operational_url = "#{ORDER_URL}/#{options[:id]}"
  elsif options.key?(:client_oid)
    operational_url = "#{ORDER_URL}/client:#{options[:client_oid]}"
  else
    return { code: '500', result: 'One of client_oid or id is required.' }
  end
  operational_url += "?product_id=#{product_id}" if options[:product_id]
  response = delete(operational_url)
  format_response(response)
end

#cancel_all(product_id = nil) ⇒ Hash

Cancel all open orders from the profile that the API key belongs to.

only cancel orders open for a specific product (optional). orders.

Parameters:

  • product_id (String) (defaults to: nil)

    The product ID of the order,

Returns:

  • (Hash)

    a hash with status code and list of ids of the canceled



44
45
46
47
48
49
# File 'lib/coinbase/orders.rb', line 44

def cancel_all(product_id = nil)
  operational_url = ORDER_URL
  operational_url += "?product_id=#{product_id}" if product_id
  response = delete(operational_url)
  format_response(response)
end

#create(order_details = {}) ⇒ Hash

Creates the order with the given data.

side [String] buy or sell. product_id [String] A valid product id (e.g. “BTC-USD”). client_oid [String] Order ID selected by you to identify your order (optional). type [String] limit or market, default is limit (optional). stp [String] Self-trade prevention flag (optional). stop [String] Either loss or entry. Requires stop_price to be defined (optional). stop_price [String] Only if stop is defined. Sets trigger price for stop order (optional). Limit Order Params size [String] Amount of base currency to buy or sell. price [String] Price per bitcoin. time_in_force [String] GTC, GTT, IOC, or FOK, default is GTC (optional). cancel_after [String] min, hour, day but requires time_in_force to be GTT (optional). post_only [Boolean] Post only flag but invalid when time_in_force is IOC or FOK (optional). Market Order Params (One of size or funds is required.) size [String] Amount of base currency to buy or sell (optional). funds [String] Desired amount of quote currency to use (optional).

Parameters:

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

    with following fields

Returns:

  • (Hash)

    a hash with status code and order details.



34
35
36
# File 'lib/coinbase/orders.rb', line 34

def create(order_details = {})
  format_response(post(ORDER_URL, body: order_details))
end

#fetch(options = {}) ⇒ Hash

Fetch a single order by order id from the profile that the API key belongs to.

product_id [String] The product ID of the order (optional). id [String] The ID of the order (optional). client_oid [String] Order ID selected by you to identify your order (optional) One of client_oid or id is required.

Parameters:

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

    an hash with the following parameters

Returns:

  • (Hash)

    a hash with status code and order details.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/coinbase/orders.rb', line 86

def fetch(options = {})
  if options.key?(:id)
    operational_url = "#{ORDER_URL}/#{options[:id]}"
  elsif options.key?(:client_oid)
    operational_url = "#{ORDER_URL}/client:#{options[:client_oid]}"
  else
    return { code: '500', result: 'One of client_oid or id is required.' }
  end
  response = get(operational_url)
  format_response(response)
end

#list(params = {}) ⇒ Hash

Fetch the list of current open orders. Only open or un-settled orders are returned.

before [String] Request page before (newer) this pagination id (optional). after [String] Request page after (older) this pagination id (optional). limit [Integer] Number of results per request. Maximum 1000, default is 1000 (optional).

Parameters:

  • product_id (String)

    The product ID of the order (optional).

  • statuses (Array)

    Limit list of orders to these statuses. Passing all returns orders of all statuses.

  • paginate_options (Hash)

    an hash with the following parameters

Returns:

  • (Hash)

    a hash with status code and order details.



110
111
112
113
114
115
116
# File 'lib/coinbase/orders.rb', line 110

def list(params = {})
  operational_url = ORDER_URL
  query_params = build_query_params(params)
  operational_url += '/?' + query_params unless query_params.empty?
  response = get(operational_url)
  format_response(response)
end