Skip to content

Instantly share code, notes, and snippets.

@riordant
Last active December 13, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riordant/06d7461fd42c45fa2b6fb2b4ec3376d6 to your computer and use it in GitHub Desktop.
Save riordant/06d7461fd42c45fa2b6fb2b4ec3376d6 to your computer and use it in GitHub Desktop.
A small python script to detect bitcoin-euro arbitrage opportunities between a number of popular euro cryptocurrency exchanges.
import requests
import json
import pdb
import time
import sys
BUY = 0
SELL = 1
PRICE = 0
AMOUNT = 1
VALUE = 0
STRING = 1
num_exchanges = 4
#API URLs for btc eur pairs.
kraken = "https://api.kraken.com/0/public/Depth?pair=XXBTZEUR"
bitfinex = "https://api.bitfinex.com/v1/book/btceur"
bitstamp = "https://www.bitstamp.net/api/v2/order_book/btceur"
gdax = "https://api.gdax.com/products/BTC-EUR/book?level=3"
exchanges = []
def get_name(id):
if(id==0): return "Kraken"
if(id==1): return "BitFinex"
if(id==2): return "Bitstamp"
if(id==3): return "GDAX"
def format_market(url):
#standard market format: a 2d array, BUYs = 0 and SELLs = 1, and then each element is a 2d array of {PRICE,AMOUNT}
market = [],[]
response = requests.get(url).json()
if url == kraken:
buys = response.get("result").get("XXBTZEUR").get("asks")
sells = response.get("result").get("XXBTZEUR").get("bids")
for buy in buys:
market[BUY].append([float(buy[PRICE]),float(buy[AMOUNT])])
for sell in sells:
market[SELL].append([float(sell[PRICE]),float(sell[AMOUNT])])
return market
if url == bitfinex:
buys = response.get("asks")
sells = response.get("bids")
for buy in buys:
market[BUY].append([float(buy.get("price")),float(buy.get("amount"))])
for sell in sells:
market[SELL].append([float(sell.get("price")),float(sell.get("amount"))])
return market
if url == bitstamp:
buys = response.get("asks")
sells = response.get("bids")
for buy in buys:
market[BUY].append([float(buy[PRICE]),float(buy[AMOUNT])])
for sell in sells:
market[SELL].append([float(sell[PRICE]),float(sell[AMOUNT])])
return market
if url == gdax:
buys = response.get("asks")
sells = response.get("bids")
for buy in buys:
market[BUY].append([float(buy[PRICE]),float(buy[AMOUNT])])
for sell in sells:
market[SELL].append([float(sell[PRICE]),float(sell[AMOUNT])])
return market
if __name__ == '__main__':
#will try all values from this value to zero.
while(True):
print "\n\ngetting markets..."
exchanges = []
best = [[0,""] for x in range((num_exchanges*num_exchanges)-num_exchanges)] #array pair is buy_counter * num_exchanges + sell_counter.
exchanges.append(format_market(kraken))
print("got Kraken..")
exchanges.append(format_market(bitfinex))
print("got BitFinex..")
exchanges.append(format_market(bitstamp))
print("got Bitstamp..")
exchanges.append(format_market(gdax))
print("got GDAX..")
eur_amount = 200000
while(eur_amount > 0):
best_index_offset = 0
for buy_exchange_counter,buy_exchange in enumerate(exchanges):
for sell_exchange_counter,sell_exchange in enumerate(exchanges):
if buy_exchange_counter == sell_exchange_counter:
best_index_offset+=1
else: #Don't compare same market
for market in buy_exchange: #Cycle buy markets in buy exchange
buy_market = market[BUY]
if (buy_market[PRICE] * buy_market[AMOUNT]) >= eur_amount: #Ensure we have enough to deal with depth
buy_btc_amount = eur_amount / buy_market[PRICE]
for market in sell_exchange:
sell_market = market[SELL]
exchange_pair_index = ((buy_exchange_counter * num_exchanges)+sell_exchange_counter)-(best_index_offset)
if buy_btc_amount <= sell_market[AMOUNT]: #Ensure the market is selling enough btc for what we have.
new_eur_amount = buy_btc_amount * sell_market[PRICE]
if new_eur_amount > eur_amount: #if profit detected
percentage = ((new_eur_amount - eur_amount) / eur_amount) * 100
profit_val = new_eur_amount-eur_amount
profit_str = "Buy " + str(buy_btc_amount) + " BTC from " + get_name(buy_exchange_counter) + " for " + str(eur_amount) + " EUR, sell to " + get_name(sell_exchange_counter) + " for " + str(new_eur_amount) + "EUR, " + str(percentage) + "%, profit of " + str(new_eur_amount-eur_amount) + " EUR"
#if the newly found profit margin is better than the current greatest profit margin for this exchange pair..
if(profit_val > best[exchange_pair_index][VALUE]):
best[exchange_pair_index][VALUE] = profit_val
best[exchange_pair_index][STRING] = profit_str
if(best[exchange_pair_index][STRING]==""):
best[exchange_pair_index][STRING] = "No pair found for " + get_name(buy_exchange_counter) + " and " + get_name(sell_exchange_counter) + "."
eur_amount-=1
for p in best:
#if(not(p[STRING]==[])):
print p[STRING]
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment