Last active
August 29, 2015 14:12
-
-
Save donpdonp/aa02b205b6624a171ed4 to your computer and use it in GitHub Desktop.
cointhink arbitrage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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