Class: StopByRangeCommand

Inherits:
Object
  • Object
show all
Includes:
HasHTTPClient
Defined in:
lib/bitflyer/cli/command/stop_by_range_command.rb

Instance Method Summary collapse

Methods included from HasHTTPClient

#http_private_client, #http_public_client

Methods included from Authorization

#api_key, #api_secret

Instance Method Details

#run(options) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bitflyer/cli/command/stop_by_range_command.rb', line 9

def run(options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  position = Position.new(http_private_client.positions)
  return puts "You don't have any position now." if position.empty?

  side = position.average.positive? ? 'SELL' : 'BUY'
  price = position.average.abs + (options.range * (side == 'BUY' ? 1.0 : -1.0))

  response = http_private_client.send_parent_order(
    order_method: 'SIMPLE',
    parameters: [{
      product_code: 'FX_BTC_JPY',
      condition_type: 'STOP',
      side: side,
      trigger_price: price,
      size: position.size.to_f
    }]
  )

  if response['parent_order_acceptance_id'].nil?
    puts "An error has occurred#{response}"
  else
    puts "Set limit order #{side} / #{price} / #{position.size.to_f}"
  end
end