I am using the Bittrex exchange API to ping markets to poll whether there are triangular arbitrage opportunities available for USD/BTC/LTC/USD. Note that I am not trading but rather synthesising them by using the API to collect bid and ask data for each of the 3 markets. My test polls also account for fees too. My current code base that uses the API is as follows
import requests as rq
import json
from CONSTANTS import API_PUBLIC, API_SECRET
import time
import hmac
import hashlib
def arbitrage():
nonce = time.time()
#Defines the API call that gets the price data
usd_btc_market = 'https://api.bittrex.com/api/v1.1/public/getticker?market=USD-BTC&apikey={0}&nonce={1}'.format(API_PUBLIC, nonce)
btc_ltc_market = 'https://api.bittrex.com/api/v1.1/public/getticker?market=BTC-LTC&apikey={0}&nonce={1}'.format(API_PUBLIC, nonce)
usd_ltc_market = 'https://api.bittrex.com/api/v1.1/public/getticker?market=USD-LTC&apikey={0}&nonce={1}'.format(API_PUBLIC, nonce)
#Ensures connections are secured through hashing
usd_btc_signature = hmac.new(API_SECRET.encode(), usd_btc_market.encode(), hashlib.sha512).hexdigest()
btc_ltc_signature = hmac.new(API_SECRET.encode(), btc_ltc_market.encode(), hashlib.sha512).hexdigest()
usd_ltc_signature = hmac.new(API_SECRET.encode(), usd_ltc_market.encode(), hashlib.sha512).hexdigest()
#Conducts the hypothetical trades using the API bid/ask prices from each market.
#Assumes a starting capital value of $1
btc_balance = (1 / json.loads(rq.get(usd_btc_market, headers = {'apisign': usd_btc_signature}).content.decode('utf-8'))['result']['Ask']) * 0.998
ltc_balance = (btc_balance / json.loads(rq.get(btc_ltc_market, headers = {'apisign': btc_ltc_signature}).content.decode('utf-8'))['result']['Ask']) * 0.998
usd_balance = (ltc_balance * json.loads(rq.get(usd_ltc_market, headers = {'apisign': usd_ltc_signature}).content.decode('utf-8'))['result']['Bid'])* 0.998
print(usd_balance)
while True:
arbitrage()
time.sleep(3) #Limited to 60 API calls per minute, ensures not too many calls are made
The hypothetical trade results appear nearly profitable. Note that if we started with $1, then the final balance at the end of the arbitrage loop are as follows (code ran every 3 seconds):
#Time span of over a 5 minute period
#Each arbitrage loop takes on average 0.48 seconds to run
0.9915490241394859
0.9915511459969782
0.9915511459969782
...
0.9901270999326443
0.9901250816417339
0.9901269101525229
I am using Python to run this bot, however I am wondering if the 'so close yet so far' conundrum could be rectified by switching to a faster language such as c++? According to this post, c++ is atleast 10 times faster than python. My thinking is, could this language, with its faster runtime, overcome the minutely small time increments between each API market call? Could this be the reason why such opportunities aren't being found, that Python is simply to slow to take advantage of fleeting arbitrage opportunities? Or, is it just the case that the Cryptocurrency market is more efficient than I thought and arbitrage opportunities don't persist. Looking forward to hearing back what you think.