Permalink
Browse files

Market: add the 'buy' and 'sell' commands to estimate market impact o…

…f an order.

Thanks copumpkin for the initial draft.
  • Loading branch information...
Daniel Folkinshteyn
Daniel Folkinshteyn committed Feb 26, 2012
1 parent 2cea559 commit 53b88b241f95223f28ea69cb862d246ba9b46959
Showing with 83 additions and 14 deletions.
  1. +83 −14 Market/plugin.py
View
@@ -71,47 +71,116 @@ def _getTicker(self):
def sell(self, irc, msg, args, optlist, value):
"""[--usd] <value>
- Calculate the effect on the market depth of a market order of <value>
- bitcoins. If '--usd' option is given, simulate a sale of <value> USD
- worth of bitcoins in a market order.
+ Calculate the effect on the market depth of a market sell order of
+ <value> bitcoins. If '--usd' option is given, <value> denotes the
+ size of the order in USD.
"""
try:
mdepth = self._getMarketDepth()
except:
irc.error("Failure to retrieve order book data. Try again later.")
return
+ bids = mdepth['bids']
+ bids.reverse() # bids are listed in ascending order
if dict(optlist).has_key('usd'):
n_coins = 0.0
total = value
top = 0.0
for bid in bids:
if total <= bid['amount'] * bid['price']:
n_coins += total / bid['price']
- top = bid['price']
+ top = bid['price']
break
else: # we can eat the entire order
n_coins += bid['amount']
- total -= bid['amount'] * bid['price']
- irc.reply("A market order to sell %s USD worth of bitcoins right"
- " now would sell %s bitcoins and would take the last "
- " price down to %s USD." % (value, n_coins, top))
+ total -= bid['amount'] * bid['price']
+ else:
+ irc.reply("This order would exceed the size of the order book. "
+ "You would sell %.8g bitcoins for a total of %.4f USD and "
+ "take the price to 0." % (n_coins, value - total,))
+ return
+ irc.reply("A market order to sell %.4f USD worth of bitcoins right "
+ "now would sell %.8g bitcoins and would take the last "
+ "price down to %.4f USD." % (value, n_coins, top,))
+ return
else:
n_coins = value
total = 0.0
top = 0.0
- bids = mdepth['bids']
for bid in bids:
if n_coins <= bid['amount']: # we don't have enough
total += n_coins * bid['price']
- top = bid['price']
+ top = bid['price']
break
else: # we can eat the entire order
n_coins -= bid['amount']
- total += bid['amount'] * bid['price']
+ total += bid['amount'] * bid['price']
+ else:
+ irc.reply("This order would exceed the size of the order book. "
+ "You would sell %.8g bitcoins, for a total of %.4f USD and "
+ "take the price to 0." % (value - n_coins, total,))
+ return
irc.reply("A market order to sell %.8g bitcoins right now would "
- "net %s and would take the last price down to %s USD."
- % (value, total, top))
- sell = wrap(sell, [getopts({'usd': '',}), 'nonNegativeFloat']))
+ "net %.4f USD and would take the last price down to %.4f USD."
+ % (value, total, top))
+ sell = wrap(sell, [getopts({'usd': '',}), 'nonNegativeFloat'])
+
+ def buy(self, irc, msg, args, optlist, value):
+ """[--usd] <value>
+
+ Calculate the effect on the market depth of a market buy order of
+ <value> bitcoins. If '--usd' option is given, <value> denotes the
+ size of the order in USD.
+ """
+ try:
+ mdepth = self._getMarketDepth()
+ except:
+ irc.error("Failure to retrieve order book data. Try again later.")
+ return
+ asks = mdepth['asks']
+ if dict(optlist).has_key('usd'):
+ n_coins = 0.0
+ total = value
+ top = 0.0
+ for ask in asks:
+ if total <= ask['amount'] * ask['price']:
+ n_coins += total / ask['price']
+ top = ask['price']
+ break
+ else: # we can eat the entire order
+ n_coins += ask['amount']
+ total -= ask['amount'] * ask['price']
+ else:
+ irc.reply("This order would exceed the size of the order book. "
+ "You would buy %.8g bitcoins for a total of %.4f USD and "
+ "take the price to %.4f." % (n_coins, value - total, ask['price'],))
+ return
+ irc.reply("A market order to buy %.4f USD worth of bitcoins right "
+ "now would buy %.8g bitcoins and would take the last "
+ "price up to %.4f USD." % (value, n_coins, top,))
+ return
+ else:
+ n_coins = value
+ total = 0.0
+ top = 0.0
+ for ask in asks:
+ if n_coins <= ask['amount']: # we don't have enough
+ total += n_coins * ask['price']
+ top = ask['price']
+ break
+ else: # we can eat the entire order
+ n_coins -= ask['amount']
+ total += ask['amount'] * ask['price']
+ else:
+ irc.reply("This order would exceed the size of the order book. "
+ "You would buy %.8g bitcoins, for a total of %.4f USD and "
+ "take the price to %.4f." % (value - n_coins, total, ask['price']))
+ return
+ irc.reply("A market order to buy %.8g bitcoins right now would "
+ "take %.4f USD and would take the last price up to %.4f USD."
+ % (value, total, top))
+ buy = wrap(buy, [getopts({'usd': '',}), 'nonNegativeFloat'])
+
def asks(self, irc, msg, args, optlist, pricetarget):
"""[--over] <pricetarget>

0 comments on commit 53b88b2

Please sign in to comment.