Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active August 29, 2015 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donpdonp/aa02b205b6624a171ed4 to your computer and use it in GitHub Desktop.
Save donpdonp/aa02b205b6624a171ed4 to your computer and use it in GitHub Desktop.
cointhink arbitrage
module Arbitrage
// exchanges: [ exchange, ... ]
// exchange: { name: "Name", markets: [ market, ...] }
// market: { buy: '<currency code>', sell: '<currency code>', offers: [offer, ...] }
// offer: { quantity: 1.0, price: 2.0 }
def calc(exchanges)
bids = asks = []
exchanges.each do |exchange|
bids += exchange.bid_market('btc').offers.sort(&:price)
asks += exchange.ask_market('btc').offers.sort(&:price)
end
# only look at offers with potential
bids = bids.select{|bid| bid.price > asks[0]}
asks = asks.select{|ask| ask.price < bids[0]}
# purse limited by either the available asks or available bids
bid_market_size = bids.sum{|b|b.price*b.quantity}
ask_market_size = asks.sum{|a|a.price*a.quantity}
usd_purse = Math.min(bid_market_size, ask_market_size)
# buy low from the asks
portfolio = buy(usd_purse, asks)
# sell high to the bids
usd_proceeds = sell(portfolio, bids)
# whats left is profit
profit = usd_proceeds - usd_purse
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment