Class: Heisencoin::Arbitrage
- Inherits:
-
Object
- Object
- Heisencoin::Arbitrage
- Defined in:
- lib/heisencoin/arbitrage.rb
Instance Attribute Summary collapse
-
#asks ⇒ Object
Returns the value of attribute asks.
-
#bids ⇒ Object
Returns the value of attribute bids.
-
#exchanges ⇒ Object
Returns the value of attribute exchanges.
Instance Method Summary collapse
- #add_depth(exchange, depth) ⇒ Object
- #add_exchanges(exchanges) ⇒ Object
- #best_price(market1, market2) ⇒ Object
- #consume_ask(bids, ask, price_limit) ⇒ Object
- #exchange(name) ⇒ Object
-
#initialize ⇒ Arbitrage
constructor
A new instance of Arbitrage.
- #plan ⇒ Object
- #profitable_asks(highwater = nil) ⇒ Object
- #profitable_bids(lowwater = nil) ⇒ Object
- #spread ⇒ Object
- #trade_all(asks, bids) ⇒ Object
Constructor Details
#initialize ⇒ Arbitrage
Returns a new instance of Arbitrage.
5 6 7 8 9 |
# File 'lib/heisencoin/arbitrage.rb', line 5 def initialize @exchanges = [] @asks = Market.new(:ask) @bids = Market.new(:bid) end |
Instance Attribute Details
#asks ⇒ Object
Returns the value of attribute asks.
3 4 5 |
# File 'lib/heisencoin/arbitrage.rb', line 3 def asks @asks end |
#bids ⇒ Object
Returns the value of attribute bids.
3 4 5 |
# File 'lib/heisencoin/arbitrage.rb', line 3 def bids @bids end |
#exchanges ⇒ Object
Returns the value of attribute exchanges.
3 4 5 |
# File 'lib/heisencoin/arbitrage.rb', line 3 def exchanges @exchanges end |
Instance Method Details
#add_depth(exchange, depth) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/heisencoin/arbitrage.rb', line 19 def add_depth(exchange, depth) @asks.drop_offers(exchange) @asks.import(exchange, depth["asks"]) @bids.drop_offers(exchange) @bids.import(exchange, depth["bids"]) end |
#add_exchanges(exchanges) ⇒ Object
11 12 13 |
# File 'lib/heisencoin/arbitrage.rb', line 11 def add_exchanges(exchanges) @exchanges += exchanges end |
#best_price(market1, market2) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/heisencoin/arbitrage.rb', line 26 def best_price(market1, market2) level = nil market1.offers.each do |offer| good = market2.offers.any?{|offer_other| yield offer.price, offer_other.price} if good level = offer.price break end end level end |
#consume_ask(bids, ask, price_limit) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/heisencoin/arbitrage.rb', line 68 def consume_ask(bids, ask, price_limit) trades = [] price = price_limit quantity = ask.quantity near_zero = 0.0001 #floats bids.each do |bid| if (bid.price*(1+bid.exchange.fee)) >= price if bid.quantity > near_zero trade_quantity = [bid.quantity, quantity].min trades << Trade.new({'from_offer' => ask.to_simple, 'to_offer' => bid.to_simple, 'quantity' => trade_quantity}) bid.quantity -= trade_quantity quantity -= trade_quantity end end break if quantity < near_zero end trades end |
#exchange(name) ⇒ Object
15 16 17 |
# File 'lib/heisencoin/arbitrage.rb', line 15 def exchange(name) exchanges.select{|e| e.name == name}.first end |
#plan ⇒ Object
89 90 91 92 93 |
# File 'lib/heisencoin/arbitrage.rb', line 89 def plan #buy sell plan plan = Plan.new plan << trade_all(profitable_asks, profitable_bids) end |
#profitable_asks(highwater = nil) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/heisencoin/arbitrage.rb', line 38 def profitable_asks(highwater=nil) highwater ||= best_price(@bids, @asks){|offer, other| offer > other} if highwater @asks.offers_better_than(highwater) else [] end end |
#profitable_bids(lowwater = nil) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/heisencoin/arbitrage.rb', line 47 def profitable_bids(lowwater=nil) lowwater ||= best_price(@asks, @bids){|offer, other| offer < other} if lowwater @bids.offers_better_than(lowwater) else [] end end |
#spread ⇒ Object
95 96 97 |
# File 'lib/heisencoin/arbitrage.rb', line 95 def spread @asks.offers.first.price - @bids.offers.first.price end |
#trade_all(asks, bids) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/heisencoin/arbitrage.rb', line 56 def trade_all(asks, bids) working_bids = bids.dup trades = [] asks.each do |ask| step_trades = consume_ask(working_bids, ask, ask.price*(1-ask.exchange.fee)) trades += step_trades qty_traded = step_trades.reduce(0){|memo, trade| memo += trade.quantity} #sum break if qty_traded < ask.quantity #out of coins end trades end |