Skip to content

Instantly share code, notes, and snippets.

@Xavier59
Created September 12, 2022 12:14
Show Gist options
  • Save Xavier59/1969c5cc5b5d6add52df25f089cc6d52 to your computer and use it in GitHub Desktop.
Save Xavier59/1969c5cc5b5d6add52df25f089cc6d52 to your computer and use it in GitHub Desktop.
Compare liquidity between Kraken and Binance
import requests
def list_common_pairs(kraken, binance):
common = []
for bcurr in binance["symbols"]:
# REMOVE THIS LINE IF YOU DON'T WANT ANY FILTER.
# HERE, ONLY RETAINS EUR PAIRS
if "EUR" not in bcurr["symbol"]:
continue
for kcurr in [*kraken["result"]]:
if bcurr["symbol"] == kraken["result"][kcurr]["altname"]:
common.append(bcurr["symbol"])
return common
def get_kraken_liquidity(pair, depths):
ob = requests.get("https://api.kraken.com/0/public/Depth?pair="+pair)
if(ob.status_code != 200):
print("Failed to fetch order book for kraken, probably rate limited !")
print(ob)
return
ob = ob.json()
depth_res = {"asks": {}, "bids": {}}
key = [*ob["result"]][0]
for direction in ["asks", "bids"]:
total = 0
for order in ob["result"][key][direction]:
total += float(order[0]) * float(order[1])
for index, depth in enumerate(depths):
if not depth in [*depth_res[direction]]:
depth_res[direction][depth] = 0
if total > depth and depth_res[direction][depth] == 0:
depth_res[direction][depth] = float(order[0])
if(index == len(depths) - 1):
break
return depth_res
def get_binance_liquidity(pair, depths):
ob = requests.get("https://www.binance.com/api/v3/depth?symbol="+pair)
if(ob.status_code != 200):
print("Failed to fetch order book for binance, probably rate limited !")
print(ob)
return
ob = ob.json()
depth_res = {"asks": {}, "bids": {}}
for direction in ["asks", "bids"]:
total = 0
for order in ob[direction]:
total += float(order[0]) * float(order[1])
for index, depth in enumerate(depths):
if not depth in [*depth_res[direction]]:
depth_res[direction][depth] = 0
if total > depth and depth_res[direction][depth] == 0:
depth_res[direction][depth] = float(order[0])
if(index == len(depths) - 1):
break
return depth_res
def main():
kraken_pairs = requests.get("https://api.kraken.com/0/public/AssetPairs")
binance_pairs = requests.get("https://api.binance.com/api/v3/exchangeInfo")
if(kraken_pairs.status_code != 200 or binance_pairs.status_code != 200):
print("{}", kraken_pairs if kraken_pairs.status_code != 200 else binance_pairs)
print("Failed to fetch assets")
return
pairs = list_common_pairs(kraken_pairs.json(), binance_pairs.json())
depths = [100, 1000, 10000]
total_better = {"binance": {}, "kraken": {}}
for depth in depths:
total_better["binance"][str(depth)] = 0
total_better["kraken"][str(depth)] = 0
for pair in pairs:
kraken_liq = get_kraken_liquidity(pair, depths)
binance_liq = get_binance_liquidity(pair, depths)
print(pair + ":")
print(kraken_liq)
print(binance_liq)
for depth in depths:
if(not kraken_liq["bids"][depth] or not binance_liq["bids"] or not kraken_liq["asks"][depth] or not binance_liq["bids"][depth]):
print(f"Error on the {pair} pair, orderbook probably too stretched or pair is deactivated")
continue
perc_diff_bids = (max(kraken_liq["bids"][depth], binance_liq["bids"][depth]) / min(kraken_liq["bids"][depth], binance_liq["bids"][depth]) - 1) * 100
perc_diff_asks = (1 - min(kraken_liq["bids"][depth], binance_liq["bids"][depth]) / max(kraken_liq["bids"][depth], binance_liq["bids"][depth])) * 100
if kraken_liq["bids"][depth] > binance_liq["bids"][depth]:
print(f"For €{depth} Kraken has a better selling price by {perc_diff_bids} %")
total_better["kraken"][str(depth)]+=1
else:
print(f"For €{depth} Binance has a better selling price by {perc_diff_bids} %")
total_better["binance"][str(depth)]+=1
if kraken_liq["asks"][depth] > binance_liq["asks"][depth]:
print(f"For €{depth} Kraken has a better buying price by {perc_diff_asks} %")
total_better["kraken"][str(depth)]+=1
else:
print(f"For €{depth} Binance has a better buying price by {perc_diff_asks} %")
total_better["binance"][str(depth)]+=1
sum_binance = sum_kraken = 0
for depth in depths:
print(f"For €{depth} Binance order book was better in {total_better['binance'][str(depth)]} cases")
print(f"For €{depth} Kraken order book was better in {total_better['kraken'][str(depth)]} cases")
sum_binance += total_better["binance"][str(depth)]
sum_kraken += total_better["kraken"][str(depth)]
print(f"Binance order book was better in {sum_binance} cases")
print(f"Kraken order book was better in {sum_kraken} cases")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment