building an arbitrage bot for crypto currencies on binance and kucoin

with python

Today we will be making a simple arbitrage bot with python. I’m using binance and kucoin because crypto only exchanges are easy to put money on and they support all the coins that are fast enough to take advantage of differences in the market.

Part 1. setup

  1. download python3 and pip3

Linux:

sudo apt-get update
sudo apt-get install python3.6
sudo apt-get install python3-pip

Windows:

follow this guide

Mac:

follow this guide

2. download two packages

Q: But why would we need packages we can just use requests!?

A: I’m lazy

Here’s the documentation python-kucoin, python-binance

Thank you Sam Mchardy

pip3 install python-kucoin
pip3 install python-binance

3. If you don’t have an account already sign up for

Binance

and Kucoin

You’ll need to navigate to settings and copy your api keys at this point here is our starter file.

import time
from binance.client import Client as BinanceClient
from kucoin.client import Client
client = Client(API_KEY, API_SECRET)
bclient = BinanceClient(API_KEY, API_SECRET)
##kucoin
orders = client.get_buy_orders('BTC-ETH', limit=50)
sellorders = client.get_sell_orders('BTC-ETH', limit=50)
##binance
depth = bclient.get_order_book(symbol='BTCETH')
print(orders)
print(sellorders)
print(depth)

Save this as testfile.py and run it with python3 testfile.py in your command line. you should see some pretty json print out that is the order books for BTC and ETH. Now BTC and ETH are not very good for arbitrage because bitcoin is a dinosaur coin and will take forever to transfer. So lets write a little script to identify the biggest difference in prices on the exchange and then make our decision for currencies to arb.

import time
from binance.client import Client as BinanceClient
from kucoin.client import Client
client = Client(API_KEY, API_SECRET)
bclient = BinanceClient(API_KEY, API_SECRET)
coins = client.get_trading_symbols() #get all the kucoin price
prices = bclient.get_all_tickers() #get all the binance prices
kucoinDict = {} # create a dictionaryfor coin in coins:
coinsymbol = coin['symbol']
coinsymbol = coinsymbol.replace('-','') # we need to remove the dash so we can compare to binance
lastDeal = coin['lastDealPrice']
kucoinDict[coinsymbol] = lastDeal #add to the dictionary to compare binance and kucoin
differenceDict = {}for price in prices:
bsymbol = price['symbol']
bprice = price['price']
if bsymbol in kucoinDict:
diff = kucoinDict[bsymbol] - float(bprice) #find the biggest difference
if bsymbol[-3:] == 'ETH': #only check things trading in eth
diff = abs(diff) #change any negatives to posotive
differenceDict[bsymbol] = diff
print(sorted(differenceDict.items(), key=lambda x: x[1])) # sort it

and there we have it bcd-eth and dash-eth have the biggest differences for me at the moment. Next we’re going to move on to setting up a script that will run indefinitely buy any order on binance or kucoin that is selling for a price greater on the other exchange. First make sure you can withdrawal and deposit the coins you will be trading in on both sites!!!!! Kucoin doesn’t support some deposits/withdrawals such as bitcoin diamond (BCD).

alright so let’s write a script that will buy low on one site, transfer the coins and then sell on the other. Here’s the naive trader it won’t make the trade on binance on it’s own yet but it will place nice low orders on kucoin then make the withdrawal to binance.

import time
from binance.client import Client as BinanceClient
from kucoin.client import Client
client = Client(APIKEY,APISECRET)
bclient = BinanceClient(APIKEY,APISECRET)
def watch():
try:
orders = client.get_buy_orders('DASH-ETH', limit=5)
sellorders = client.get_sell_orders('DASH-ETH', limit=5)
depth = bclient.get_order_book(symbol='DASHETH')
bsell = depth['asks'][0][0]
bbuy = depth['bids'][0][0]
ksell = sellorders[0][0]
kbuy = orders[0][0]
kbuyAdd = kbuy + 0.01
except:
print('problem grabbing order books')
kbuyAdd = 2
bbuy = 1
#if an ask is larger than a sell order arbitrage
if kbuyAdd < float(bbuy):
print('buy kucoin sell binance')
print(sellorders[0])
print(depth['bids'][0])
dashbalance = client.get_coin_balance('ETH')
print(dashbalance['balance'])
kucoinBalance = dashbalance['balance'] #get balance
buyPrice = kbuy + 0.000001 #place buy order at top
print(kucoinBalance/buyPrice)
amount = kucoinBalance/buyPrice #find the amount you can buy
print('buying %s dash coin on kucoin' % amount)
try:
transaction = client.create_buy_order('DASH-ETH', buyPrice, amount)
print(transaction)
maketrade(transaction['orderOid'])
except:
client.cancel_all_orders()
print('problem making trade')
time.sleep(7)def maketrade(oid):
for x in range(0,9):
time.sleep(1)
orders = client.get_active_orders('DASH-ETH')
time.sleep(1)
print(orders)
if orders['BUY']:
print('order has not been filled')
else:
print('the order has been filled')
dashbalance = client.get_coin_balance('DASH')
address = bclient.get_deposit_address(asset='DASH')
client.create_withdrawal('DASH', float(dashbalance['balance']), str(address['address']))
makeTradeBinance()
print(x)
if x == 8:
client.cancel_all_orders()
time.sleep(1)
def makeTradeBinance():
print('madeit')
while True:
watch();

Thanks for reading!

be sure to check out my other article about predicting the Price of Bitcoin with machine learning.

--

--