Skip to content

Instantly share code, notes, and snippets.

@riordant
Last active January 13, 2018 01:37
Show Gist options
  • Save riordant/524f0d58d3b7cf63e1af87fd3e89436c to your computer and use it in GitHub Desktop.
Save riordant/524f0d58d3b7cf63e1af87fd3e89436c to your computer and use it in GitHub Desktop.
A script to get your total USDT balance on Bittrex via the API. Create API/secret combo on Bittrex and insert under 'key' and 'secret'
from time import time
import hashlib
import hmac
import requests
key = "API_KEY"
secret = "SECRET_KEY"
nonce = str(int(time() * 1000))
#sign and call the url.
def call(url):
sign = hmac.new(secret, url, hashlib.sha512).hexdigest()
headers = {'apisign': sign,}
return(requests.get(url, headers=headers).json())
#no direct function to get balance in usdt.
#this gets all the balances, turns into BTC and at the end into USDT.
def get_usdt_balance():
#first get balances
price_base = "https://bittrex.com/api/v1.1/public/getticker?market=BTC-"
price_usd = "https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc"
btc_usdt = call(price_usd)["result"]["Bid"]
url = "https://bittrex.com/api/v1.1/account/getbalances?apikey=" + key + "&nonce=" + nonce
currencies = call(url)["result"]
btc_total = 0
for currency in currencies:
balance = currency["Balance"]
if(balance>0):
name = currency["Currency"]
print("name: " + str(name))
print("balance: " + format(balance, '.8f'))
url = price_base + str(name)
if(name=="BTC"):
btc_price = 1
elif(name=="USDT"):
btc_price = 1/btc_usdt
else:
btc_price = call(url)["result"]["Bid"]
print("btc price per coin: " + format(btc_price, '.8f'))
amount_btc = (btc_price * balance)
print("amount btc: " + format(amount_btc, '.8f'))
btc_total += amount_btc
print("")
#we now have total btc in the account.
#grab usdt price and return.
return(str(btc_total * btc_usdt) + " USD")
print(get_usdt_balance())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment