Question: TIIIThis programming assignment will give you experience with using graphs, and crypto currency exchange trading.Different cryptocurrencies have different exchange rates. For example, the exchange rates

TIIIThis programming assignment will give you experience with using graphs, and crypto currency exchange trading.Different cryptocurrencies have different exchange rates. For example, the exchange rates for Bitcoin to Ethereum crypto currencies are:Bitcoin to Ethereum 32.34243When the path to and from the nodes are in eq
Ethereum to Bitcoin for 0.03092437
The price is quoted as a currency pair, and can be read as: if you have one bitcoin and you want to trade it for ethereum, you will receive 32.34 ethereum for your one bitcoin. To create a graph, you will enter the nodes/edges into the graph like this:(node_from, node_to, directed_edge_weight) Using this convention, here is the syntax to enter the currency pairs above, into a graph:
import networkx as nx
g = nx.DiGraph()
g.add_weighted_edges_from([('eth','btc',0.03092437),('btc', 'eth', 32.34243)])
Your program will search the graph, for paths which maximize the amount of money traded from one currency to another. For example, to go from Bitcoin (btc) to Ethereum (eth) you would receive 32.34234 Ethereum coins. However, there also exists a path:
['btc','xrp','bch', 'eos', 'ltc', 'eth']32.364755582613085
If you start from bitcoin, and then trade to ripple, bitcoin-cash (a separate currency from bitcoin), eos, litecoin, to ethereum, you will receive 32.364 Ethereum coins. An additional 0.022 ethereum, which is worth $37.84!!!!
Note: these are real prices, from coingecko, as of the time I wrote this assignment.
When this scenario occurs we refer to it as dis-equilibrium. Your program will search all paths in the graph from every currency to every other currency for all possible paths. Then your program will add logic to detect dis-equilibrium.
The easiest way to do this, is to find a path from node1 to node2, and also a path from node2 to node1. Then multiply the weights in the path from node1 to node2 and the weights in the path from node2 to node1. If the paths are in equilibrium, when you multiply the path weights they should be exactly 1.0.
If the value when you multiply the path weights, is not exactly 1.0, you have found dis-equilibrium, and an arbitrage opportunity!
Data Requirements
Your program will get exchange rates from coingecko
The coingecko API details for /simple/price can be found here:
coingecko api website
It returns the following JSON:
{"ethereum":{"eth":1.0,"btc":0.0309095},"bitcoin":{"eth":32.335902,"btc":1.0}}
Your program should parse the JSON, then enter the ticker symbol as the node name, for each currency pair. Meaning you only have one node for btc(not bitcoin), like this:
g.add_weighted_edges_from([('eth','btc',0.03092437),('btc', 'eth', 32.34243)])
Your program only needs to trade the top 7 crypto currencies (there are hundreds more). Use these ids and vs_currencies in the JSON api url above.
Note: the coin full name (id), and ticker (vs_currencies) are both required in the url to get the price quote.
Your program needs to get the most recent prices every time your program runs. Meaning you cannot copy/paste the json by hand, save it, and run your program.
Programming Requirements
You will need the following code snippets to create your graph, search paths, and calculate the weights.
We will be using the networkx python library for building graphs, because it is very easy to use. For documentation on networkx visit:
networkx
To create a graph:
import networkx as nx
g = nx.DiGraph(
To add edges:
g.add_weighted_edges_from([('eth','btc',0.03092437),('btc', 'eth', 32.34243)])
To calculate the weight (currency exchange rate) of an edge:
g[btc][eth]['weight'] # returns 32.34243
g[eth][btc][weight] # returns 0.0309095
To see all the nodes in a graph use:
g.nodes # list of all nodes
To see all paths from node1 to node2:
nx.all_simple_paths(g,btc,eth) # returns every path from btc to eth. Each path is returned as a list of strings.
The function above will return all paths from node1 to node2(in this case btc and eth). After you have all the paths from node1 to node2, your program needs to do the following for each path.
For each path from node1 to node2 calculate the weight of the path, by multiplying all the edges of the weights in the path.
For example, one of the paths from btc to eth is ['btc','xrp', 'eth'].
To calculate the weight of the path multiply the weights of all the edges in the path:
g[btc][xrp][weight]* g[xrp][eth][weight] # 32.5358904
Then calculate the weight of the reverse path. For the example above, the reverse path would be [eth,xrp,btc]. The path weight is calculated as follows:
g[eth][xrp][weight]* g[xrp][btc][weight] # 0.03078474
Now that you have the weight of the path to and from node1 and node2, multiply them both:
32.5358904*0.03078474*=1.0016089266324961
When the path to and from the nodes are in equilibrium, the factor of both path weights, will be exactly 1.0.
As you can see in this example
TIIIThis programming assignment will give you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!