Class: BitexBot::ClosingFlow
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- BitexBot::ClosingFlow
- Defined in:
- lib/bitex_bot/models/closing_flow.rb
Overview
Close buy/sell positions.
Direct Known Subclasses
Class Method Summary collapse
-
.close_open_positions ⇒ Object
Start a new CloseBuy that closes exising OpenBuy’s by selling on another exchange what was just bought on bitex.
- .create_closing_flow!(price, quantity, amount, open_positions) ⇒ Object
-
.suggested_amount(positions) ⇒ Object
close_open_positions helpers.
Instance Method Summary collapse
-
#create_initial_order_and_close_position! ⇒ Object
end: close_open_positions helpers.
- #estimate_fiat_profit ⇒ Object
- #positions_balance_amount ⇒ Object
-
#sync_closed_positions(orders, transactions) ⇒ Object
TODO: should receive a order_ids and user_transaccions array, then each Wrapper should know how to search for them.
Class Method Details
.close_open_positions ⇒ Object
Start a new CloseBuy that closes exising OpenBuy’s by selling on another exchange what was just bought on bitex.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/bitex_bot/models/closing_flow.rb', line 9 def self.close_open_positions open_positions = open_position_class.open return if open_positions.empty? quantity = open_positions.map(&:quantity).sum amount = open_positions.map(&:amount).sum price = suggested_amount(open_positions) / quantity # Don't even bother trying to close a position that's too small. return unless Robot.taker.enough_order_size?(quantity, price) create_closing_flow!(price, quantity, amount, open_positions) end |
.create_closing_flow!(price, quantity, amount, open_positions) ⇒ Object
27 28 29 30 31 |
# File 'lib/bitex_bot/models/closing_flow.rb', line 27 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 |
.suggested_amount(positions) ⇒ Object
close_open_positions helpers
23 24 25 |
# File 'lib/bitex_bot/models/closing_flow.rb', line 23 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
34 35 36 |
# File 'lib/bitex_bot/models/closing_flow.rb', line 34 def create_initial_order_and_close_position! create_order_and_close_position(quantity, desired_price) end |
#estimate_fiat_profit ⇒ Object
44 45 46 |
# File 'lib/bitex_bot/models/closing_flow.rb', line 44 def estimate_fiat_profit raise 'self subclass responsibility' end |
#positions_balance_amount ⇒ Object
48 49 50 |
# File 'lib/bitex_bot/models/closing_flow.rb', line 48 def positions_balance_amount close_positions.sum(:amount) * fx_rate end |
#sync_closed_positions(orders, transactions) ⇒ Object
TODO: should receive a order_ids and user_transaccions array, then each Wrapper should know how to search for them.
39 40 41 42 |
# File 'lib/bitex_bot/models/closing_flow.rb', line 39 def sync_closed_positions(orders, transactions) # 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!(orders, transactions) end |