Class: BitexBot::ClosingFlow
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- BitexBot::ClosingFlow
show all
- Defined in:
- lib/bitex_bot/models/closing_flow.rb
Class Method Summary
collapse
Instance Method Summary
collapse
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.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/bitex_bot/models/closing_flow.rb', line 7
def self.close_open_positions
open_positions = open_position_class.open
return if open_positions.empty?
quantity = open_positions.collect(&:quantity).sum
amount = open_positions.collect(&:amount).sum
suggested_amount = open_positions.collect do |open|
open.quantity * open.opening_flow.suggested_closing_price
end.sum
price = suggested_amount / quantity
return if quantity * price < minimum_amount_for_closing
flow = create!(
desired_price: price,
quantity: quantity,
amount: amount,
open_positions: open_positions)
flow.create_order_and_close_position(quantity, price)
return flow
end
|
.close_time_to_live ⇒ Object
76
77
78
|
# File 'lib/bitex_bot/models/closing_flow.rb', line 76
def self.close_time_to_live
60
end
|
.minimum_amount_for_closing ⇒ Object
When placing a closing order we need to be aware of the smallest order amount permitted by the other exchange. If the other order is less than this USD amount then we do not attempt to close the positions yet.
72
73
74
|
# File 'lib/bitex_bot/models/closing_flow.rb', line 72
def self.minimum_amount_for_closing
5
end
|
Instance Method Details
#create_order_and_close_position(quantity, price) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/bitex_bot/models/closing_flow.rb', line 32
def create_order_and_close_position(quantity, price)
order = Bitstamp.orders.send(order_method,
amount: quantity.round(8), price: price.round(8))
Robot.logger.info("Closing: Going to #{order_method} ##{order.id} for"\
"#{self.class.name} ##{id} #{quantity} BTC @ $#{price}")
close_positions.create!(order_id: order.id.to_i)
end
|
#sync_closed_positions(orders, transactions) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/bitex_bot/models/closing_flow.rb', line 40
def sync_closed_positions(orders, transactions)
latest_close = close_positions.last
order = orders.find{|x| x.id.to_s == latest_close.order_id.to_s }
if order.nil?
closes = transactions.select{|t| t.order_id.to_s == latest_close.order_id.to_s}
latest_close.amount = closes.collect{|x| x.usd.to_d }.sum.abs
latest_close.quantity = closes.collect{|x| x.btc.to_d }.sum.abs
latest_close.save!
next_price, next_quantity = get_next_price_and_quantity
if (next_quantity * next_price) > self.class.minimum_amount_for_closing
create_order_and_close_position(next_quantity, next_price)
else
self.btc_profit = get_btc_profit
self.usd_profit = get_usd_profit
self.done = true
Robot.logger.info("Closing: Finished #{self.class.name} ##{id}"\
"earned $#{self.usd_profit} and #{self.btc_profit} BTC. ")
save!
end
elsif latest_close.created_at < self.class.close_time_to_live.seconds.ago
Robot.with_cooldown{ order.cancel! }
end
end
|