Class: BitexBot::ClosingFlow

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bitex_bot/models/closing_flow.rb

Overview

Close buy/sell positions.

Direct Known Subclasses

BuyClosingFlow, SellClosingFlow

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.close_open_positionsObject

Start a new CloseBuy that closes existing OpenBuy’s by selling on another exchange what was just bought on bitex. rubocop:disable Metrics/AbcSize



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bitex_bot/models/closing_flow.rb', line 12

def self.close_open_positions
  return unless open_positions.any?

  positions = open_positions
  quantity = positions.sum(&:quantity)
  amount = positions.sum(&:amount) / fx_rate
  price = suggested_amount(positions) / quantity
  unless Robot.taker.enough_order_size?(quantity, price)
    Robot.log(
      :info,
      "Closing: #{Robot.taker.name} - enough order size for #{Robot.taker.base.upcase} #{quantity}"\
      " @ #{Robot.taker.quote.upcase} #{price}"
    )
    return
  end

  create_closing_flow!(price, quantity, amount, positions)
end

.create_closing_flow!(price, quantity, amount, open_positions) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/bitex_bot/models/closing_flow.rb', line 41

def self.create_closing_flow!(price, quantity, amount, open_positions)
  flow = create!(desired_price: price, quantity: quantity, amount: amount, open_positions: open_positions)
  Robot.log(
    :info,
    "Closing: created #{self}##{flow.id}, desired price: #{flow.desired_price}, quantity: #{flow.quantity}, "\
    "amount: #{flow.amount}."
  )
  flow.create_initial_order_and_close_position!
  nil
end

.open_positionsObject

rubocop:enable Metrics/AbcSize



32
33
34
# File 'lib/bitex_bot/models/closing_flow.rb', line 32

def self.open_positions
  open_position_class.open
end

.suggested_amount(positions) ⇒ Object

close_open_positions helpers



37
38
39
# File 'lib/bitex_bot/models/closing_flow.rb', line 37

def self.suggested_amount(positions)
  positions.map { |p| p.quantity * p.opening_flow.suggested_closing_price }.sum
end

Instance Method Details

#create_initial_order_and_close_position!Object

end: close_open_positions helpers



53
54
55
# File 'lib/bitex_bot/models/closing_flow.rb', line 53

def create_initial_order_and_close_position!
  create_order_and_close_position(quantity, desired_price)
end

#estimate_fiat_profitObject



63
64
65
# File 'lib/bitex_bot/models/closing_flow.rb', line 63

def estimate_fiat_profit
  raise 'self subclass responsibility'
end

#positions_balance_amountObject



67
68
69
# File 'lib/bitex_bot/models/closing_flow.rb', line 67

def positions_balance_amount
  close_positions.sum(:amount) * fx_rate
end

#sync_closed_positionsObject

TODO: should receive a order_ids and user_transaccions array, then each Wrapper should know how to search for them.



58
59
60
61
# File 'lib/bitex_bot/models/closing_flow.rb', line 58

def sync_closed_positions
  # Maybe we couldn't create the bitstamp order when this flow was created, so we try again when syncing.
  latest_close.nil? ? create_initial_order_and_close_position! : create_or_cancel!
end