Skip to content

Commit

Permalink
Implements 'transfer' method in clients
Browse files Browse the repository at this point in the history
  • Loading branch information
hstove committed Dec 18, 2013
1 parent 6c1a73d commit ccff8be
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 31 deletions.
4 changes: 4 additions & 0 deletions lib/rbtc_arbitrage/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def sell
trade :sell
end

def address
ENV["#{exchange.to_s.upcase}_ADDRESS"]
end

private

def set_key config, key, default
Expand Down
4 changes: 4 additions & 0 deletions lib/rbtc_arbitrage/clients/bitstamp_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def trade action
}
Bitstamp.orders.send(action, bitstamp_options)
end

def transfer other_client
Bitstamp.transfer(@options[:volume], other_client.address)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rbtc_arbitrage/clients/mtgox_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def trade action
action = "#{action.to_s}!".to_sym
MtGox.send(action, @options[:volume], :market)
end

def transfer other_client
MtGox.withdraw! @options[:volume], other_client.address
end
end
end
end
22 changes: 2 additions & 20 deletions lib/rbtc_arbitrage/trader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ def execute_trade
@buy_client.buy
@sell_client.sell
@buy_client.transfer @sell_client
# Bitstamp.orders.buy @options[:volume], buyer[:price] + 0.001
# MtGox.sell! @options[:volume], :market
# Bitstamp.transfer @options[:volume], ENV['MTGOX_ADDRESS']
end
else
logger.info "Not trading live because cutoff is higher than profit." if @options[:verbose]
Expand All @@ -60,23 +57,8 @@ def execute_trade

def fetch_prices
logger.info "Fetching exchange rates" if @options[:verbose]
threads = [1,2].map do |n|
Thread.new do
if n == 1
Thread.current[:output] = @buy_client.price :buy
else
Thread.current[:output] = @sell_client.price :sell
end
end
end
threads.each_with_index do |thread, i|
thread.join
if i == 0
buyer[:price] = thread[:output]
else
seller[:price] = thread[:output]
end
end
buyer[:price] = @buy_client.price(:buy)
seller[:price] = @sell_client.price(:sell)
prices = [buyer[:price], seller[:price]]
@paid = prices.min * 1.005 * @options[:volume]
@received = prices.max * 0.994 * @options[:volume]
Expand Down
8 changes: 8 additions & 0 deletions spec/clients/bitstamp_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@
end
end
end

describe "#transfer" do
it "calls Bitstamp correctly" do
sell_client = RbtcArbitrage::Clients::MtGoxClient.new
Bitstamp.should_receive(:transfer).with(0.01, sell_client.address)
client.transfer sell_client
end
end
end
8 changes: 8 additions & 0 deletions spec/clients/mtgox_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@
client.trade(:buy)
end
end

describe "#transfer" do
it "calls MtGox correctly" do
sell_client = RbtcArbitrage::Clients::BitstampClient.new
MtGox.should_receive(:withdraw!).with(0.01, sell_client.address)
client.transfer sell_client
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions spec/trader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@
end
end

# describe "#get_balance" do
# it "fetches the right balance", :vcr do
# trader.validate_env
# balances = MtGox.balance
# balance = RbtcArbitrage::Clients::MtGoxClient.new(:mtgox).balance
# balance.should == [balances[0].amount.to_f, balances[1].amount.to_f]
# balances = Bitstamp.balance
# RbtcArbitrage::Client.new(:bitstamp).balance.should == [balances["usd_available"].to_f, balances["btc_available"].to_f]
# end
# end

describe "#initialize" do
let(:options) {
{
Expand Down

0 comments on commit ccff8be

Please sign in to comment.