Last active
November 10, 2019 08:48
-
-
Save rocket-pig/3f95988a48feed29bf585ac686d5f492 to your computer and use it in GitHub Desktop.
The absolute least required to make binance api requests in one function. everything is in this file and only pure python is needed. You can just scroll to bottom and create an apiGet('api/v1/etcetc') call and start parsing results without spending ten hours learning syntax and workarounds for some new python module (ccxt)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# the absolute least required to make binance api requests in one function. | |
# everything is in this file and only pure python is needed. | |
# can just scroll to bottom and create an apiGet('api/v1/etcetc') call and start | |
# parsing results without spending ten hours learning syntax and workarounds | |
# for some new python module (ccxt) | |
#API Secrets go here: | |
akey="get_your_own_from_your_binance_settings_page" | |
skey="fillmeindaddy" | |
import time | |
import json | |
import hmac | |
import hashlib | |
import requests | |
import sys | |
from urllib.parse import urljoin, urlencode | |
# the basic function. path is API path ( find em at https://binance-docs.github.io/apidocs/spot/en/ ) | |
# params are any args to path | |
# sign = True/False, off by default. if hmac signing is required by path, then True it. | |
# timestamp = same as above. convenience to write True instead of int(time.time... | |
# recvWindow = usually required when 'sign' is. | |
# RETURNS: the result, or will throw exception and crash. | |
def apiGet(path,params={},sign=False,timestamp=False,recvWindow=False): | |
BASE_URL = 'https://api.binance.com' | |
headers = { | |
'X-MBX-APIKEY': akey | |
} | |
if timestamp: | |
params['timestamp'] = int(time.time() * 1000) | |
if recvWindow: | |
params['recvWindow'] = 59999 | |
query_string = urlencode(params) | |
if sign: | |
params['signature'] = hmac.new(skey.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest() | |
url = urljoin(BASE_URL, path) | |
r = requests.get(url, headers=headers,params=params) | |
if r.status_code == 200: | |
#print(json.dumps(r.json(), indent=2)) | |
return(r.json()) | |
else: | |
print(json.dumps(r.json(), indent=2)) | |
raise Exception | |
# The above is literally it. I'm going to leave tested and working calls below | |
# commented out for future ease of use. | |
# Example of call with no params, and iterating over returned dict. | |
info = apiGet('api/v3/exchangeInfo') | |
marginAllowed,marginNotAllowed = 0,0 | |
for i in info['symbols']: | |
if i['isMarginTradingAllowed'] == True: | |
marginAllowed+=1 | |
else: | |
marginNotAllowed+=1 | |
print("Margin is allowed on %s, not allowed on %s markets." % (marginAllowed,marginNotAllowed) ) |
things become one(ish)-liners now pretty quickly:
# easily get current BTC price.
btcPrice = apiGet('api/v3/avgPrice',params={'symbol':'BTCUSDT'})
print(btcPrice['price'])
#all 'assets' that we have a positive balance in
accts = apiGet('api/v3/account',params={},sign=True,timestamp=True,recvWindow=True)
for i in accts['balances']:
if float(i['free']) > 0 or float(i['locked']) > 0:
print(i['asset'])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
large credit is due to this guy:
https://code.luasoftware.com/tutorials/cryptocurrency/python-connect-to-binance-api/