Class: Bitmex::Position

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

Overview

Summary of Open and Closed Positions

Author:

  • Iulian Costan

Instance Attribute Summary collapse

Attributes inherited from Base

#rest, #websocket

Instance Method Summary collapse

Constructor Details

#initialize(rest, websocket, symbol = 'XBTUSD') ⇒ Position

A new instance of Position

Parameters:

  • rest (Bitmex::Rest)

    the HTTP rest

  • symbol (String) (defaults to: 'XBTUSD')

    the symbol of the underlying position



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

def initialize(rest, websocket, symbol = 'XBTUSD')
  super rest, websocket
  @symbol = symbol
end

Instance Attribute Details

#symbolObject (readonly)

Returns the value of attribute symbol.



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

def symbol
  @symbol
end

Instance Method Details

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

Get your positions

Examples:

Get all positions

positions = client.positions.all

Listen for positions changes

client.positions.all do |position|
  puts position.inspect
end

Yields:

  • (Hash)

    the position

Returns:

  • (Array)

    the list of positions



24
25
26
27
28
29
30
# File 'lib/bitmex/position.rb', line 24

def all(&ablock)
  if block_given?
    websocket.listen position: nil, &ablock
  else
    rest.get position_path, auth: true
  end
end

#isolate(enabled: true) ⇒ Hash

Enable isolated margin or cross margin per-position

Parameters:

  • enabled (true, false) (defaults to: true)

    true for isolated margin, false cross margin

Returns:

  • (Hash)

    the updated position



35
36
37
38
39
40
41
# File 'lib/bitmex/position.rb', line 35

def isolate(enabled: true)
  path = position_path(:isolate)
  params = { symbol: symbol, enabled: enabled }
  rest.post path, params: params do |response|
    response_handler response
  end
end

#leverage(leverage) ⇒ Hash

Choose leverage for a position

Parameters:

  • leverage (0-100)

    leverage value. send a number between 0.01 and 100 to enable isolated margin with a fixed leverage. send 0 to enable cross margin

Returns:

  • (Hash)

    the updated position

Raises:

  • (ArgumentError)


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

def leverage(leverage)
  raise ArgumentError, "leverage #{leverage} is outside of [0..100] range" unless (0..100).include? leverage

  path = position_path(:leverage)
  params = { symbol: symbol, leverage: leverage }
  rest.post path, params: params do |response|
    response_handler response
  end
end

#risk_limit(risk_limit) ⇒ Hash

Update your risk limit

Parameters:

  • risk_limit (Double)

    new risk limit, in Satoshis.

Returns:

  • (Hash)

    the updated position



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

def risk_limit(risk_limit)
  path = position_path(:riskLimit)
  params = { symbol: symbol, riskLimit: risk_limit }
  rest.post path, params: params do |response|
    response_handler response
  end
end

#transfer_margin(amount) ⇒ Hash

Transfer equity in or out of a position

Examples:

Transfer 1000 Satoshi

position = client.position('XBTUSD').transfer_margin 1000

Parameters:

  • amount (Double)

    amount to transfer, in Satoshis. may be negative.

Returns:

  • (Hash)

    the updated position



72
73
74
75
76
77
78
# File 'lib/bitmex/position.rb', line 72

def transfer_margin(amount)
  path = position_path(:transferMargin)
  params = { symbol: symbol, amount: amount }
  rest.post path, params: params do |response|
    response_handler response
  end
end