def do_sell(self, args): try: opts, args = getopt.getopt(args.split(), 's:a:') except getopt.GetoptError as err: print str(err) opts, args = ([], []) if (len(opts) != 1 or len(args) != 1): self.do_help("sell") return price = float(args[0]) btc_available = float(json.loads(api.balance(self._api_auth()))["btc_available"]) btc_amount = 0.0 if ('-s' == opts[0][0]): btc_share = float(opts[0][1]) if (btc_share < 0.0 or btc_share > 1.0): print "BTC share {} not in expected range (0.0 - 1.0).".format(btc_share) return btc_amount = btc_share * btc_available if ('-a' == opts[0][0]): btc_amount = float(opts[0][1]) if (btc_amount < 0.0 or btc_amount > btc_available): print "BTC amount {} not available.".format(btc_amount) return if (btc_amount == 0.0): print "Can't sell 0.0 BTC." return confirm = raw_input("Selling {} BTC for {} USD/BTC? [y/n]: ".format(btc_amount, price)) if (confirm != "y"): return print api.sell(self._api_auth(), btc_amount, price)
def do_buy(self, args): try: opts, args = getopt.getopt(args.split(), 's:a:') except getopt.GetoptError as err: print str(err) opts, args = ([], []) if (len(opts) != 1 or len(args) != 1): self.do_help("buy") return price = float(args[0]) usd_available = float(json.loads(api.balance(self._api_auth()))["usd_available"]) # correct available USD for the fee balance_json = json.loads(api.balance(self._api_auth())) fee = (float(balance_json["fee"]) / 100.0) usd_available = usd_available * (1.0 - fee) - 0.01 print "Available USD for buying: " + str(usd_available) btc_amount = 0.0 if ('-s' == opts[0][0]): usd_share = float(opts[0][1]) if (usd_share < 0.0 or usd_share > 1.0): print "USD share {} not in expected range (0.0 - 1.0).".format(usd_share) return btc_amount = (usd_share * usd_available) / price if ('-a' == opts[0][0]): usd_amount = float(opts[0][1]) if (usd_amount < 0.0 or usd_amount > usd_available): print "USD amount {} not available.".format(usd_amount) return btc_amount = usd_amount / price if (btc_amount == 0.0): print "Can't buy 0.0 BTC." return confirm = raw_input("Buying {} BTC for {} USD/BTC? [y/n]: ".format(btc_amount, price)) if (confirm != "y"): return print api.buy(self._api_auth(), btc_amount, price)
def do_balance(self, args): ret = json.loads(api.balance(self._api_auth())) print json.dumps(ret, indent=2)