Module: Bitfinex::RESTv1Orders

Included in:
RESTv1
Defined in:
lib/rest/v1/orders.rb

Instance Method Summary collapse

Instance Method Details

#cancel_orders(ids = nil) ⇒ Hash

Cancel an order

Examples:

client.cancel_orders([100,231,400])

Parameters:

  • ids (Array) (defaults to: nil)

    or [integer] or nil if it’s Array it’s supposed to specify a list of IDS if it’s an integer it’s supposed to be a single ID if it’s not specified it deletes all the orders placed

Returns:

  • (Hash)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rest/v1/orders.rb', line 59

def cancel_orders(ids=nil)
  case ids
  when Array
    authenticated_post("order/cancel/multi", params: {order_ids: ids.map(&:to_i)}).body
  when Numeric, String
    authenticated_post("order/cancel", params: {order_id: ids.to_i}).body
  when NilClass
    authenticated_post("order/cancel/all").body
  else
    raise ParamsError
  end
end

#multiple_orders(orders) ⇒ Hash

Submit several new orders at once

@example:

client.multiple_orders([{symbol: "usdbtc", amount: 10, price: 0, exchange: "bitfinex", side: "buy", type: "market"}])

Parameters:

  • orders (Array)

    Array of Hash with the following elements

  • orders

    :symbol [string] The name of the symbol

  • orders

    :amount [decimal] Order size: how much to buy or sell

  • orders

    :price [decimal] Price to buy or sell at. May omit if a market order

  • orders

    :exchange [string] “bitfinex”

  • orders

    :side [string] Either “buy” or “sell”

  • orders

    :type [string] Either “market” / “limit” / “stop” / “trailing-stop” / “fill-or-kill”

Returns:

  • (Hash)

    with a ‘object_id` that is an `Array`



46
47
48
# File 'lib/rest/v1/orders.rb', line 46

def multiple_orders(orders)
  authenticated_post("order/new/multi", params: {orders: orders}).body
end

#new_order(symbol, amount, type, side, price = nil, params = {}) ⇒ Hash

Submit a new order @example:

client.new_order("usdbtc", 100, "market", "sell", 0)

Parameters:

  • symbol (string)

    The name of the symbol (see ‘#symbols`)

  • amount (decimal)

    Order size: how much to buy or sell

  • type (string)

    Either “market” / “limit” / “stop” / “trailing-stop” / “fill-or-kill” / “exchange market” / “exchange limit” / “exchange stop” / “exchange trailing-stop” / “exchange fill-or-kill”. (type starting by “exchange ” are exchange orders, others are margin trading orders)

  • side (string)

    Either “buy” or “sell”

  • price (decimal) (defaults to: nil)

    Price to buy or sell at. Must be positive. Use random number for market orders.

  • params (defaults to: {})

    :is_hidden [bool] (optional) true if the order should be hidden. Default is false

  • params (defaults to: {})

    :is_postonly [bool] (optional) true if the order should be post only. Default is false. Only relevant for limit orders

  • params (defaults to: {})

    :ocoorder [bool] Set an additional STOP OCO order that will be linked with the current order

  • params (defaults to: {})

    :buy_price_oco [decimal] If ocoorder is true, this field represent the price of the OCO stop order to place

  • params (defaults to: {})

    :sell_price_oco [decimal] If ocoorder is true, this field represent the price of the OCO stop order to place

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rest/v1/orders.rb', line 17

def new_order(symbol, amount, type, side, price = nil, params = {})
  check_params(params, %i{is_hidden is_postonly ocoorder buy_price_oco use_all_available sell_price_oco})

  # for 'market' order, we need to pass a random positive price, not nil
  price ||= 0.001 if type == "market" || type == "exchange market"

  params.merge!({
    symbol: symbol,
    amount: amount.to_s,
    type: type,
    side: side,
    exchange: 'bitfinex',
    price: "%.10f" % price.to_f.round(10) # Decimalize float price (necessary for small numbers)
  })
  authenticated_post("order/new", params: params).body
end

#order_status(id) ⇒ Hash

Get the status of an order. Is it active? Was it cancelled? To what extent has it been executed? etc.

@exmaple:

client.order_status(100)

Parameters:

  • id

Returns:

  • (Hash)


106
107
108
# File 'lib/rest/v1/orders.rb', line 106

def order_status(id)
  authenticated_post("order/status", params: {order_id: id.to_i}).body
end

#ordersHash

View your active orders.

@example:

client.orders

Returns:

  • (Hash)


116
117
118
# File 'lib/rest/v1/orders.rb', line 116

def orders
  authenticated_post("orders").body
end

#replace_order(id, symbol, amount, type, side, price, is_hidden = false, use_remaining = false) ⇒ Hash

Replace an orders with a new one

@example:

client.replace_order(100,"usdbtc", 10, "market", "buy", 0)

Parameters:

  • id (int)

    the ID of the order to replace

  • symbol (string)

    the name of the symbol

  • amount (decimal)

    Order size: how much to buy or sell

  • type (string)

    Either “market” / “limit” / “stop” / “trailing-stop” / “fill-or-kill” / “exchange market” / “exchange limit” / “exchange stop” / “exchange trailing-stop” / “exchange fill-or-kill”. (type starting by “exchange ” are exchange orders, others are margin trading orders)

  • side (string)

    Either “buy” or “sell”

  • price (decimal)

    Price to buy or sell at. May omit if a market order

  • is_hidden (bool) (defaults to: false)

    (optional) true if the order should be hidden. Default is false

  • use_remaining (bool) (defaults to: false)

    (optional) will use the amount remaining of the canceled order as the amount of the new order. Default is false

Returns:

  • (Hash)

    the order



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rest/v1/orders.rb', line 85

def replace_order(id, symbol, amount, type, side, price, is_hidden=false, use_remaining=false)
  params = {
    order_id: id.to_i,
    symbol: symbol,
    amount: amount.to_s,
    type: type,
    side: side,
    exchange: 'bitfinex',
    is_hidden: is_hidden,
    use_remaining: use_remaining,
    price: price.to_s
  }
  authenticated_post("order/cancel/replace", params: params).body
end