def test_formatCurrencyByPair(self): info = btceapi.APIInfo(self.connection) for i in info.pairs.values(): d = i.decimal_places self.assertEqual(i.format_currency(1.12), btceapi.formatCurrencyDigits(1.12, d)) self.assertEqual(i.format_currency(44.0), btceapi.formatCurrencyDigits(44.0, d)) self.assertEqual(i.truncate_amount(1.12), btceapi.truncateAmountDigits(1.12, d)) self.assertEqual(i.truncate_amount(44.0), btceapi.truncateAmountDigits(44.0, d))
def test_formatCurrency(self): self.assertEqual(btceapi.formatCurrencyDigits(1.123456789, 1), "1.1") self.assertEqual(btceapi.formatCurrencyDigits(1.123456789, 2), "1.12") self.assertEqual(btceapi.formatCurrencyDigits(1.123456789, 3), "1.123") self.assertEqual(btceapi.formatCurrencyDigits(1.123456789, 4), "1.1234") self.assertEqual(btceapi.formatCurrencyDigits(1.123456789, 5), "1.12345") self.assertEqual(btceapi.formatCurrencyDigits(1.123456789, 6), "1.123456") self.assertEqual(btceapi.formatCurrencyDigits(1.123456789, 7), "1.1234567") for i in range(2, 8): print(i) self.assertEqual(btceapi.formatCurrencyDigits(1.12, i), "1.12") self.assertEqual(btceapi.formatCurrencyDigits(44.0, i), "44.0")
print "\tCurrent value of open orders:" orders = t.activeOrders(connection=conn) if orders: for o in orders: c1, c2 = o.pair.split("_") c2_equiv = o.amount * exchange_rates[o.pair] if c2 == "btc": btc_equiv = c2_equiv else: btc_equiv = c2_equiv / exchange_rates["btc_%s" % c2] btc_str = btceapi.formatCurrency(btc_equiv, o.pair) print "\t\t%s %s %s @ %s (~%s BTC)" % ( o.type, o.amount, o.pair, o.rate, btc_str) btc_total += btc_equiv else: print "\t\tThere are no open orders." btc_str = btceapi.formatCurrency(btc_total, "btc_usd") print "\n\tTotal estimated value: %s BTC" % btc_str for fiat in ("usd", "eur", "rur"): fiat_pair = "btc_%s" % fiat fiat_total = btc_total * exchange_rates[fiat_pair] fiat_str = btceapi.formatCurrencyDigits(fiat_total, 2) print "\t %s %s" % (fiat_str, fiat.upper()) except Exception as e: print " An error occurred: %s" % e raise e
else: pair = "btc_%s" % currency btc_equiv = balance / exchange_rates[pair] bal_str = btceapi.formatCurrency(balance, pair) btc_str = btceapi.formatCurrency(btc_equiv, "btc_usd") print "\t%s balance: %s (~%s BTC)" % (currency.upper(), bal_str, btc_str) btc_total += btc_equiv print "\tCurrent value of open orders:" orders = t.orderList(connection = conn) for o in orders: # TODO: handle pairs that don't involve BTC btc_equiv = o.amount * exchange_rates[o.pair] btc_str = btceapi.formatCurrency(btc_equiv, pair) print "\t\t%s %s %s @ %s (~%s BTC)" % (o.type, o.amount, o.pair, o.rate, btc_str) btc_total += btc_equiv btc_str = btceapi.formatCurrency(btc_total, "btc_usd") print "\n\tTotal estimated value: %s BTC" % btc_str for fiat in ("usd", "eur", "rur"): fiat_pair = "btc_%s" % fiat fiat_total = btc_total * exchange_rates[fiat_pair] fiat_str = btceapi.formatCurrencyDigits(fiat_total, 2) print "\t %s %s" % (fiat_str, fiat.upper()) except Exception as e: print " An error occurred: %s" % e raise e
def showAccountInfo(self, keyfile): ''' Show account info such as balance and total value of wallet ''' handler = btceapi.KeyHandler(keyfile, resaveOnDeletion=True) for key in handler.getKeys(): # print "Printing info for key %s" % key conn = btceapi.BTCEConnection() t = btceapi.TradeAPI(key, handler=handler) try: r = t.getInfo(connection = conn) print "\t*************" for currency in btceapi.all_currencies: balance = getattr(r, "balance_" + currency) if balance != 0: print "\t%s balance: %s" % (currency.upper(), balance) # print "\tInformation rights: %r" % r.info_rights # print "\tTrading rights: %r" % r.trade_rights # print "\tWithrawal rights: %r" % r.withdraw_rights print "\tServer time: %r" % str(r.server_time) #compute estimated account value too exchange_rates = {} for pair in btceapi.all_pairs: asks, bids = btceapi.getDepth(pair) exchange_rates[pair] = bids[0][0] btc_total = 0 for currency in btceapi.all_currencies: balance = getattr(r, "balance_" + currency) if currency == "btc": # print "\t%s balance: %s" % (currency.upper(), balance) btc_total += balance else: pair = "%s_btc" % currency if pair in btceapi.all_pairs: btc_equiv = balance * exchange_rates[pair] else: pair = "btc_%s" % currency btc_equiv = balance / exchange_rates[pair] bal_str = btceapi.formatCurrency(balance, pair) btc_str = btceapi.formatCurrency(btc_equiv, "btc_usd") #print "\t%s balance: %s (~%s BTC)" % (currency.upper(), bal_str, btc_str) btc_total += btc_equiv #print "\tCurrent value of open orders:" orders = t.activeOrders(connection = conn) if orders: for o in orders: btc_equiv = o.amount * exchange_rates[o.pair] btc_str = btceapi.formatCurrency(btc_equiv, pair) print "\t\t%s %s %s @ %s (~%s BTC)" % (o.type, o.amount, o.pair, o.rate, btc_str) btc_total += btc_equiv else: print "\t\tThere are no open orders." btc_str = btceapi.formatCurrency(btc_total, "btc_eur") print "\tTotal estimated value: %s BTC" % btc_str # for fiat in ("eur"): fiat_pair = "btc_%s" % "eur" fiat_total = btc_total * exchange_rates[fiat_pair] fiat_str = btceapi.formatCurrencyDigits(fiat_total, 2) print "\t %s EUR" % fiat_str print "\t*************" #print "\tItems in transaction history: %r" % r.transaction_count except: print " An error occurred: kakzooi" raise