-
Notifications
You must be signed in to change notification settings - Fork 1
/
exchange_interface.rb
83 lines (72 loc) · 1.93 KB
/
exchange_interface.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'mtgox_client'
class ExchangeInterface
def initialize(dry_run=false)
@dry_run = dry_run
MtGox.configure do |c|
c.key = BotSettings::KEY
c.secret = BotSettings::SECRET
end
@client = MtGox
@decimals = BotSettings::DECIMAL_PLACES
puts "initialized ExchangeInterface" if BotSettings::DEBUG
self
end
def new_deposit_address
retry_forever {@client.address};
end
def cancel_all_orders
return if @dry_run
orders = order_array
orders.each do |order|
# skip inactive orders to avoid 404
next if order.id.to_s[0] == 'X'
type = :buy if order.class == MtGox::Buy
type ||= :sell
log("Cancelling: #{type} #{order.amount}@#{order.price}")
retry_forever {@client.cancel(order.id)}
end
end
def get_ticker
t = retry_forever {@client.ticker}
{:last => t.price, :buy => t.buy, :sell => t.sell}
end
def get_trade_data
if @dry_run
btc = BotSettings::DRY_BTC
usd = BotSettings::DRY_USD
orders = []
else
orders = order_array
balances = retry_forever {@client.balance}
balances.each do |b|
btc = b.amount if b.currency == "BTC"
usd = b.amount if b.currency == "USD"
end
end
{:btc => btc, :usd => usd, :orders => orders}
end
def place_order(price, amount, type)
if @dry_run
log("#{type.to_s.capitalize}: #{amount}@#{price}")
return
else
if type == :buy
order_id = @client.buy_id(amount, price)
elsif type == :sell
order_id = @client.sell_id(amount, price)
else
log("invalid order type")
exit
end
log("#{type.to_s.capitalize}: #{amount}@#{price} id: #{order_id}")
return order_id
end
end
private
def order_array
puts "in order_array" if BotSettings::DEBUG
o = retry_forever {@client.orders}
puts "orders = #{o.inspect}" if BotSettings::DEBUG
o[:buys] + o[:sells]
end
end