Class: Bitmex::Order

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

Overview

Order Placement, Cancellation, Amending, and History

Author:

  • Iulian Costan

Instance Attribute Summary collapse

Attributes inherited from Base

#rest, #websocket

Instance Method Summary collapse

Constructor Details

#initialize(rest, websocket = nil, orderID = nil, clOrdID = nil) ⇒ Order

Returns a new instance of Order.



7
8
9
10
11
# File 'lib/bitmex/order.rb', line 7

def initialize(rest, websocket = nil, orderID = nil, clOrdID = nil)
  super rest, websocket
  @orderID = orderID
  @clOrdID = clOrdID
end

Instance Attribute Details

#clOrdIDObject (readonly)

Returns the value of attribute clOrdID.



5
6
7
# File 'lib/bitmex/order.rb', line 5

def clOrdID
  @clOrdID
end

#orderIDObject (readonly)

Returns the value of attribute orderID.



5
6
7
# File 'lib/bitmex/order.rb', line 5

def orderID
  @orderID
end

Instance Method Details

#all(filters = {}) {|Hash| ... } ⇒ Array

Get your orders

Parameters:

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

    the filters to apply to mostly REST API requests with a few exceptions

Options Hash (filters):

  • :symbol (String)

    the instrument symbol, this filter works in both REST and Websocket APIs

  • :filter (String)

    generic table filter, send key/value pairs Timestamp Filters

  • :columns (String)

    array of column names to fetch; if omitted, will return all columns.

  • :count (Double) — default: 100

    number of results to fetch.

  • :start (Double)

    Starting point for results.

  • :reverse (Boolean) — default: false

    if true, will sort results newest first.

  • :startTime (Datetime, String)

    Starting date filter for results.

  • :endTime (Datetime, String)

    Ending date filter for results

Yields:

  • (Hash)

    the order

Returns:

  • (Array)

    the orders



17
18
19
20
21
22
23
# File 'lib/bitmex/order.rb', line 17

def all(filters = {}, &ablock)
  if block_given?
    websocket.listen order: filters[:symbol], &ablock
  else
    rest.get order_path, params: filters, auth: true
  end
end

#cancel(text = nil) ⇒ Bitmex::Mash

Cancel an order

Parameters:

  • text (String) (defaults to: nil)

    Optional cancellation annotation. e.g. ‘Spread Exceeded’.

Returns:



63
64
65
66
67
68
69
# File 'lib/bitmex/order.rb', line 63

def cancel(text = nil)
  params = { orderID: orderID, clOrdID: clOrdID, text: text }
  rest.delete order_path, params: params do |response|
    # a single order only
    response_handler(response).first
  end
end

#create(symbol, attributes) ⇒ Bitmex::Mash

Place new order

Parameters:

  • symbol (String)

    instrument symbol

  • attributes (Hash)

    order attributes

Options Hash (attributes):

  • :side (Buy, Sell)

    Order side. Defaults to ‘Buy’ unless orderQty is negative

  • :orderQty (Integer)

    Order quantity in units of the instrument (i.e. contracts)

  • :price (Double)

    Optional limit price for ‘Limit’, ‘StopLimit’, and ‘LimitIfTouched’ orders

  • :displayQty (Double)

    Optional quantity to display in the book. Use 0 for a fully hidden order.

  • :stopPx (Double)

    Optional trigger price for ‘Stop’, ‘StopLimit’, ‘MarketIfTouched’, and ‘LimitIfTouched’ orders. Use a price below the current price for stop-sell orders and buy-if-touched orders. Use execInst of ‘MarkPrice’ or ‘LastPrice’ to define the current price used for triggering.

  • :clOrdID (String)

    Optional Client Order ID. This clOrdID will come back on the order and any related executions.

  • :pegOffsetValue (Double)

    Optional trailing offset from the current price for ‘Stop’, ‘StopLimit’, ‘MarketIfTouched’, and ‘LimitIfTouched’ orders; use a negative offset for stop-sell orders and buy-if-touched orders. Optional offset from the peg price for ‘Pegged’ orders.

  • :pegPriceType (LastPeg, MidPricePeg, MarketPeg, PrimaryPeg, TrailingStopPeg)

    Optional peg price type.

  • :ordType (Market, Limit, Stop, StopLimit, MarketIfTouched, LimitIfTouched, MarketWithLeftOverAsLimit, Pegged)

    Order type. Defaults to ‘Limit’ when price is specified. Defaults to ‘Stop’ when stopPx is specified. Defaults to ‘StopLimit’ when price and stopPx are specified.

  • :timeInForce (Day, GoodTillCancel, ImmediateOrCancel, FillOrKill)

    Time in force. Defaults to ‘GoodTillCancel’ for ‘Limit’, ‘StopLimit’, ‘LimitIfTouched’, and ‘MarketWithLeftOverAsLimit’ orders.

  • :execInst (ParticipateDoNotInitiate, AllOrNone, MarkPrice, IndexPrice, LastPrice, Close, ReduceOnly, Fixed)

    Optional execution instructions. AllOrNone’ instruction requires displayQty to be 0. ‘MarkPrice’, ‘IndexPrice’ or ‘LastPrice’ instruction valid for ‘Stop’, ‘StopLimit’, ‘MarketIfTouched’, and ‘LimitIfTouched’ orders.

  • :text (String)

    Optional amend annotation. e.g. ‘Take profit’

Returns:



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

def create(symbol, attributes)
  params = attributes.merge symbol: symbol
  rest.post order_path, params: params
end

#update(attributes) ⇒ Bitmex::Mash

Amend the quantity or price of an open order

Parameters:

  • attributes (Hash)

    the fields to update

Options Hash (attributes):

  • :orderQty (Integer)

    Optional order quantity in units of the instrument (i.e. contracts)

  • :leavesQty (Integer)

    Optional leaves quantity in units of the instrument (i.e. contracts). Useful for amending partially filled orders.

  • :price (Double)

    Optional limit price for ‘Limit’, ‘StopLimit’, and ‘LimitIfTouched’ orders.

  • :stopPx (Double)

    Optional trigger price for ‘Stop’, ‘StopLimit’, ‘MarketIfTouched’, and ‘LimitIfTouched’ orders. Use a price below the current price for stop-sell orders and buy-if-touched orders.

  • :pegOffsetValue (Double)

    Optional trailing offset from the current price for ‘Stop’, ‘StopLimit’, ‘MarketIfTouched’, and ‘LimitIfTouched’ orders; use a negative offset for stop-sell orders and buy-if-touched orders. Optional offset from the peg price for ‘Pegged’ orders.

  • :text (String)

    Optional amend annotation. e.g. ‘Adjust skew’

Returns:



34
35
36
37
# File 'lib/bitmex/order.rb', line 34

def update(attributes)
  params = attributes.merge orderID: orderID, origClOrdID: clOrdID
  rest.put order_path, params: params
end