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.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bitex_bot/models/closing_flow.rb', line 11

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

  return unless Robot.taker.enough_order_size?(quantity, price)

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

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



33
34
35
36
37
# File 'lib/bitex_bot/models/closing_flow.rb', line 33

def self.create_closing_flow!(price, quantity, amount, open_positions)
  create!(desired_price: price, quantity: quantity, amount: amount, open_positions: open_positions)
    .create_initial_order_and_close_position!
  nil
end

.open_positionsObject



24
25
26
# File 'lib/bitex_bot/models/closing_flow.rb', line 24

def self.open_positions
  open_position_class.open
end

.suggested_amount(positions) ⇒ Object

close_open_positions helpers



29
30
31
# File 'lib/bitex_bot/models/closing_flow.rb', line 29

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



40
41
42
# File 'lib/bitex_bot/models/closing_flow.rb', line 40

def create_initial_order_and_close_position!
  create_order_and_close_position(quantity, desired_price)
end

#estimate_fiat_profitObject



50
51
52
# File 'lib/bitex_bot/models/closing_flow.rb', line 50

def estimate_fiat_profit
  raise 'self subclass responsibility'
end

#positions_balance_amountObject



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

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.



45
46
47
48
# File 'lib/bitex_bot/models/closing_flow.rb', line 45

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