Crypto Arbitrage with NetworkX and Python PlatoBlockchain Data Intelligence. Vertical Search. Ai.

Crypto Arbitrage with NetworkX and Python

Analyzing crypto data from the Coingecko API to build a crypto arbitrage scanner in python

McKlayne Marshall

Co-authored with Isaac Rhea

Photo by Alina Grubnyak on Unsplash

Markets for currencies around the world trade 24 hours a day at volumes significantly higher than bond, stock, or futures markets. Participants in foreign exchange markets are hedging risk or speculating on future changes in currency values.

Another source of profits comes from taking advantage of short-term imbalances in currency valuations. Using lightning-fast algorithms, high-frequency traders identify arbitrage opportunities and quickly execute a series of exchanges that results in a small profit. See this article from the Corporate Finance Institute for a more in-depth explanation and examples.

Corporate Finance Institute

Due to the high competition and trading volume in currency markets, these opportunities are short-lived and profits are minuscule. Although gains through currency arbitrage can accrue over time with a high number of trades, a similar opportunity exists in cryptocurrency markets which may be even more profitable.

Because there are many cryptos to trade, there are many possible combinations to check for arbitrage opportunities. The Graph (Network) data structure is ideal for keeping track of the different exchange rates between coins and quickly identifying instances of disequilibrium that we can take advantage of. For more info on Graphs/Networks, and Python packages to work with them, check out this book in the Pragmatic Programmers series.

To build a Graph for cryptocurrencies we will leverage the NetworkX package. This is a powerful tool that makes it easy to analyze the coins we are interested in and find trading opportunities. First, we will obtain crypto exchange rates from the CoinGecko API. Then, we will initialize the Graph and define the relationships (exchange rates) between each of the coins we are interested in. Finally, we will loop through all of the paths from one coin to another and back to identify arbitrage opportunities.

If you have experience with JSON APIs the CoinGecko API is relatively simple to use. With this snippet of code, I pulled current exchange rates for five different coins (Bitcoin, Bitcoin Cash, Ethereum, Litecoin, and EOS).

The URL for the API call will look something like this, depending on the coins you would like to pull data for:

https://api.coingecko.com/api/v3/simple/price?ids=bitcoin-cash,ethereum,bitcoin,litecoin,eos&vs_currencies=bch,eth,btc,ltc,eos

By using the Request and JSON packages for Python we can load this data as a dictionary with keys for each crypto that we included in our list. The value associated with each of those keys is another dictionary with entries for the exchange rates for that currency pair. For example, for Bitcoin Cash we get the following result:

This shows that 0.25 Ethereum or 0.16 Bitcoin can be purchased with 1 Bitcoin Cash. With these results for each of the cryptos we are ready to define the Graph.

Each of the coins represents a ‘vertex’ in the Graph and the exchange rate between two coins is an ‘edge’. After initializing an empty Graph object, we define a list of tuples for each pair of coins and their exchange rate in both directions.

The list for the edges will look something like this:

With the edges added to the graph, we are ready to scan for arbitrage opportunities. Using the combinations function from the itertools package we define all of the possible pairs of coins. Then, we use the all_simple_paths function from NetworkX to define all of the possible paths from the first coin to the second.

For example, if we are looking at Litecoin and Bitcoin Cash, there are many possible paths given the coins we are considering. We can simply buy Bitcoin Cash with Litecoin or we can buy Bitcoin with Litecoin and then use Bitcoin to purchase Bitcoin Cash.

We loop through each path and perform the following calculations at each step. First, we assume that we start with one of the initial coins. We multiply that one by the exchange rate from one coin to another until we get to the end of the path.

For example, if we start with one Bitcoin Cash we can buy 0.24 Ethereum so we multiply 1 x 0.24197529 = 0.24197529. The exchange rate from Ethereum to Bitcoin is 0.06 so we multiply 0.24197529 x 0.06484324 = 0.0156904618035396. This value is very close to the exchange rate between Bitcoin Cash and Bitcoin but not exactly the same.

At this point, we check the reverse of the path, i.e., Bitcoin to Ethereum to Bitcoin Cash by multiplying 1 x 15.414849 x 4.132739 = 63.705547641411. We multiply these two results together for our final evaluation of the path (0.0156904618035396 x 63.705547641411 = 0.9995694619411315). To my knowledge, there is not a defined term for this value. We can call it the Arbitrage Factor.

If the exchange rates were in sync, the Arbitrage Factor would have been exactly one. A value less than one suggests that we went through the series of exchanges and finished with less than we started with. So, we are looking for this value to be greater than one as making the exchanges would result in a profit. If we had found the Arbitrage Factor to be 1.005 in our previous example, this would have indicated that by following that path of exchanges from one crypto to another and back, we could have gained 0.005 Bitcoin Cash (worth around $3).

Arbitrage opportunities come and go for different cryptos throughout the day and it is possible to check all combinations for several coins without finding an Arbitrage Factor significantly above one. However, I have seen Arbitrage Factors above 1.01, indicating that a 1% return could be earned in a matter of moments through simple cryptocurrency exchanges.

Bringing the three functions explained above, we are able to create a crypto arbitrage scanner.

Currency arbitrage is a well-established and low-risk trading method, but the market for traditional currencies is very efficient and competitive. A greater opportunity exists in cryptocurrencies and a few simple Python tools can help facilitate the strategy. NetworkX can be used to build a Graph and quickly search for arbitrage opportunities.

However, there are still challenges to overcome. First, the fees to trade cryptocurrencies can be very high. This means that any imbalances between cryptos must be significant in order to be profitable. At the same time, the strategy will be most effective if automated and set to run periodically or around the clock. Watch for future articles on implementing crypto trading strategies with AWS EC2 instances or Lambda functions.

More content at plainenglish.io

Source: https://python.plainenglish.io/crypto-arbitrage-with-networkx-and-python-638166e5a947?source=rss——-8—————–cryptocurrency

Time Stamp:

More from Medium