Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interesting Problem (Currency arbitrage)

Tags:

Arbitrage is the process of using discrepancies in currency exchange values to earn profit.
Consider a person who starts with some amount of currency X, goes through a series of exchanges and finally ends up with more amount of X(than he initially had).
Given n currencies and a table (nxn) of exchange rates, devise an algorithm that a person should use to avail maximum profit assuming that he doesn't perform one exchange more than once.

I have thought of a solution like this:

  1. Use modified Dijkstra's algorithm to find single source longest product path.
  2. This gives longest product path from source currency to each other currency.
  3. Now, iterate over each other currency and multiply to the maximum product so far, w(curr,source)(weight of edge to source).
  4. Select the maximum of all such paths.

While this appears good, i still doubt of correctness of this algorithm and the completeness of the problem.(i.e Is the problem NP-Complete?) as it somewhat resembles the traveling salesman problem.

Looking for your comments and better solutions(if any) for this problem.

Thanks.

EDIT:
Google search for this topic took me to this here, where arbitrage detection has been addressed but the exchanges for maximum arbitrage is not.This may serve a reference.

like image 860
sud03r Avatar asked Feb 17 '10 16:02

sud03r


People also ask

What are the possible reasons that you may not earn profits from triangular arbitrage opportunity?

A profitable trade is only possible if there exist market imperfections. Profitable triangular arbitrage is very rarely possible because when such opportunities arise, traders execute trades that take advantage of the imperfections and prices adjust up or down until the opportunity disappears.

Is triangular arbitrage still possible?

Triangular arbitrage opportunities rarely exist in the real world. This can be explained by the nature of foreign currency exchange markets. Forex markets are extremely competitive with a large number of players, such as individual and institutional traders.

Why do arbitrage situation arises in a foreign exchange market?

It can be observed that arbitrage opportunities exist in the official fx market. It arises as a result of the differences between the buying rate and selling rate of the various hard currencies.

Is currency arbitrage possible?

Currency arbitrage is the exploitation of differences in quotes offered by brokers. Currency arbitrage can be practiced using different strategies, such as two-currency arbitrage and three-currency arbitrages.


2 Answers

Dijkstra's cannot be used here because there is no way to modify Dijkstra's to return the longest path, rather than the shortest. In general, the longest path problem is in fact NP-complete as you suspected, and is related to the Travelling Salesman Problem as you suggested.

What you are looking for (as you know) is a cycle whose product of edge weights is greater than 1, i.e. w1 * w2 * w3 * ... > 1. We can reimagine this problem to change it to a sum instead of a product if we take the logs of both sides:

log (w1 * w2 * w3 ... ) > log(1)

=> log(w1) + log(w2) + log(w3) ... > 0

And if we take the negative log...

=> -log(w1) - log(w2) - log(w3) ... < 0 (note the inequality flipped)

So we are now just looking for a negative cycle in the graph, which can be solved using the Bellman-Ford algorithm (or, if you don't need the know the path, the Floyd-Warshall algorihtm)

First, we transform the graph:

for (int i = 0; i < N; ++i)   for (int j = 0; j < N; ++j)     w[i][j] = -log(w[i][j]); 

Then we perform a standard Bellman-Ford

double dis[N], pre[N];  for (int i = 0; i < N; ++i)    dis[i] = INF, pre[i] = -1;  dis[source] = 0;  for (int k = 0; k < N; ++k)   for (int i = 0; i < N; ++i)     for (int j = 0; j < N; ++j)       if (dis[i] + w[i][j] < dis[j])         dis[j] = dis[i] + w[i][j], pre[j] = i; 

Now we check for negative cycles:

for (int i = 0; i < N; ++i)   for (int j = 0; j < N; ++j)     if (dis[i] + w[i][j] < dis[j])       // Node j is part of a negative cycle 

You can then use the pre array to find the negative cycles. Start with pre[source] and work your way back.

like image 127
Peter Alexander Avatar answered Sep 30 '22 19:09

Peter Alexander


The fact that it is an NP-hard problem doesn't really matter when there are only about 150 currencies currently in existence, and I suspect your FX broker will only let you trade at most 20 pairs anyway. My algorithm for n currencies is therefore:

  1. Make a tree of depth n and branching factor n. The nodes of the tree are currencies and the root of the tree is your starting currency X. Each link between two nodes (currencies) has weight w, where w is the FX rate between the two currencies.
  2. At each node you should also store the cumulative fx rate (calculated by multiplying all the FX rates above it in the tree together). This is the FX rate between the root (currency X) and the currency of this node.
  3. Iterate through all the nodes in the tree that represent currency X (maybe you should keep a list of pointers to these nodes to speed up this stage of the algorithm). There will only be n^n of these (very inefficient in terms of big-O notation, but remember your n is about 20). The one with the highest cumulative FX rate is your best FX rate and (if it is positive) the path through the tree between these nodes represents an arbitrage cycle starting and ending at currency X.
  4. Note that you can prune the tree (and so reduce the complexity from O(n^n) to O(n) by following these rules when generating the tree in step 1:
    1. If you get to a node for currency X, don't generate any child nodes.
    2. To reduce the branching factor from n to 1, at each node generate all n child nodes and only add the child node with the greatest cumulative FX rate (when converted back to currency X).
like image 38
Nicholas White Avatar answered Sep 30 '22 18:09

Nicholas White