Class: Bitmex::Client

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

Overview

Main client interface for Bitmex API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(testnet: false, api_key: nil, api_secret: nil) ⇒ Client

Create new client instance

Parameters:

  • testnet (Boolean) (defaults to: false)

    true for testnet network

  • api_key (String) (defaults to: nil)

    the api key

  • api_secret (String) (defaults to: nil)

    the api secret



10
11
12
13
14
# File 'lib/bitmex/client.rb', line 10

def initialize(testnet: false, api_key: nil, api_secret: nil)
  @host = testnet ? TESTNET_HOST : MAINNET_HOST
  @api_key = api_key
  @api_secret = api_secret
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



4
5
6
# File 'lib/bitmex/client.rb', line 4

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



4
5
6
# File 'lib/bitmex/client.rb', line 4

def api_secret
  @api_secret
end

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/bitmex/client.rb', line 4

def host
  @host
end

Instance Method Details

#announcements(&ablock) ⇒ Array

Get site announcements

Returns:

  • (Array)

    the public announcements



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

def announcements(&ablock)
  if block_given?
    websocket.listen announcement: nil, &ablock
  else
    rest.get :announcement
  end
end

#apikey(api_key = nil) ⇒ Bitmex::Apikey

Persistent API Keys for Developers

Returns:



28
29
30
# File 'lib/bitmex/client.rb', line 28

def apikey(api_key = nil)
  Bitmex::Apikey.new rest, api_key
end

#chatBitmex::Chat

Trollbox Data

Returns:



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

def chat
  Bitmex::Chat.new rest, websocket
end

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

Get funding history

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 funding data

Returns:

  • (Array)

    the history



48
49
50
51
52
53
54
# File 'lib/bitmex/client.rb', line 48

def funding(filters = {}, &ablock)
  if block_given?
    websocket.listen funding: nil, &ablock
  else
    rest.get :funding, params: filters
  end
end

#instrumentBitmex::Instrument

Tradeable Contracts, Indices, and History

Returns:



40
41
42
# File 'lib/bitmex/client.rb', line 40

def instrument
  Bitmex::Instrument.new rest, websocket
end

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

Get insurance fund history

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 insurance data

Returns:

  • (Array)

    the history



60
61
62
63
64
65
66
# File 'lib/bitmex/client.rb', line 60

def insurance(filters = {}, &ablock)
  if block_given?
    websocket.listen insurance: nil, &ablock
  else
    rest.get :insurance, params: filters
  end
end

#leaderboard(ranking = 'notional') ⇒ Array

Get current leaderboard

Parameters:

  • ranking (notional ROE) (defaults to: 'notional')

    the ranking type

Returns:

  • (Array)

    current leaders



71
72
73
# File 'lib/bitmex/client.rb', line 71

def leaderboard(ranking = 'notional')
  rest.get :leaderboard, params: { method: ranking }
end

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

Get liquidation orders

Examples:

Get liquidations orders

liquidations = client.liquidations symbol: 'XBTUSD'

Listen for liquidation orders

client.liquidations symbol: 'XBTUSD' do |liquidation|
  puts liquidation.inspect
end

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 liquidation data

Returns:

  • (Array)

    the liquidations



85
86
87
88
89
90
91
# File 'lib/bitmex/client.rb', line 85

def liquidations(filters = {}, &ablock)
  if block_given?
    websocket.listen liquidation: filters[:symbol], &ablock
  else
    rest.get :liquidation, params: filters
  end
end

#order(orderID: nil, clOrdID: nil) ⇒ Bitmex::Order

Get an order by id

Parameters:

  • orderID (String) (defaults to: nil)

    the order #

  • clOrdID (String) (defaults to: nil)

    the client order #

Returns:

Raises:

  • (ArgumentError)


104
105
106
107
108
# File 'lib/bitmex/client.rb', line 104

def order(orderID: nil, clOrdID: nil)
  raise ArgumentError, 'either orderID or clOrdID is required' if orderID.nil? && clOrdID.nil?

  Bitmex::Order.new rest, websocket, orderID, clOrdID
end

#orderbook(symbol, depth: 25) {|Hash| ... } ⇒ Array

Get current Level 2 orderbook in vertical format

Examples:

Get the first level

orderbook = client.orderbook 'XBTUSD', depth: 1

Listen to orderbook changes

client.orderbook 'XBTUSD' do |orderbook|
  puts orderbook.inspect
end

Parameters:

  • symbol (String)

    instrument symbol, send a series (e.g. XBT) to get data for the nearest contract in that series

  • depth (Integer) (defaults to: 25)

    orderbook depth per side. send 0 for full depth.

Yields:

  • (Hash)

    the orderbook data

Returns:

  • (Array)

    the orderbook

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
# File 'lib/bitmex/client.rb', line 121

def orderbook(symbol, depth: 25, &ablock)
  raise ArgumentError, 'symbol is required' unless symbol

  if block_given?
    websocket.listen orderBookL2: symbol, &ablock
  else
    rest.get 'orderbook/L2', params: { symbol: symbol, depth: depth }
  end
end

#ordersBitmex::Order

Order Placement, Cancellation, Amending, and History

Returns:



95
96
97
98
# File 'lib/bitmex/client.rb', line 95

def orders
  # TODO: use class method
  Bitmex::Order.new rest, websocket
end

#position(symbol) ⇒ Bitmex::Position

Get an open position

Parameters:

  • symbol (String)

    symbol of position

Returns:



141
142
143
# File 'lib/bitmex/client.rb', line 141

def position(symbol)
  Bitmex::Position.new rest, websocket, symbol
end

#positionsArray

Summary of Open and Closed Positions

Returns:

  • (Array)

    the list of positions



133
134
135
136
# File 'lib/bitmex/client.rb', line 133

def positions
  # TODO: use class method
  Bitmex::Position.new rest, websocket
end

#quotesBitmex::Quote

Best Bid/Offer Snapshots & Historical Bins

Returns:



147
148
149
150
# File 'lib/bitmex/client.rb', line 147

def quotes
  # TODO: use class method
  Bitmex::Quote.new rest, websocket
end

#restObject



191
192
193
# File 'lib/bitmex/client.rb', line 191

def rest
  @rest ||= Rest.new host, api_key: api_key, api_secret: api_secret
end

#schemaHash

Get model schemata for data objects returned by this AP

Returns:

  • (Hash)

    the schema



154
155
156
# File 'lib/bitmex/client.rb', line 154

def schema
  rest.get :schema
end

#settlements {|Hash| ... } ⇒ Array

Get settlement history

Yields:

  • (Hash)

    the settlement data

Returns:

  • (Array)

    the settlement history



161
162
163
164
165
166
167
# File 'lib/bitmex/client.rb', line 161

def settlements(&ablock)
  if block_given?
    websocket.listen settlement: nil, &ablock
  else
    rest.get :settlement
  end
end

#statsBitmex::Stats

Exchange statistics

Returns:



171
172
173
# File 'lib/bitmex/client.rb', line 171

def stats
  Bitmex::Stats.new rest
end

#tradesBitmex::Trade

Individual and bucketed trades

Returns:



177
178
179
# File 'lib/bitmex/client.rb', line 177

def trades
  Bitmex::Trade.new rest, websocket
end

#userBitmex::User

Account operations

Returns:



183
184
185
# File 'lib/bitmex/client.rb', line 183

def user
  Bitmex::User.new rest, websocket
end

#websocketObject



187
188
189
# File 'lib/bitmex/client.rb', line 187

def websocket
  @websocket ||= Websocket.new host, api_key: api_key, api_secret: api_secret
end