Last active
July 28, 2021 15:11
-
-
Save kukushi/9ef50b523d40359b70b2b48aa2112516 to your computer and use it in GitHub Desktop.
Binance Triangular Arbitrage
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
import requests | |
import datetime | |
import operator | |
host = 'https://api.binance.com' | |
priceURL = host + '/api/v3/ticker/price' | |
priceResponse = requests.get(priceURL) | |
priceResponseJSON = priceResponse.json() | |
# {'symbol': 'ETHBTC', 'price': '0.07608700'}: 1 ETH = 0.0760 BTC | |
# It's a BTC pair. ETH = price * BTC | |
# Buy: BTC -> ETH | |
BTC_Pairs = dict() # 1 AC = x BTC | |
ETH_Pairs = dict() # 1 AC = y ETH | |
for pair in priceResponseJSON: | |
coin_name = pair['symbol'][:-3] | |
quote_name = pair['symbol'][-3:] | |
if quote_name == 'BTC': | |
BTC_Pairs[coin_name] = pair | |
elif quote_name == 'ETH': | |
ETH_Pairs[coin_name] = pair | |
ETH_price = float(BTC_Pairs["ETH"]["price"]) | |
print("Binance: Triangular-Arbitrage") | |
print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) | |
# Buy ETH/BTC (BTC -> ETH, 1 * ETH = price * BTC) | |
# Buy AC/ETH (ETH -> AC, 1 * AC = price * ETH) | |
# Sell AC/BTC (AC -> BTC, 1 * AC = price * BTC) | |
result = dict() | |
for k, v in BTC_Pairs.items(): | |
if k in ETH_Pairs: | |
coin2btc = 1 / (float(ETH_price) * float(ETH_Pairs[k]["price"])) | |
btcbuy = 1 / float(BTC_Pairs[k]["price"]) #BTC买一 | |
profit = round((coin2btc - btcbuy) * 1000 / coin2btc, 2) | |
result[k] = profit | |
result = sorted(result.items(), key=operator.itemgetter(1), reverse=True) | |
formatted_result = dict() | |
for i in result: | |
formatted_result[i[0]] = i[1] | |
count = 10 | |
for i, v in BTC_Pairs.items(): | |
if not i in ETH_Pairs: | |
continue | |
name = i | |
# print("%s\t%10.8f\t%10.8f\t%10.8f\t%10.8f\t"%(name,formatted_result[name], ETH_price, float(ETH_Pairs[name]["price"]), float(BTC_Pairs[name]["price"]))) | |
if formatted_result[name] < 0: | |
continue | |
depth_url = host + "/api/v1/depth?symbol=" + name + "ETH" | |
# Buy | |
buy_depth = requests.get(depth_url).json() | |
buy_price = buy_depth["asks"][0][0] | |
depth_url_btc = host + "/api/v1/depth?symbol=" + name + "BTC" | |
# Buy | |
sell_depth = requests.get(depth_url_btc).json() | |
sell_price = sell_depth["bids"][0][0] | |
coin2btc = 1 / (float(ETH_price) * float(buy_price)) | |
btcbuy = 1 / float(sell_price) | |
profit = round((coin2btc - btcbuy) * 1000 / coin2btc, 2) | |
btc_amount_after_ta = coin2btc*float(sell_price) | |
if btc_amount_after_ta >= 1.0015: | |
print('1 BTC = %10.8f ETH = %10.8f %s = %s BTC, %10.2f'%(1 / float(ETH_price), coin2btc, name, btc_amount_after_ta, (btc_amount_after_ta-1) * 100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment